Hello. How can I run a thread and get an event raised when it runs for a certain amount of time?
For example: Let's say I have an encryption program. When the user presses encrypt the thread Encrypt starts. But when the thread Encrypt has been running for more than half a second or so, a "Please wait..." message is sent to the user.
Here's what I tried to do (not specifically for the scenario above, just a general solution):
Thread thread = new Thread(new ThreadStart(() =>
{
//do something...
}));
thread.Start();
System.Timers.Timer timer = new System.Timers.Timer(500);
timer.Elapsed += (object source, System.Timers.ElapsedEventArgs e) =>
{
if (thread.ThreadState == System.Threading.ThreadState.Running)
//please wait...
timer.Stop();
};
timer.Start();
The problem with the above code is that ThreadState always returns ThreadState.Stopped for no reason.
Thanks!