1

I have written a C# program to call a function through backgndWorkerEnroll_DoWork which in turn calls another function through a thread. After I start the thread the first time, the function works properly. I then abort the thread using the thread.Abort() method and call thread.Start() method again. The previous function call still works, it doesn't abort. How can I force shutdown the thread?

public int capture()
{
    System.Threading.Thread thEnroll = new System.Threading.Thread(delegate()
    {
        // winbio.OpenSession1();
        refresult1 = winbio.Enroll1(finger, false, refresult);
        //winbio.Enroll1(finger, false, refresult, out refresult1);
    });

    thEnroll.Name = "thEnroll";
    thEnroll.Start();
    while (true)
    {
        if (!thEnroll.IsAlive)
            break;
    }
    thEnroll.Abort();
    return result;
}

private void backgndWorkerEnroll_DoWork(object sender, DoWorkEventArgs e)
{
    capture();
}

and also one more query.how to force close the backgroundDo_Work through button click event.

Siva
  • 259
  • 6
  • 24
  • 3
    This cannot work in general, a finger print reader will almost always be buried inside unmanaged code, waiting for the driver to spit something out. Code like that cannot be aborted, only managed code can be safely interrupted by the CLR. You will need a separate process that you can Kill(). – Hans Passant Mar 18 '13 at 12:17
  • What's the point in waiting until the thread is dead to (try to) kill it? And why are you spinlocking on an operation that could take seconds? – MattW Mar 18 '13 at 12:28
  • Highly related: [How to stop a non responsive thread?](http://stackoverflow.com/questions/11798080/how-to-stop-a-non-responsive-thread/11798203#11798203) ... Short version: `Thread.Abort` is hamfisted, and you can't really trust the state it leaves your app domain in anyway. Find another way. :P – cHao Mar 18 '13 at 12:51
  • What's more...what's the point in a separate thread if you're just going to start it, *wait til it finishes*, then continue? You could do the work on one thread and save yourself the headaches of threading. – cHao Mar 18 '13 at 13:00

2 Answers2

3

It seems like it doesn't getting to your abort action since you have infinite loop:

while (true)
{
    // This will run forever
    if (!thEnroll.IsAlive)
        break;
}

this loop blocking the thread from finishing.
also your implantation seems weird, if the thread isn't alive you don't need to abort it..

Dor Cohen
  • 16,769
  • 23
  • 93
  • 161
0

I'm not that familiar with multithreading, so corrected me if im wrong... But from what i understand is that once a Thread has been aborted it can not be started again. This might explain the behaviour you are experiencing (e.i. it works fine the first time, but not after the firt time the thread is stopped)

i googled the difference between thread.abort and thread.join (see this)

if you call Thread.Join instead, the thread will exit nicely instead of throwing an ThreadAbortedException what the site also says is that after you call abort you should also call join, or handle the ThreadAbortedException to do that.

wterbeek
  • 451
  • 9
  • 26