29

Can anyone give me a headstart on the topic of threading? I think I know how to do a few things but I need to know how to do the following:

Setup a main thread that will stay active until I signal it to stop(in case you wonder, it will terminate when data is received). Then i want a second thread to start which will capture data from a textbox and should quit when I signal it to that of which occurs when the user presses the enter key.

Cheers!

RHoward78
  • 53
  • 1
  • 9
8bitcat
  • 2,206
  • 5
  • 30
  • 59
  • just use a while loop on the other thread until it receives data – Cole Tobin May 19 '12 at 22:05
  • 2
    Starting a thread is very easy. Stopping one is incredibly hard. Focus on the stopping. – Hans Passant May 19 '12 at 22:17
  • 2
    You might look into using a BackgroundWorker. It has thread safe event callbacks which are quite handy if you need to manipulate form/control components. – Mike Park May 19 '12 at 22:20
  • 1
    What is the purpose of the thread?--there might be better options. If the thread is just going to be polling stuff, you might want to use a `System.Windows.Forms.Timer` instead of a new thread. – Dax Fohl May 19 '12 at 22:23

3 Answers3

24

This is how I do it...

public class ThreadA {
    public ThreadA(object[] args) {
        ...
    }
    public void Run() {
        while (true) {
            Thread.sleep(1000); // wait 1 second for something to happen.
            doStuff();
            if(conditionToExitReceived) // what im waiting for...
                break;
        }
        //perform cleanup if there is any...
    }
}

Then to run this in its own thread... ( I do it this way because I also want to send args to the thread)

private void FireThread(){
    Thread thread = new Thread(new ThreadStart(this.startThread));
    thread.start();
}
private void (startThread){
    new ThreadA(args).Run();
}

The thread is created by calling "FireThread()"

The newly created thread will run until its condition to stop is met, then it dies...

You can signal the "main" with delegates, to tell it when the thread has died.. so you can then start the second one...

Best to read through : This MSDN Article

njplumridge
  • 336
  • 3
  • 11
GHz
  • 471
  • 2
  • 8
  • 1
    Note that this approach creates the opportunity for up to a second of lag between the stuff to do showing up and actually doing the stuff. Not necessarily bad, depends on your requirements. I'm currently googling for how best to use a background worker thread using an EventWaitHandle for cross-process/thread signalling, but then I have the good luck to have control over both processes (the stuff generator and the stuff do-er). This should eliminate any polling lag. – William T. Mallard Mar 26 '16 at 23:11
12
Thread th = new Thread(function1);
th.Start();
th.Abort();

void function1(){
//code here
}
akjoshi
  • 15,374
  • 13
  • 103
  • 121
Ali Tarhini
  • 5,278
  • 6
  • 41
  • 66
  • 23
    I think `Abort()` should be a last resort call. It'd be better to set a flag and have the thread stop gracefully. – Mike Park May 19 '12 at 22:10
  • 14
    -1: I’m sorry, but `Thread.Abort` is almost _never_ the way to do it. A loop with a `ManualResetEvent` – or even a volatile boolean flag – would be a safer approach. – Douglas May 19 '12 at 22:12
  • Abort seems fine to me. It sounds scary ... like it will force it to exit, but all it does is throw an exception in the thread. If the thread doesn't catch it then it exits. Thing is usually have a general error handler around the code. So, need to catch it and handle that exception by returning. – steve Sep 23 '19 at 18:10
  • old guys were here `PlatformNotSupportedException` Thread abort is not supported on this platform. – Soner from The Ottoman Empire Aug 01 '21 at 20:28
  • Not having an abort method is a problem for threads that are dealing with blocking IO. The thread will not see any flag change until the IO operation finishes, if at all. – silicontrip Sep 29 '21 at 12:26
  • Abort has been deprecated, and will throw a PlatformNotSupportedException. – ciao1092 Dec 26 '22 at 16:45
5

Use a static AutoResetEvent in your spawned threads to call back to the main thread using the Set() method. This guy has a fairly good demo in SO on how to use it.

AutoResetEvent clarification

Community
  • 1
  • 1
MrWuf
  • 1,478
  • 1
  • 14
  • 30