Start Stop Seperate Thread in Button Click Event


  • Hi,

    I am a beginner in the C# Threading. I create a C# Windows forms application, and call  a void method ( function  DetailBrowserData ) with Thread.

    Please see the code below

    Thread extractionthread = null;
    extractionthread = new Thread(() => { DetailBrowserData(GetLoadText(url), url); });

    its works fine. I want to stop this thread in a button click event. Please see the code below.

    private void btnStop_Click(object sender, EventArgs e)
                    if (extractionthread != null)
                        if (extractionthread.IsAlive)
                catch (Exception ex)
                    throw ex;

    But the application is Hang.  Any fault in my code. Please Help.


    • Edited by RageshShiva Thursday, November 01, 2012 6:39 AM
    Thursday, November 01, 2012 6:38 AM


  • Does it hang at Abort or Stop?

    Usually the threads must not be stopped by Abort. The potential problems, including deadlocks, are described in documentation for this function. It also depends on what your DetailBrowserData, GetLoadText and Stop are doing. You can use an event (e.g. ManualResetEvent), which will be checked periodically in your thread to terminate the operations.

    • Marked as answer by RageshShiva Thursday, November 01, 2012 7:42 AM
    Thursday, November 01, 2012 7:34 AM