WebJun 19, 2024 · Create a thread first and start it − // new thread Thread thread = new Thread (c.display); thread.Start (); Now display the thread and set a stop function to stop the working of the thread − public void display () { while (!flag) { Console.WriteLine ("It's Working"); Thread.Sleep (2000); } } public void Stop () { flag = true; } } Example WebJun 24, 2015 · The simplest way to synchronize the threads is to use a lock statement around all access and modification of a boolean field. The field would be contained in an object associated with the thread to be stopped. When the variable changes, you exit the thread by returning from the top-level method that is executing in the thread.
How to Terminate a Thread in C# - GeeksforGeeks
WebNov 27, 2024 · You can pause a thread by calling thread.Suspend but that is deprecated. I would take a look at autoresetevent for performing your synchronization. Share Improve … WebNov 21, 2014 · You should pass a parameter in integer values to be used by the application for stopping. Try this one, C# // thread1 is your thread, for instance thread1.Sleep ( 5000 ); // 5000ms = 5seconds This would pause the thread for 5 seconds. Posted 21-Nov-14 4:23am Afzaal Ahmad Zeeshan Updated 21-Nov-14 4:29am v2 Solution 5 bloomington in catholic church
How to Terminate a Thread in C# - Dot Net Tutorials
WebOct 4, 2024 · How to: Pause or interrupt a thread You use the Thread.Sleep method to pause the current thread for a specified amount of time. You can interrupt a blocked thread by … WebFeb 23, 2015 · Thread workerThread = new Thread (Worker.DoWork); workerThread.Start (); I interrupt the Thread like this: workerThread.Interrupt (); I stop the Thread like this: Worker.Running = false; Everything is working as expected but I'm not sure if this is how it should be implemented. Is this best practice? What can go wrong? WebMar 29, 2024 · To solve the above problem, we need to use Thread.Sleep () method, this method takes an argument in milliseconds to pause the thread. Program: /* * Program to … bloomington in breakfast places