I have a timer set for 10 seconds at one of my windows form. And for the OnTimedEvent, i set for the form to be disposed after the time has been up. However there seems to be an error of
InvalidOperationException was unhandled by user code.
Cross-thread operation not valid: Control ‘notificationForm’ accessed from a thread other than the thread it was created on.
The error was on the line
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
the code for my timer event is
private void OnTimedEvent(object source, ElapsedEventArgs e)
{
this.Dispose();
}
Anyone know how to fix this? thanks!
It looks like you are using the
System.Timers.Timer.Try using the
System.Windows.Forms.Timerinstead and subscribe to theTickevent.If you have to use that Timer, you could try changing your code to this: