473,396 Members | 1,914 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Me.InvokeRequired

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
Jan 15 '08 #1
4 10734
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

"Guy Cohen" <Gu******@discussions.microsoft.comschreef in bericht
news:8B**********************************@microsof t.com...
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

Jan 15 '08 #2
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

"Michel Posseth [MCP]" wrote:
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

"Guy Cohen" <Gu******@discussions.microsoft.comschreef in bericht
news:8B**********************************@microsof t.com...
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


Jan 15 '08 #3
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

"Guy Cohen" <Gu******@discussions.microsoft.comschreef in bericht
news:18**********************************@microsof t.com...
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

"Michel Posseth [MCP]" wrote:
>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

"Guy Cohen" <Gu******@discussions.microsoft.comschreef in bericht
news:8B**********************************@microso ft.com...
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



Jan 15 '08 #4
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

"Michel Posseth [MCP]" <MS**@posseth.comwrote in message
news:uC**************@TK2MSFTNGP04.phx.gbl...
>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

"Guy Cohen" <Gu******@discussions.microsoft.comschreef in bericht
news:18**********************************@microsof t.com...
>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

"Michel Posseth [MCP]" wrote:
>>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

"Guy Cohen" <Gu******@discussions.microsoft.comschreef in bericht
news:8B**********************************@micros oft.com...
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


Jan 16 '08 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Sgt. Sausage | last post by:
Problem. Work-around. No issues, just looking to see if I'm the only one that's seen this. I just lost 3 hours (shoulda found it sooner) to the InvokeRequired on a form being somewhat...
2
by: BoloBaby | last post by:
Earlier, I had a threading issue where I had to use the InvokeRequired to get my controls to function properly. Does InvokeRequired apply to my custom classes as well? That is, if I have...
2
by: Samuel R. Neff | last post by:
I'm trying to find a good way to handle Control.InvokeRequired without duplicating four lines of code in every function/event. Typically what I've seen in books is this: If InvokeRequired Then...
5
by: Michael C# | last post by:
Hi all, I set up a System.Timers.Time in my app. The code basically just updates the screen, but since the processing performed is so CPU-intensive, I wanted to make sure it gets updated...
1
by: Mesan | last post by:
I'm getting a "Cross-thread operation not valid" Exception and I can't figure out why. I've got a BackgroundWorker that creates a UserControl with a whole lot of other user controls inside of...
1
by: Mark Denardo | last post by:
Hey all, I'm having a problem with the following: Private m_myDELEGATE As New myDELEGATE(AddressOf ReceivingServerMessage_UIThread) Delegate Sub myDELEGATE(ByVal ReceivingServerMessage As...
5
by: RobKinney1 | last post by:
Wow... unbelieveable that this problem would arise right before giving the software to our public testers... or maybe it is believable. We tweaked some seemingly unrelated code somewhere else...
4
by: Bill McCormick | last post by:
Hello, A timer control (I think) runs in another thread apart from a main form. In the OnTick event, you can update some form control with no worries. I'm making an AsyncronousServer class...
1
by: Curious | last post by:
I have the following code: if (this.InvokeRequired) { IAsyncResult result = this.BeginInvoke(new EventHandler(this.UpdateButtons), new object { this, EventArgs.Empty }); while...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.