0

I have button call this code

private void but_Click(object sender, EventArgs e)
{
    Thread My_Thread = new Thread(() => Send_File());
    My_Thread.IsBackground = true;
    My_Thread.Start();
}

I want a way to kill

My_Thread

from the function

Send_File()

please help me how to fix it ??? :(

motaz99
  • 471
  • 3
  • 8
  • 15
  • Why do you want to kill the thread? There is no guarantee that `Thread.Abort()` will do what you want, when you want it to. – Bryan Crosby Aug 14 '12 at 21:46
  • 3
    Use a BackgroundWorker (because it takes care of the details and makes life much easier in WebForms) and give it cancellation support? It is generally best if a thread "knows how to end itself" .. –  Aug 14 '12 at 21:46
  • @Bryan Crosby I have a chat program with a file transfer and I have a form with progressBar when close this form I want to cancel the thread – motaz99 Aug 14 '12 at 21:50
  • So you don't want to cancel from `Send_File`, you want to cancel from somewhere else? – Austin Salonen Aug 14 '12 at 21:55
  • @ Austin Salonen that I use new form in Send_File in other word in another class – motaz99 Aug 14 '12 at 22:05

4 Answers4

1

Just declare your thread globally like any other variable (eg. int or string) you are using in different functions:

Thread My_Thread; //goes before your functions/main method

and then use it:

private void but_Click(object sender, EventArgs e)
{
    My_Thread = new Thread(Send_File);
    My_Thread.IsBackground = true;
    My_Thread.Start();
}

and kill it:

private void Send_File()
{
    MyThread.Abort();
}

If you are talking about Send_File running in the thread, just exit it for example using break, stop all loops to complete it.

EDIT: As Austin Salonen has stated in his comment this would overwrite the thread reference. My suggestion would be using a thread list.

public List<Thread> ThreadList=new List<Thread>(); //goes before your functions/main method (public for use in other classes)

and use it:

private void but_Click(object sender, EventArgs e)
{
    Thread My_Thread = new Thread(Send_File);
    My_Thread.IsBackground = true;
    My_Thread.Start();
    int ThreadIndex = ThreadList.Count; //remember index
    ThreadList.Add(My_Thread);
}

You just need to remember the index of the list to create a reference to the thread again.

To abort a thread just use its index:

ThreadList[ThreadIndex].Abort();
ThreadList[ThreadIndex] = null;

or just let the thread return.

1' OR 1 --
  • 1,694
  • 1
  • 16
  • 32
0

Define the Thread at the class level:

public class Whatever
{
    Thread My_Thread;

    private void but_Click(object sender, EventArgs e)
    {
        My_Thread = new Thread(() => Send_File());
        //...
    }

    private void Send_File()
    {
        My_Thread.Abort()  //though I can never ever ever encourage doing this
        //...
    }
}

Or just return. When a Thread's work method returns, it is killed.

Phillip Schmidt
  • 8,805
  • 3
  • 43
  • 67
  • What happens when but_Click is invoked multiple times? – Austin Salonen Aug 14 '12 at 21:50
  • I can't I have 2 reason the first if press but 2 time and the second reason I use new form in Send_File in other word My_Thread.Abort() in another class – motaz99 Aug 14 '12 at 22:00
  • @AustinSalonen good point. Hadn't thought of that when I wrote this up. Note that this isn't the way I'd do any of this, so maybe I'm not the best advice-giver on this. – Phillip Schmidt Aug 14 '12 at 22:21
  • @motaz99 then you need to just let the thread return. There is almost never a reason to explicitly abort a thread – Phillip Schmidt Aug 14 '12 at 22:22
0

I'd highly recommend you not use Thread directly if you need to abort what it's doing. I'd recommend a Task and using CancellationTokenSource to communicate cancellation requests. If you need to communicate with the UI, e.g. progress, I'd recommend a BackgroundWorker. If you must use Thread, then you need to inform the user to abort. You can do this by using a shared boolean value that the thread periodically checks to see if it should continue. You should read/write the value in a thread-safe way. Maybe Interlocked.Exchange would do that for you or Thread.VolatileRead and Thread.VolatileWrite...

When you use Thread.Abort it simply stops the thread unless the thread tries to catch the ThreadAbortException. It's a bit iffy when you start using exceptions for normal logic flow; but, it's doable. There is a potential for deadlock with Thread.Abort in the context of try/catch/finally blocks. (and any other constrained region) But, Thread.Abort is not all that recommended: http://haacked.com/archive/2004/11/12/how-to-stop-a-thread.aspx

Peter Ritchie
  • 35,463
  • 9
  • 80
  • 98
0

Thread.Abort() is the one you're looking for.

References & useful pages:

eldarerathis
  • 35,455
  • 10
  • 90
  • 93