Me.InvokeRequired


G

Guy Cohen

Hi all

I wrote a small demo program to understand timers and delegation - and its
working fine.

However, I would like to use the timer in a DLL(Class)

When I compile the code:

Protected Sub Timer2_Elapsed(ByVal sender As Object, ByVal e As
ElapsedEventArgs)

If Me.InvokeRequired Then
Dim MyIncrement As New IncrementDelegate(AddressOf MyTimerHander)
Me.BeginInvoke(MyIncrement)
End If

End Sub

I get the error InvokeRequired is not a part of <my class name goes here>

Please advise

TIA
Guy Cohen
 
Ad

Advertisements

M

Michel Posseth [MCP]

InvokeRequired is only used with GUI elements a code dll is obviously not a
GUI element so there is your problem
if you want to update a GUI element with the timers event then you need to
use InvokeRequired on that element in the called method of the GUI class

hth

Michel
 
G

Guy Cohen

Hi
Well...
I found out that in services there is no need to delegate when you work with
timers (afaik..)

So there is no problem :)

Guy Cohen
 
M

Michel Posseth [MCP]

Well...
I found out that in services there is no need to delegate when you work
with
timers (afaik..)

You did not provide this info in your previous post, i asumed you were bussy
with a threading mechanism as you only need control.InvokeRequired to
synchronize with the GUI thread , as a service does usually not provide a
GUI ( although not impossible with some help of remoting or named pipes etc
etc )
you are right .

By the way it is not such a good idea to use Timers in a windows service

Windows timers can`t be used at all , a timer that does work is the system
threading timer , however it is much easier and reliable to use a loop with
a thread sleep for x miliseconds

i myself have always written my windows services with a threading timer ,
until we had once a discussion here in this newsgroup ( June 2007 )
where one of the active and valued persons here brought up a good point

<<<<

IMHO, there's one very Good Reason for /not/ using a Timer.

It's called JIT compilation.


You write a Service and put a Timer in it.
You code up the routine that the Timer calls.
This routine references an external Assembly.


You deploy the Service.
Somehow, you miss the dependent assembly.


Your deployed service runs!
It starts and stops perfectly!
It doesn't report /any/ errors or Exceptions!


But it never does any useful work.


Why???


When the Timer fires, the runtime attempts to JIT the method invoked by
the Timer.
With the referenced Assembly /missing/, this JIT-linking fails but the
runtime doesn't report this and, if the Exception gets logged anywhere,
I've /never/ managed to find it. You can't catch this Exception - it's
gets thrown into the depths of the runtime and the Service
Infrastructure - you never see it.


I've found that calling the "worker" method from another one inside the
service - one /with/ a loop and Sleep(s) - works 100% reliably, and you
even get to catch the Exception if you really want to. ;-)


Regards,
Phill W.


Thought this might be interesting info for you

HTH

Michel Posseth
 
Ad

Advertisements

G

Guy Cohen

Hi again

Well.. I did use an while/wend endless loop at first
But then it was difficult to stop the service.
Then I used 2 threads one for "service start command" and one for "service
stop command" (I wrote the service as a - small service that has two methods
stop and start and a dll that actually does everything and includes
stop/start methods - very easy to debug this way with a form that uses that
class/dll).
I added the timers as a watchdog (My guess is that you know what it is...)
It works 100% now :)

HTH
Guy Cohen
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top