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!

You're definitely sure the thread is still running?

You're definitely sure the thread is still running?

Yeah, 100%.

Never mind. I've managed to solve the problem. Unfortunately, I can't post my solution as it's very specific to my program - but it was nothing more than a dumb mistake.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.