Watch, Follow, &
Connect with Us

For forums, blogs and more please visit our
Developer Tools Community.


Welcome, Guest
Guest Settings
Help

Thread: WithEvents,WaitOne, Set()


This question is not answered. Helpful answers available: 2. Correct answers available: 1.


Permlink Replies: 3 - Last Post: Sep 1, 2016 10:11 AM Last Post By: Remy Lebeau (Te...
Fred Smith

Posts: 81
Registered: 12/4/15
WithEvents,WaitOne, Set()  
Click to report abuse...   Click to reply to this thread Reply
  Posted: Sep 1, 2016 4:14 AM
Hi,
Can any1 tell me what's the equivalent to these VB.NET lines in Delphi ?
The WithEvents, WaitOne(5000), & set() are concise & work very well.
Can anyone convert these lines to Delphi ? I'm using a console application.
Not using any threads at this moment. Do I need to create a thread.
I would be grateful if you could give any example.
Thank you.

Module Module1
  Private WithEvents MyObj As MyInterface
  Private WaitForInitialiseLibrary As AutoResetEvent = New AutoResetEvent(False)
 
Sub Main()
  MyObj.InitialiseLibrary(Myparam1, Myparam2,...)
 
  If WaitForInitialiseLibrary.WaitOne(5000) = False Then
    Throw New Exception("Operation timed out")
  End If
End Sub
 
//A call back event
Private Sub InitialiseLibraryCallBack() Handles Obj.InitialiseLibraryCallBack
  WaitForInitialiseLibrary.Set()
End Sub
 
Peter Below

Posts: 1,227
Registered: 12/16/99
Re: WithEvents,WaitOne, Set()  
Click to report abuse...   Click to reply to this thread Reply
  Posted: Sep 1, 2016 5:51 AM   in response to: Fred Smith in response to: Fred Smith
Fred Smith wrote:

Hi,
Can any1 tell me what's the equivalent to these VB lines in Delphi ?
The WithEvents, WaitOne(5000), & set() are concise & work very well.
Can anyone convert these lines to Delphi ? I'm using a console
application. Not using any threads at this moment. Do I need to
create a thread. I would be grateful if you could give any example.
Thank you.

Module Module1
  Private WithEvents MyObj As MyInterface
  Private WaitForInitialiseLibrary As AutoResetEvent = New
AutoResetEvent(False)
 
Sub Main()
  MyObj.InitialiseLibrary(Myparam1, Myparam2,...)
 
  If WaitForInitialiseLibrary.WaitOne(5000) = False Then
    Throw New Exception("Operation timed out")
  End If
End Sub
 
//A call back event
Private Sub InitialiseLibraryCallBack() Handles
Obj.InitialiseLibraryCallBack   WaitForInitialiseLibrary.Set()
End Sub
 

Look at the SyncObjs unit, TSimpleEvent is what you need for the
waiting part.

Is this VB6 or VB .NEt code? In VB6 objects are OLE automation objects
with an interface based on IDispatch. The withEvents modifier (if
memory servers) tells VB to implements a number of additional COM
interfaces on the object it creates for you, to support COM events. See
https://msdn.microsoft.com/en-us/library/1hee64c7(v=vs.100).aspx


--
Peter Below
TeamB

Fred Smith

Posts: 81
Registered: 12/4/15
Re: WithEvents,WaitOne, Set()  
Click to report abuse...   Click to reply to this thread Reply
  Posted: Sep 1, 2016 8:06 AM   in response to: Peter Below in response to: Peter Below
Peter Below wrote:
Fred Smith wrote:

Hi,
Can any1 tell me what's the equivalent to these VB lines in Delphi ?
The WithEvents, WaitOne(5000), & set() are concise & work very well.
Can anyone convert these lines to Delphi ? I'm using a console
application. Not using any threads at this moment. Do I need to
create a thread. I would be grateful if you could give any example.
Thank you.

Look at the SyncObjs unit, TSimpleEvent is what you need for the
waiting part.

Is this VB6 or VB .NEt code? In VB6 objects are OLE automation objects
with an interface based on IDispatch. The withEvents modifier (if
memory servers) tells VB to implements a number of additional COM
interfaces on the object it creates for you, to support COM events. See
https://msdn.microsoft.com/en-us/library/1hee64c7(v=vs.100).aspx


--
Peter Below
TeamB


Thanks for your reply.
It's VB.NET code.
All the communications with the COM library works fine.
Just not sure how to convert these couple of lines into Delphi.
Not sure how to convert WithEvents,WaitOne, & Set.

I just need to make sure the InitialiseLibraryCallBack event has been executed before the specified timeout, if not throw an error.
The few VB.NET lines of code achieve that brilliantly without timers & without booleans. It works flawlessly every time.
Not sure how to do the same in a Delphi console application.

Thanks again.
Remy Lebeau (Te...


Posts: 9,447
Registered: 12/23/01
Re: WithEvents,WaitOne, Set()  
Click to report abuse...   Click to reply to this thread Reply
  Posted: Sep 1, 2016 10:11 AM   in response to: Peter Below in response to: Peter Below
Peter wrote:

Look at the SyncObjs unit, TSimpleEvent is what you need for the
waiting part.

Just note that you might need to set its UseCOMWait parameter to True in
the constructor, depending on the COM object's threading model.

The withEvents modifier (if memory servers) tells VB to implements a
number of additional COM interfaces on the object it creates for you,
to support COM events.

Correct. On the Delphi side, when you import a COM object as a VCL component,
it should generate code to hook up the COM object's events for you.

--
Remy Lebeau (TeamB)
Legend
Helpful Answer (5 pts)
Correct Answer (10 pts)

Server Response from: ETNAJIVE02