0

I'm a beginner in C#. I'm making a Window Form App with thread A

Because I want this thread to work continuously, I push a while loop into thread A. So, How can I stop this thread with a button? Are there any guys who have a solution for me?

  • 1
    The typical way is to have a concurrentQueue in a blockingcollection and iterate over this with [GetConsumableEnumerable](https://learn.microsoft.com/en-us/dotnet/api/system.collections.concurrent.blockingcollection-1.getconsumingenumerable?view=net-5.0). Whatever needs processing is put on the queue, and the queue will block while the queue is empty. 'Interupt' is a low level concept that is rarely used outside of system level programming. – JonasH Jul 23 '21 at 06:41
  • @JonasH: a) `BlockingCollection` doesn't offer anything specific with respect to responding to user input and interrupting a worker thread that is performing some on-going work (as opposed to _consuming_ the result of some other _producer_, which is what `BlockingCollection` is good for), and b) the word "interrupt" as used here is _very_ common, not low-level at all, and is a perfectly fine way to describe the desired goal of _interrupting_ some work that is going on in one's code. – Peter Duniho Jul 23 '21 at 06:55
  • Your question has nothing to do with interrupts, imo. – TaW Jul 23 '21 at 07:17
  • 1
    @Peter Duniho I disagree that *interrupt* is a common term in c#, since it implies preemptive interrupting, or at least it does to me . As far as I know the only way to preempt a thread is Thread.Abort, and that is not supported in later .net versions. If you want to delegate work, some kind of message queue is a usual method, if you want to abort, a CancellationToken is the usual way. But these methods are *cooperative* and not preemptive. – JonasH Jul 23 '21 at 08:54
  • @JonasH: all due respect, you are being _way_ too pedantic about _your_ interpretation of the word "interrupt". It is quite clear what the author of the post means, and there's _nothing_ in it that demands that the interruption be unliteral instead of cooperative. If you are in the middle of a task and someone comes up and asks you to stop, you are still **interrupted**. It's only unilateral (or preemptive) if the person forcibly removes you from the task, and yet in the English language we still call that an "interruption" or "interrupting". – Peter Duniho Jul 23 '21 at 16:52
  • 1
    'Interrupt' is too overloaded. Unqualified, the term should refer to a hardware signal that forces a jump/call from the next instruction, as specified by the PC, to an interrupt-handler. The other overloads, eg the OS defn as a kernel entry from hardware signal or syscall, need to be further qualified when used. – Martin James Jul 24 '21 at 10:03
  • @JonasH - `Thread.Abort()` was barely supported in the framework when it was available. It was only useful when trying to forcibly exit an app. In other cases it can leave the remaining threads, and the run-time itself, in an invalid state. – Enigmativity Jul 25 '21 at 02:01
  • @Cong - Please don't use a tight `while` loop to keep a thread alive. Use a `ConcurrentQueue` and have either an explicit call or a repeating timer that drains the queue when it's not empty. If you use a timer then the way to "stop the thread" is just to stop the timer. – Enigmativity Jul 25 '21 at 02:04

0 Answers0