C Solution to a file that is occupied with resources and cannot be deleted or modified
- 2021-10-13 08:28:42
- OfStack
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>
/// Methods executed by threads, telnet Get 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 complete! \n File location: " + localPath + "\n Whether to enter the directory ?", " Prompt ", MessageBoxButtons.YesNo) == DialogResult.Yes)
{
System.Diagnostics.Process.Start(localPath);
}
this.Close();
System.Threading.Thread.CurrentThread.Abort();// Terminate the current thread
}
catch(Exception e)
{
MessageBox.Show(this," Failed to print data! "," Prompt ");
System.Threading.Thread.CurrentThread.Abort();// Terminate the current thread
this.Close();
}
System.Threading.Thread.CurrentThread.Abort();// Terminate the current thread
}
private void button1_Click(object sender, EventArgs e)
{
//System.Threading.Thread.CurrentThread.Abort();// Terminate the current thread
if (parameterThread_t != null)
{
parameterThread_t.Interrupt();
parameterThread_t.Abort();
parameterThread_t.Join();
GC.Collect();
}
this.Close();
}
Example: Start a thread to execute an telnet program in an onshow event in the winform window. When the Abort button (button1_Click) is clicked, the thread is aborted and then continues to execute, and the file resources occupied by the current thread are released.
Key: parameterThread_t. Abort (); parameterThread_t. Join (); GC. Collect (); This means that the thread Abort () stops thread execution, Join (); The thread hangs until the thread aborts, and then GC reclaims resources.