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!

4
  • just use a while loop on the other thread until it receives data
    – Cole Tobin
    May 19, 2012 at 22:05
  • 2
    Starting a thread is very easy. Stopping one is incredibly hard. Focus on the stopping. May 19, 2012 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, 2012 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, 2012 at 22:23

3 Answers 3

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

1
  • 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. Mar 26, 2016 at 23:11
13
Thread th = new Thread(function1);
th.Start();
th.Abort();

void function1(){
//code here
}
6
  • 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, 2012 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, 2012 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, 2019 at 18:10
  • old guys were here PlatformNotSupportedException Thread abort is not supported on this platform. Aug 1, 2021 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. Sep 29, 2021 at 12:26
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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.