473,387 Members | 1,669 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,387 software developers and data experts.

Timer and Threading problem

Hello Experts,

Currently I am working on a windows application using VS.NET 2002 with
C#.

On one of my form, I have a refresh button which will dispose created
controls and re-create them. I need the refresh perform in every certain
period, so I wrote a timer function to perform this:

...
private void InitTimer()
{
timerClock = new System.Timers.Timer();
this.timerClock.Elapsed += new ElapsedEventHandler(OnTimer);
this.timerClock.Interval = 1000 * 60; // every minute
this.timerClock.Enabled = true;
}

private void OnTimer(object sender, ElapsedEventArgs e)
{
try{ Refresh_Click(null, null); }
catch( Exception ex ){ MessageBox.Show(ex.ToString());}
}

Everything working on the timer, it fired up an event (call the OnTimer
function) in every minute.
However, when it execute Refresh_Click(null, null) in the OnTimer
function, it gave me an exception error:

System.ArgumentException: Controls created on one thread cannot be
parented to a
control on a different thread.
at System.Windows.Forms.ControlCollection.Add(Control value)
at System.Windows.Forms.ControlCollection.Add(Control value)
...
The Refresh_Click(object sender, System.EventArgs e) works fine on it
self.

Anyone have any idea why this happened? And how can I solve this
problem?
Thanks,

Benny

*** Sent via Devdex http://www.devdex.com ***
Don't just participate in USENET...get rewarded for it!
Nov 16 '05 #1
3 2212

Why can't you launch it once and use the Timer_Tick event to raise the
necessary actions ?
Benny wrote:
Hello Experts,

Currently I am working on a windows application using VS.NET 2002 with
C#.

On one of my form, I have a refresh button which will dispose created
controls and re-create them. I need the refresh perform in every certain
period, so I wrote a timer function to perform this:

..
private void InitTimer()
{
timerClock = new System.Timers.Timer();
this.timerClock.Elapsed += new ElapsedEventHandler(OnTimer);
this.timerClock.Interval = 1000 * 60; // every minute
this.timerClock.Enabled = true;
}

private void OnTimer(object sender, ElapsedEventArgs e)
{
try{ Refresh_Click(null, null); }
catch( Exception ex ){ MessageBox.Show(ex.ToString());}
}

Everything working on the timer, it fired up an event (call the OnTimer
function) in every minute.
However, when it execute Refresh_Click(null, null) in the OnTimer
function, it gave me an exception error:

System.ArgumentException: Controls created on one thread cannot be
parented to a
control on a different thread.
at System.Windows.Forms.ControlCollection.Add(Control value)
at System.Windows.Forms.ControlCollection.Add(Control value)
..
The Refresh_Click(object sender, System.EventArgs e) works fine on it
self.

Anyone have any idea why this happened? And how can I solve this
problem?
Thanks,

Benny

*** Sent via Devdex http://www.devdex.com ***
Don't just participate in USENET...get rewarded for it!


--
w 4
Nov 16 '05 #2
You are trying to dispose/recreate your controls on a thread which is not
the UI thread of the form. To solve this, you have two choices:

1) Use the Timer in System.Windows.Forms namespace by dropping the timer
control on your form.

2) Use the form's Invoke() method from your OnTimer() event handler to
marshal the call to Refresh_Click to the form's UI thread.

See:

http://msdn.microsoft.com/library/de...asp?frame=true

Ken
"Benny" <an*******@devdex.com> wrote in message
news:O9**************@TK2MSFTNGP11.phx.gbl...
Hello Experts,

Currently I am working on a windows application using VS.NET 2002 with
C#.

On one of my form, I have a refresh button which will dispose created
controls and re-create them. I need the refresh perform in every certain
period, so I wrote a timer function to perform this:

..
private void InitTimer()
{
timerClock = new System.Timers.Timer();
this.timerClock.Elapsed += new ElapsedEventHandler(OnTimer);
this.timerClock.Interval = 1000 * 60; // every minute
this.timerClock.Enabled = true;
}

private void OnTimer(object sender, ElapsedEventArgs e)
{
try{ Refresh_Click(null, null); }
catch( Exception ex ){ MessageBox.Show(ex.ToString());}
}

Everything working on the timer, it fired up an event (call the OnTimer
function) in every minute.
However, when it execute Refresh_Click(null, null) in the OnTimer
function, it gave me an exception error:

