Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Languages / C# / June 2007

Tip: Looking for answers? Try searching our database.

timer not being enabled by thread

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Beemer Biker - 20 Jun 2007 16:31 GMT
Unaccountably, I cannot re-enable a timer from an background thread.  The
disable works fine, I just cannot get it to start back up.  There is no
method "InvokeRequired" like there is for windows.forms.controls and I get
no error message about cross thread so I am not sure where the problem is.

I have a "SerialPoll_timer" that every second requests status from a device
on an RS232 port.  I use this to determine if the device is on line.  There
is a mutex "BusyWriting" associated with the serial port handler to allow
this timer task and other threads to all send info out the same serial port.
This all works fine.

Occassionally, I need to send a long series of initialization commands to
the device.  These commands are sent from a background worker thread.  Since
there is a long list of commands I thought I would have the background work
disable the serial poll timer, send out all the commands, then re-enable the
serial poll timer.

If I click on a button which does the initialization (ie: the bw thread is
not used)  the timer thread gets disabled and re-enabled just fine.  When I
started using the background worker to send the initialization commands the
timer never starts back up.  This is all I do to the timer.  I threw in the
"start" but that didnt help.  I can even step thru this code in the debugger
and the timer will not start up. I have to quit the program to get it to
work again.

public void RemoteTimerEnable(bool bEnable)
{
   SerialPoll_timer.Enabled = bEnable;
  if (bEnable) SerialPoll_timer.Start();
}

So, if I call the above routine from a "button" it works but from a
background task things seem to go wrong.    Maybe there is some other
problem?  If the timer thread is in the mutex BusyWriteing.WaitOne() when
the disable occures, can this be why it cannot be re-enabled?

Signature

======================================================================
Joseph "Beemer  Biker" Stateson
http://ResearchRiders.org     Ask about my 99'R1100RT
======================================================================

Peter Duniho - 20 Jun 2007 18:06 GMT
> Unaccountably, I cannot re-enable a timer from an background thread.  
> The disable works fine, I just cannot get it to start back up.  There is  
> no method "InvokeRequired" like there is for windows.forms.controls and  
> I get no error message about cross thread so I am not sure where the  
> problem is.

Just because there's no Invoke() on the timer itself, that doesn't mean  
that you can't use Invoke().  Just use it on the form to which the timer  
belongs instead.

Pete
Beemer Biker - 20 Jun 2007 19:29 GMT
>> Unaccountably, I cannot re-enable a timer from an background thread.
>> The disable works fine, I just cannot get it to start back up.  There is
[quoted text clipped - 5 lines]
> that you can't use Invoke().  Just use it on the form to which the timer
> belongs instead.

Thanks, I was unaware those tools existed nor have I used them.  OK, i
looked at

this.Invoke(Delegate method, params object[] args)
  and
this.InvokeOnClick(Control toIinvoke, EventArgs e)

I have not coded anything up yet, just looked at them and have this
observation ...

If (as I mentioned on original post) I have a button object that can
correctly start and stop the timer, then I assume I can put that control
into the InvokeOnClick and quote "Raise the
System.Windows.Forms.Control.Click event for the specified control"   If so,
I assume the controls "underlying thread" handles this as intellisense seems
to indicate..  If so, then I am certain the following problem will show up:
I have found that when I "click" on a button, and the code it calls includes
a "Thread.Sleep(1000);"  [for example], the form is incapable of fireing any
callbacks during that one second wait or any other.  Any timers, serial data
received, etc are never fired until the thread (from the button click) does
a final return.   On the other hand, if the Thread.Sleep(1000); is executed
by a background worker process then all the other events seem to work just
fine on the form while that BW thread is sleeping.

The long initialization procedure I need to send to the serial device
includes a bunch of Thread.Sleep(mseconds) because some of the commands to
the device require a delay to allow hardware to perform a task.

So, I suspect I cannot use the InvokeOnClick

However, you mentioned Invoke as used on the form.  So maybe I can have a
delegate routine handle turning on and off the enable boolean on the timer?
I am not sure how to set that up.  I will look at it and see what it does
(ie: poke around).  Can you point me to any example code where someone used
"this.invoke(..)" with a delegate?  One would think a cross thread error
message would show up if an invoke was required but I have not seen one. All
I have seen is the timer does stop but never gets re-enabled.

..thanks..

Signature

======================================================================
Joseph "Beemer  Biker" Stateson
http://ResearchRiders.org     Ask about my 99'R1100RT
======================================================================

Peter Duniho - 20 Jun 2007 20:31 GMT
>> Just because there's no Invoke() on the timer itself, that doesn't mean  
>> that you can't use Invoke().  Just use it on the form to which the  
[quoted text clipped - 6 lines]
>    and
> this.InvokeOnClick(Control toIinvoke, EventArgs e)

Just one more example of where .NET carelessly uses the same word for very  
different things.  Please don't mix up Invoke() with InvokeOnClick().  The  
latter simply raises an event, while the former has a specific behavior  
with respect to the control's owning thread's message pump.  The two  
methods are not related at all in functionality.

> [...]
> So, I suspect I cannot use the InvokeOnClick

That is correct.  It has nothing to do with the issue you've described.

> However, you mentioned Invoke as used on the form.  So maybe I can have  
> a delegate routine handle turning on and off the enable boolean on the  
> timer?

Yes, this is what I was suggesting.

> I am not sure how to set that up.  I will look at it and see what it  
> does (ie: poke around).  Can you point me to any example code where  
> someone used "this.invoke(..)" with a delegate?

There is an example here:
http://msdn2.microsoft.com/en-us/library/zyzhdc6b.aspx

You can only use Invoke() with a delegate, so any example code you find  
that uses Invoke() will be an example of code that uses Invoke() with a  
delegate.

> One would think a cross thread error message would show up if an invoke  
> was required but I have not seen one. All I have seen is the timer does  
> stop but never gets re-enabled.

I agree that a cross-thread MDA should occur, but note that the Timer  
class itself is not actually the form.  It's possible that the way that  
the MDA exception is set up, it only detects cross-thread calls within the  
actual Control class, rather than the more general case.

I don't actually know how the MDA works, so I can't comment on why it  
wouldn't fire in this case.  I also can't guarantee you that you're  
running into a cross-thread issue.  I just know that it stands to reason  
that you might be, since the Forms.Timer class does use the underlying  
WM_TIMER mechanism, and I think it likely that does require that calls be  
made on the correct thread.

It's very easy to do your work on the correct thread, using Invoke(), so  
it seems to me that's the first thing to try.  That may or may not fix  
it.  If it does, great.  If not, then you can pursue other possibilities.

Pete

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.