Service Event Handling

  • Thread starter Thread starter bclegg
  • Start date Start date
B

bclegg

Hi,
I am trying to use a 3rd Party telephony (Intel's CT-ADE 8.3) library in
a vb.net service.
The way it hangs up is to raise an Event.
If you build a windows Application you can write:
Sub DoSomeWork
While not Hungup
Do lots of good things
application.doevents
end While
end sub

The hangup event handler sets variable hungup to true.

In a service you do not have 'application.doevents'
So the eventhandler doesn't get a chance to set the variable.
Is these some equivalent to 'application.doevents' for services?
Thanks
Bob
 
Hi Bob,

First of all, I would like to confirm my understanding of your issue.
From your description, I understand that you wants to use the something
similar with Application.Doevents in windows service.
Have I fully understood you? If there is anything I misunderstood, please
feel free to let me know.

Based on my knowledge, Application.Doevents is used to Processes all
Windows messages currently in the message queue. The message is somewhat
different from the event we said in the RaiseEvent to some extent.

The event in the .net is similar with an callback mechanism, raiseevent is
somewhat similar with the call the delegate(i.e. the function point in C++
category)
You may take a look the link below about event and delegate.

Events and Delegates
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpguide/htm
l/cpconeventsdelegates.asp

Raising an Event
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpguide/htm
l/cpconprovidingeventfunctionality.asp

e.g.
[Service1]
Public WithEvents tc As TestClass

Protected Overrides Sub OnStop()
tc = New TestClass
Dim i As Integer
i = 0
While Not tc.flag
i += 1
If i > 4 Then
tc.FireEvent()
End If
End While
' Add code here to perform any tear-down necessary to stop your
service.
End Sub

Private Sub tc_TestEvent() Handles tc.TestEvent
tc.flag = True
End Sub

[TestClass]
Public Class TestClass
Public Sub New()
flag = False
End Sub
Public Event TestEvent()
Public Sub FireEvent()
RaiseEvent TestEvent()
End Sub
Public flag As Boolean
End Class

In the code snippet above, we do not need the mesage loop to fire the event
to set the flag.

If the control you are using is depended on the message loop to handle the
event, that is to say, if we must need to generate a message loop for the
windows service, which by default will not have the message loop, by
calling the System.Windows.Forms.Application.Run (please add reference to
the system.windows.forms.dll). But if we call it in the main thread, the
thread will be stop at the System.Windows.Forms.Application.Run() code
line. Usually we not use the message loop in the windows services, and it
is an backgroup application.

So I am not sure how do you change the value Hungup by raising event. Did
the snippet code I provide help you? Or can you provide more information
about how you change the Hungup? e.g. In which condition, you will change
the Hungup, do you have concern to run more than one thread in the windows
service, so that we can run the application.run in other thread, and handle
the message loop issue in that thead, if certain condition is met, we can
changed synchronized object's value to tell the main thread that something
had happened.

And what do you wants to use the third party library for?

Please apply my suggestion above and let me know if it helps resolve your
problem.

Best regards,

Peter Huang
Microsoft Online Partner Support

Get Secure! - www.microsoft.com/security
This posting is provided "AS IS" with no warranties, and confers no rights.
 
Hi Bob,

Did my suggestion help you?
If you still have any concern on this issue, please feel free to post here.

Best regards,

Peter Huang
Microsoft Online Partner Support

Get Secure! - www.microsoft.com/security
This posting is provided "AS IS" with no warranties, and confers no rights.
 
Hi Peter,
My apologies for not replying sooner.
I have been out of the Office.
I have been having some seconds thoughts on the construction of the
service, so I won't bother you any more on this issue at present.
Thanks
Bob




Peter said:
Hi Bob,

First of all, I would like to confirm my understanding of your issue.
From your description, I understand that you wants to use the something
similar with Application.Doevents in windows service.
Have I fully understood you? If there is anything I misunderstood, please
feel free to let me know.

Based on my knowledge, Application.Doevents is used to Processes all
Windows messages currently in the message queue. The message is somewhat
different from the event we said in the RaiseEvent to some extent.

The event in the .net is similar with an callback mechanism, raiseevent is
somewhat similar with the call the delegate(i.e. the function point in C++
category)
You may take a look the link below about event and delegate.

Events and Delegates
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpguide/htm
l/cpconeventsdelegates.asp

Raising an Event
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpguide/htm
l/cpconprovidingeventfunctionality.asp

e.g.
[Service1]
Public WithEvents tc As TestClass

Protected Overrides Sub OnStop()
tc = New TestClass
Dim i As Integer
i = 0
While Not tc.flag
i += 1
If i > 4 Then
tc.FireEvent()
End If
End While
' Add code here to perform any tear-down necessary to stop your
service.
End Sub

Private Sub tc_TestEvent() Handles tc.TestEvent
tc.flag = True
End Sub

[TestClass]
Public Class TestClass
Public Sub New()
flag = False
End Sub
Public Event TestEvent()
Public Sub FireEvent()
RaiseEvent TestEvent()
End Sub
Public flag As Boolean
End Class

In the code snippet above, we do not need the mesage loop to fire the event
to set the flag.

If the control you are using is depended on the message loop to handle the
event, that is to say, if we must need to generate a message loop for the
windows service, which by default will not have the message loop, by
calling the System.Windows.Forms.Application.Run (please add reference to
the system.windows.forms.dll). But if we call it in the main thread, the
thread will be stop at the System.Windows.Forms.Application.Run() code
line. Usually we not use the message loop in the windows services, and it
is an backgroup application.

So I am not sure how do you change the value Hungup by raising event. Did
the snippet code I provide help you? Or can you provide more information
about how you change the Hungup? e.g. In which condition, you will change
the Hungup, do you have concern to run more than one thread in the windows
service, so that we can run the application.run in other thread, and handle
the message loop issue in that thead, if certain condition is met, we can
changed synchronized object's value to tell the main thread that something
had happened.

And what do you wants to use the third party library for?

Please apply my suggestion above and let me know if it helps resolve your
problem.

Best regards,

Peter Huang
Microsoft Online Partner Support

Get Secure! - www.microsoft.com/security
This posting is provided "AS IS" with no warranties, and confers no rights.
 
Back
Top