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.