System.ArgumentException: Controls created on one thread cannot be
parented to a
control on a different thread.
at System.Windows.Forms.ControlCollection.Add(Control value)
at System.Windows.Forms.ControlCollection.Add(Control value)
..
The Refresh_Click(object sender, System.EventArgs e) works fine on it
self.

Anyone have any idea why this happened? And how can I solve this
problem?
Thanks,

Benny

*** Sent via Devdex http://www.devdex.com ***
Don't just participate in USENET...get rewarded for it!

Nov 16 '05 #3
Ken Kolda wrote:
You are trying to dispose/recreate your controls on a thread which is not
the UI thread of the form. To solve this, you have two choices:

1) Use the Timer in System.Windows.Forms namespace by dropping the timer
control on your form.

2) Use the form's Invoke() method from your OnTimer() event handler to
marshal the call to Refresh_Click to the form's UI thread.
BeginInvoke() is generally better if you don't care about synchronous
operation (which you usually don't when you're just handling an event.)

David Logan

See:

http://msdn.microsoft.com/library/de...asp?frame=true

Ken
"Benny" <an*******@devdex.com> wrote in message
news:O9**************@TK2MSFTNGP11.phx.gbl...
Hello Experts,

Currently I am working on a windows application using VS.NET 2002 with
C#.

On one of my form, I have a refresh button which will dispose created
controls and re-create them. I need the refresh perform in every certain
period, so I wrote a timer function to perform this:

..
private void InitTimer()
{
timerClock = new System.Timers.Timer();
this.timerClock.Elapsed += new ElapsedEventHandler(OnTimer);
this.timerClock.Interval = 1000 * 60; // every minute
this.timerClock.Enabled = true;
}

private void OnTimer(object sender, ElapsedEventArgs e)
{
try{ Refresh_Click(null, null); }
catch( Exception ex ){ MessageBox.Show(ex.ToString());}
}

Everything working on the timer, it fired up an event (call the OnTimer
function) in every minute.
However, when it execute Refresh_Click(null, null) in the OnTimer
function, it gave me an exception error:

System.ArgumentException: Controls created on one thread cannot be
parented to a
control on a different thread.
at System.Windows.Forms.ControlCollection.Add(Control value)
at System.Windows.Forms.ControlCollection.Add(Control value)
..
The Refresh_Click(object sender, System.EventArgs e) works fine on it
self.

Anyone have any idea why this happened? And how can I solve this
problem?
Thanks,

Benny

*** Sent via Devdex http://www.devdex.com ***
Don't just participate in USENET...get rewarded for it!


Nov 16 '05 #4

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

Similar topics

3
by: ray | last post by:
Hi, I just wrote a windows service application to call a function of another object periodically. I used System.Server.Timer and I found that it works fine within the first 2 hours but the...
3
by: ELO | last post by:
Hi all Every week, I need to get two files on a remote server. I have developped a C# Windows Service with two System.Threading.Timer to do this task For the first one, the delay (TimeSpan...
4
by: Hagay Lupesko | last post by:
Hi, I've encountered a strange phenomena which appears to me as a bug: I have an engine that uses a System.Threading.Timer to invoke a delegate every X minutes. The code looks something...
5
by: Dave | last post by:
I trying to setup a time. The following code is in a function, Timer stateTimer = new Timer(ab(), null, 1000, 1000); I have included using System.Threading; at the begining of my code....
9
by: HL | last post by:
I am using VS 2005 Beta - C# Problem: The Timer fires a few milliseconds before the actual Due-Time Let's say a timer is created in the following manner: System.Threading.Timer m_timer = null;...
10
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...
2
by: KSC | last post by:
Hello, I have used a thread timer as in the documentation on MSDN in my VB.NET application. Using System.Threading.Interlocked.Increment I increment the counter to a certain point, perform an...
8
by: Stephen Rice | last post by:
Hi, I have a periodic problem which I am having a real time trying to sort. Background: An MDI VB app with a DB on SQL 2000. I have wrapped all the DB access into an object which spawns a...
19
by: adi | last post by:
Hi I need the following: at particular times of day, I want to make some processing. System.Windoes.Timer doesn't help much, because it exposes the Interval property. Instead, I need to tell...
8
by: =?Utf-8?B?RGF2ZSBCb29rZXI=?= | last post by:
I have a Timer that I set to go off once a day, but it frequently fails! In order to debug I would like to be able to check, at any moment, whether the Timer is enabled and when it will next...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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,...

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.