Windows Develop Bookmark and Share   
 index > Windows Forms General > Exception while trying to access a form control from Timer event!
 

Exception while trying to access a form control from Timer event!

Hi,
I have a form with a timer.. I also have a listbox that I'm trieng to access it from the elapsed event handler:
private void OnTimedEvent(object source, ElapsedEventArgs e)
{
aTimer.Stop();
//Console.Clear();
//Console.WriteLine("The Elapsed event was raised at {0}", e.SignalTime);
this.listMessages.Items.Add("The Elapsed event was raised at " + e.SignalTime);

(Note: I disabled the "static" for the method cause I can't use the reserved keyword "this" inside it!)

But I'm getting this exception:
An exception of type 'System.InvalidOperationException' occurred in System.Windows.Forms.dll but was not handled in user code Additional information: Cross-thread operation not valid: Control 'listMessages' accessed from a thread other than the thread it was created on.

What is that about?!
Thanks in advance.
Moodi_z  Wednesday, September 02, 2009 9:53 AM
Use a System.Windows.Forms timer or Invoke.
JohnWein  Wednesday, September 02, 2009 9:59 AM
Hello,
You need to post more of your code for a more exact answer. Your message says that you have performed an illegal cross-thread operation. You should access form controls only on the main thread---the thread where they were created. Accessing form controls from threads or backgroundworkers will almost always result in that cross-thread exception.

HINT: Accessing only the objects that you define wiithin the thread's scope is the best way to avoid this exception. Pass objects to the thread when you invoke it, and use the APM Callback pattern to return objects from the thread. The BackgroundWorker object has mechanisms and events already defined that seem to do a pretty solid and stable job of passing data to and from threads.


Rudedog =8^D

Mark the best replies as answers. "Fooling computers since 1971."
Rudedog2  Wednesday, September 02, 2009 4:30 PM
Hi,

According to your code, I suggest you to use System.Windows.Form.Timer control.

http://msdn.microsoft.com/en-us/library/system.windows.forms.timer.aspx

private void Form1_Load(object sender, EventArgs e)

{

timer1.Interval = 5000;

timer1.Start();

timer1.Tick +=new EventHandler(timer1_Tick);

}

private void timer1_Tick(object sender, EventArgs e)

{

this.listBox1.Items.Add(System.DateTime.Now);

}

System.Timers.Timer component is designerd for use with worker threads in a multithreader environment.

http://msdn.microsoft.com/en-us/library/system.timers.timer.aspx

Best regards,

Ling Wang


Please remember to click “Mark as Answer�on the post that helps you, and to click “Unmark as Answer�if a marked post does not actually answer your question. This can be beneficial to other community members reading the thread.
  • Marked As Answer byMoodi_z Thursday, September 10, 2009 5:20 AM
  •  
Ling Wang  Wednesday, September 09, 2009 9:14 AM
Use a System.Windows.Forms timer or Invoke.
JohnWein  Wednesday, September 02, 2009 9:59 AM
Hello,
You need to post more of your code for a more exact answer. Your message says that you have performed an illegal cross-thread operation. You should access form controls only on the main thread---the thread where they were created. Accessing form controls from threads or backgroundworkers will almost always result in that cross-thread exception.

HINT: Accessing only the objects that you define wiithin the thread's scope is the best way to avoid this exception. Pass objects to the thread when you invoke it, and use the APM Callback pattern to return objects from the thread. The BackgroundWorker object has mechanisms and events already defined that seem to do a pretty solid and stable job of passing data to and from threads.


Rudedog =8^D

Mark the best replies as answers. "Fooling computers since 1971."
Rudedog2  Wednesday, September 02, 2009 4:30 PM
Hi,

According to your code, I suggest you to use System.Windows.Form.Timer control.

http://msdn.microsoft.com/en-us/library/system.windows.forms.timer.aspx

private void Form1_Load(object sender, EventArgs e)

{

timer1.Interval = 5000;

timer1.Start();

timer1.Tick +=new EventHandler(timer1_Tick);

}

private void timer1_Tick(object sender, EventArgs e)

{

this.listBox1.Items.Add(System.DateTime.Now);

}

System.Timers.Timer component is designerd for use with worker threads in a multithreader environment.

http://msdn.microsoft.com/en-us/library/system.timers.timer.aspx

Best regards,

Ling Wang


Please remember to click “Mark as Answer�on the post that helps you, and to click “Unmark as Answer�if a marked post does not actually answer your question. This can be beneficial to other community members reading the thread.
  • Marked As Answer byMoodi_z Thursday, September 10, 2009 5:20 AM
  •  
Ling Wang  Wednesday, September 09, 2009 9:14 AM
I already did it, thank you anyway :)
Moodi_z  Thursday, September 10, 2009 5:20 AM

You can use google to search for other answers

Custom Search

More Threads

• Crystal Report 9 DOS Printing
• Using ReadFile() asynchronously
• richtext box scroller position
• Transfering data from one form to other
• This is really driving me Crazy, but the solution must be so simple
• Help - Partitions
• how to populate lable controls in a table inside FlowLayout panel?
• Layered "ImageMap" in windows forms? - Completely lost
• Subscribing and unsubscribing to the FormClosed Event
• Owner drawn control question