C # kills a thread

In my application, I have a thread that runs continuously. Using Thread.Sleep (), the function runs every 10 minutes.

I need to be able to kill this thread when the user clicks a button. I know that Thread.Abort () is not reliable. I can use a variable to stop the thread, but since it is sleeping, it may be 10 minutes before the thread destroys itself.

Any ideas?

+4
source share
5 answers

Why don't you use a timer to schedule a task every ten minutes. This will run your code in the thread pool thread, and therefore you will not have to manage this yourself.

See the System.Threading.Timer class for more details.

+9
source

Instead of Thread.Sleep use System.Threading.ManualResetEvent . The WaitOne method has a timeout just like Thread.Sleep , your thread will sleep during this interval, unless the event is triggered first, and the return value indicates whether the interval or event was set.

+4
source

So, here is an example for which timer users do the work suggested by Brian. Use start / stop if necessary. To clear an object (Program) as soon as you are done with it, make sure you call Dispose.

Just remember that when you call Stop, this will prevent the timer from starting again, however you can still have a worker thread in the middle of the timer_Elapsed handler, i.e. stopping the timer does not stop the execution of the current workflow.

 using System; using System.Timers; namespace TimerApp { class Program : IDisposable { private Timer timer; public Program() { this.timer = new Timer(); this.timer.Elapsed += new ElapsedEventHandler(timer_Elapsed); this.timer.AutoReset = true; this.timer.Interval = TimeSpan.FromMinutes(10).TotalMilliseconds; } void timer_Elapsed(object sender, ElapsedEventArgs e) { // TODO...your periodic processing, executed in a worker thread. } static void Main(string[] args) { // TODO...your app logic. } public void Start() { this.timer.Start(); } public void Stop() { this.timer.Stop(); } public void Dispose() { this.timer.Dispose(); } } } 
+4
source

Building Ben's answer, here is a sample that will help you ...

 using System.Threading; public class MyWorker { private ManualResetEvent mResetEvent = new ManualResetEvent(false); private volatile bool mIsAlive; private const int mTimeout = 6000000; public void Start() { if (mIsAlive == false) { mIsAlive = true; Thread thread = new Thread(new ThreadStart(RunThread)); thread.Start(); } } public void Stop() { mIsAlive = false; mResetEvent.Set(); } public void RunThread() { while(mIsAlive) { //Reset the event -we may be restarting the thread. mResetEvent.Reset(); DoWork(); //The thread will block on this until either the timeout //expires or the reset event is signaled. if (mResetEvent.WaitOne(mTimeout)) { mIsAlive = false; // Exit the loop. } } } public void DoWork() { //... } } 
+3
source

One option is to stay awake for ten minutes. Let him sleep for 10 seconds, and then do his work only every sixty times. Then you will only have latency ten seconds before the stop.

Also: this is not necessarily the best solution, but it is probably the quickest to implement. As with all possibilities, you must do a cost-benefit analysis when choosing the solution that is right for you.

If there are still too many ten seconds, you can push them further, although keep in mind that too low a return will lead to a possible performance impact.

You are right that you should not kill threads from the outside, this is usually a recipe for disaster if you happen to do this when they have a lock on some resource that is not released during the kill. Threads should always be responsible for their own resources, including their lifetime.

+2
source

Source: https://habr.com/ru/post/1316491/


All Articles