Copy Code code as follows:
Thread parameterthread_t = null;
private void Print_detailform_shown (object sender, EventArgs e)
{
parameterthread_t = new Thread (new Parameterizedthreadstart (this.openthread_telnet));
Parameterthread_t.isbackground = false;
Parameterthread_t.start (NULL);
}
<summary>
The method that the thread executes, Telnet gets the data
</summary>
private void Openthread_telnet (Object obj)
{
Telnet_manage Manage = new Telnet_manage ();
Try
{
Manage. Printbysockets (NULL, wy_name, table, PROGRESSBAR1, Label2);
String LocalPath = environment.currentdirectory + @ "\ne_data\wy\" + wy_name;
If MessageBox.Show (this, "Data printing is done!") \ n File Location: "+ LocalPath +" \ n Enter the directory? "," hint ", messageboxbuttons.yesno) = = Dialogresult.yes)
{
System.Diagnostics.Process.Start (LocalPath);
}
This. Close ();
System.Threading.Thread.CurrentThread.Abort ();//Terminate current thread
}
catch (Exception e)
{
MessageBox.Show (This, "Print data failed!") "," hint ");
System.Threading.Thread.CurrentThread.Abort ();//Terminate current thread
This. Close ();
}
System.Threading.Thread.CurrentThread.Abort ();//Terminate current thread
}
private void Button1_Click (object sender, EventArgs e)
{
System.Threading.Thread.CurrentThread.Abort ();//Terminate current thread
if (parameterthread_t!= null)
{
Parameterthread_t.interrupt ();
Parameterthread_t.abort ();
Parameterthread_t.join ();
Gc. Collect ();
}
This. Close ();
}
Example: in the WinForm window, start a thread in the OnShow event to execute the Telnet program. When the Abort button (button1_click) is clicked, the thread is aborted and the file resource occupied by the current thread is freed.
Key: parameterthread_t.abort ();p arameterthread_t.join (); Gc. Collect (); This means that the thread is abort () to stop the thread from executing, Join (), the thread hangs until the thread aborts, and then the GC reclaims the resource.
The above is the C # solution files are occupied with all the content of resources, I hope to give you a reference, but also hope that we support the cloud habitat community.