473,594 Members | 2,655 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

timer not being enabled by thread

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.c ontrols and I get
no error message about cross thread so I am not sure where the problem is.

I have a "SerialPoll_tim er" 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 "BusyWritin g" 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 RemoteTimerEnab le(bool bEnable)
{
SerialPoll_time r.Enabled = bEnable;
if (bEnable) SerialPoll_time r.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.Wa itOne() when
the disable occures, can this be why it cannot be re-enabled?
--
=============== =============== =============== =============== ==========
Joseph "Beemer Biker" Stateson
http://ResearchRiders.org Ask about my 99'R1100RT
=============== =============== =============== =============== ==========
Jun 20 '07 #1
3 3835
On Wed, 20 Jun 2007 08:31:54 -0700, Beemer Biker <js*******@swri .edu>
wrote:
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.c ontrols 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
Jun 20 '07 #2
"Peter Duniho" <Np*********@nn owslpianmk.comw rote in message
news:op******** *******@petes-computer.local. ..
On Wed, 20 Jun 2007 08:31:54 -0700, Beemer Biker <js*******@swri .edu>
wrote:
>Unaccountabl y, 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.c ontrols 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.
Thanks, I was unaware those tools existed nor have I used them. OK, i
looked at

this.Invoke(Del egate method, params object[] args)
and
this.InvokeOnCl ick(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.C lick 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(1 000);" [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(10 00); 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(ms econds) 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..
--
=============== =============== =============== =============== ==========
Joseph "Beemer Biker" Stateson
http://ResearchRiders.org Ask about my 99'R1100RT
=============== =============== =============== =============== ==========

Jun 20 '07 #3
On Wed, 20 Jun 2007 11:29:51 -0700, Beemer Biker <js*******@swri .edu>
wrote:
>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.

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

this.Invoke(Del egate method, params object[] args)
and
this.InvokeOnCl ick(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
Jun 20 '07 #4

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

Similar topics

3
16861
by: Peter Johnsson | last post by:
How come the eventhandler for the timer's elapsed time event is called over and over again, even though the AutoReset property is set to false, if you assign a new value to the timer objects interval property inside the event handler? Example follows: Constructor: mTimer = new System.Timers.Timer(20000); mTimer.AutoReset = false;
8
2770
by: Daniel P. | last post by:
I'm trying to set a timer that gets called every 3 seconds so I can update a field in the UI with the time elapsed since the process started. What am I doing wrong that timerDF_Tick does not get called? private System.Windows.Forms.Timer timerDF; this.timerDF = new System.Windows.Forms.Timer(this.components);
11
24042
by: avivgur | last post by:
Hello, I have devised a console application that uses a System.Timers.Timer and its Elapsed event. The problem is that I want the program to continue to run so that each time the event is raised, the handler will be executed. I tried using a while(timer.Enabled) {} loop but it made the process use 90% cpu in the task manager. What is the correct way to wait for an event and keep the program alive in a console application? Thanks, Aviv.
10
27661
by: Vincent | last post by:
Hi, I was trying to do something in a Thread that starts by a timer. I turn off the timer at the begining of the thread incase it fires again when the thread is still running. Then turn on the timer again before the thread finishes. To my surprise, the timer never come alive again once it has been turned of in the thread. To test the problem I made a simple Windows Form project, put a Beep() in the timer. It supposed to hear beep beep...
10
1650
by: Bob | last post by:
Okay, I've done this for years but now I'm going to question it just because this idea has been at the back of my head since I started using DotNet... My WinForms app queries a database every 60 seconds. I set up a singleton class that's instantiated when the app starts, and that starts a timer. My question is, would it be better to run this checking process on a separate thread and have it use its own DB connection? If there isn't a...
4
2885
by: Rich P | last post by:
Greetings, I have a routine I was running in VB6 on a timed schedule. When the timeframe came up, the timer would be disable, the routine would run, and the timer gets enabled. I am trying to migrate this project to vb.net. But when the timer gets disable it is not getting re-enabled after the routine is completed. I added Application.DoEvents just before the call to Timer1.Enabled = True. But that did not help. Is there something...
2
4048
by: John David Thornton | last post by:
I've got a Windows Service class, and I put a System.Threading.Timer, and I've coded it as shown below. However, when I install the service and then start it in MMC, I get a peculiar message: The MyService service on Local Computer started and then stopped. Some services stop automatically if they ahve no work to do, for example, the Perforamnce Logs and Alert service. I tried switching to a System.Threading.Timer and that didn't work...
17
1869
by: Ratnesh Raval | last post by:
hi all, i m having some problem in timer control. sub timer.tick() timer.stop() do...something timer.enabled = true end sub
7
5988
by: RobKinney1 | last post by:
Hello, Wow...I have one for you all and hopefully I am not understanding this timer object correctly. I have a timer setup that pulses a connection through a socket every 60 seconds. But it seems recently connections just drop off because the timer stops firing. My question is if there is a timeout in the timer event that just shuts down the call if the timer event is taking too long to complete...?
0
7941
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
7874
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8246
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
8000
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
6652
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
5738
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5404
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
1
2383
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
0
1205
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.