Starting intrupped thread again..
-
Hi, I need to design a program should be able to update certain textbox for each event inside the GUI and after every 10 seconds of most recent event, text tox should be back to original value..Let say
Event one occurs --> text box updated --> after 10 seconds --> text box back to normal
This is the case in perfect world, but it might be possible that someone create another GUI event during this period of 10 seconds..Event one occurs --> text box updated --> after 8 seconds --> event two occurs --> GUI updated --> after 2 seconds the other thread (10 seconds completed for first event), text box back to normal.
So in this case, for second event, the textbox get back to original value in just 2 seconds. I know I could use thread intrupt to cancel the other thread. But my question is, it the best way of doing it?? And what is there any other way to starting intrupped thread again, without making a new thread? Will thread intrupt make sure everything will go perfect?? Would you suggest some more efficient way? I may be misunderstanding about threading here, please forgive that. Any suggestions, links or comments will be appreciated. PS So far my code is like this..Thread t = new Thread(keep_run);
public void update()
{
t.Interrupt();
if (MessageLbl.InvokeRequired)
{
MessageLbl.Invoke(new setText(() => MessageLbl.Text = "User settings have been updated."));
}
else
{
MessageLbl.Text = "User settings have been updated.";
}t = new Thread(keep\_run); t.Start();
}
private void keep_run()
{
try
{
Thread.Sleep(10000);
MessageLbl.Invoke(new setText(() => MessageLbl.Text = "Welcome to MAT Downloader"));
}
catch { MessageBox.Show("Interrupted"); }
}
}If you find any other problems with the code then do mention them. Thanks Regards, Shivam Kalra :)
-
Hi, I need to design a program should be able to update certain textbox for each event inside the GUI and after every 10 seconds of most recent event, text tox should be back to original value..Let say
Event one occurs --> text box updated --> after 10 seconds --> text box back to normal
This is the case in perfect world, but it might be possible that someone create another GUI event during this period of 10 seconds..Event one occurs --> text box updated --> after 8 seconds --> event two occurs --> GUI updated --> after 2 seconds the other thread (10 seconds completed for first event), text box back to normal.
So in this case, for second event, the textbox get back to original value in just 2 seconds. I know I could use thread intrupt to cancel the other thread. But my question is, it the best way of doing it?? And what is there any other way to starting intrupped thread again, without making a new thread? Will thread intrupt make sure everything will go perfect?? Would you suggest some more efficient way? I may be misunderstanding about threading here, please forgive that. Any suggestions, links or comments will be appreciated. PS So far my code is like this..Thread t = new Thread(keep_run);
public void update()
{
t.Interrupt();
if (MessageLbl.InvokeRequired)
{
MessageLbl.Invoke(new setText(() => MessageLbl.Text = "User settings have been updated."));
}
else
{
MessageLbl.Text = "User settings have been updated.";
}t = new Thread(keep\_run); t.Start();
}
private void keep_run()
{
try
{
Thread.Sleep(10000);
MessageLbl.Invoke(new setText(() => MessageLbl.Text = "Welcome to MAT Downloader"));
}
catch { MessageBox.Show("Interrupted"); }
}
}If you find any other problems with the code then do mention them. Thanks Regards, Shivam Kalra :)
Is it really necessary to resort to threads here? And if the answer is yes, then more specifically, is it really necessary to resort to multiple GUI-altering threads? Why not just make the main (GUI) thread have a method that updates the text box and a Timer control that triggers after 10 seconds to reset the contents of the text box. When a new text-event occurs, your method is called again, and you reset the timer to 10 seconds again. Something in the way of (incomplete code):
void AddNotification(string NotifyText)
{
// Note: depending on if you're sticking to multiple threads,
// you might actually need to use Invokes here. Possibly you could
// force the caller to do the Invoke and throwing an exception if
// the caller didn't
Timer1.Enabled = false;
MessageLbl.Text = NotifyText;
Timer1.Enabled = true;
}// Timer event function (can't remember at the top my head how C# names it exactly)
void Timer1_Timer(object sender)
{
Timer1.Enabled = false;
MessageLbl.Text = "Default text here";
}With Timer1's timeout set to 10 seconds. If your GUI thread is doing some intensive work, then the timer might not fire until after the work is done. If that is a problem, thát would be the time to use threads, to do the work in a background thread while the GUI remains responsive. All in all, it's best to have the main thread handle all GUI.