C#解決文件被占用資源,無法刪除或修改的方法
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>
/// 線程執(zhí)行的方法,telnet獲取數(shù)據(jù)
/// </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,"數(shù)據(jù)打印完成!\n文件位置:" + localPath + "\n是否進(jìn)入該目錄?", "提示", MessageBoxButtons.YesNo) == DialogResult.Yes)
{
System.Diagnostics.Process.Start(localPath);
}
this.Close();
System.Threading.Thread.CurrentThread.Abort();//終止當(dāng)前線程
}
catch(Exception e)
{
MessageBox.Show(this,"打印數(shù)據(jù)失??!","提示");
System.Threading.Thread.CurrentThread.Abort();//終止當(dāng)前線程
this.Close();
}
System.Threading.Thread.CurrentThread.Abort();//終止當(dāng)前線程
}
private void button1_Click(object sender, EventArgs e)
{
//System.Threading.Thread.CurrentThread.Abort();//終止當(dāng)前線程
if (parameterThread_t != null)
{
parameterThread_t.Interrupt();
parameterThread_t.Abort();
parameterThread_t.Join();
GC.Collect();
}
this.Close();
}
例子:在winform窗口在onshow事件中啟動(dòng)一個(gè)線程執(zhí)行telnet程序。當(dāng)點(diǎn)擊中止按鈕(button1_Click)中止線程再繼續(xù)執(zhí)行,并且釋放當(dāng)前線程占用的文件資源。
關(guān)鍵:parameterThread_t.Abort();parameterThread_t.Join(); GC.Collect();意思是,把線程Abort()停止線程執(zhí)行,Join();線程掛起直到線程中止了,然后再GC回收資源。
以上就是C#解決文件被占用資源的全部內(nèi)容,希望能給大家一個(gè)參考,也希望大家多多支持我們。
上一篇:在WinForm中發(fā)送HTTP請求的實(shí)現(xiàn)方法
欄 目:C#教程
本文標(biāo)題:C#解決文件被占用資源,無法刪除或修改的方法
本文地址:http://mengdiqiu.com.cn/a1/C_jiaocheng/6477.html
您可能感興趣的文章
- 01-10關(guān)于ASP網(wǎng)頁無法打開的解決方案
- 01-10C#實(shí)現(xiàn)多線程下載文件的方法
- 01-10C#文件斷點(diǎn)續(xù)傳實(shí)現(xiàn)方法
- 01-10C#實(shí)現(xiàn)多線程寫入同一個(gè)文件的方法
- 01-10C#編程獲取資源文件中圖片的方法
- 01-10C#實(shí)現(xiàn)讀取被進(jìn)程占用的文件實(shí)現(xiàn)方法
- 01-10C#刪除只讀文件或文件夾(解決File.Delete無法刪除文件)
- 01-10C# readnodefile()不能讀取帶有文件名為漢字的osg文件解決方法
- 01-10C#路徑,文件,目錄及IO常見操作匯總
- 01-10winform實(shí)現(xiàn)拖動(dòng)文件到窗體上的方法


閱讀排行
本欄相關(guān)
- 01-10C#通過反射獲取當(dāng)前工程中所有窗體并
- 01-10關(guān)于ASP網(wǎng)頁無法打開的解決方案
- 01-10WinForm限制窗體不能移到屏幕外的方法
- 01-10WinForm繪制圓角的方法
- 01-10C#實(shí)現(xiàn)txt定位指定行完整實(shí)例
- 01-10WinForm實(shí)現(xiàn)仿視頻 器左下角滾動(dòng)新
- 01-10C#停止線程的方法
- 01-10C#實(shí)現(xiàn)清空回收站的方法
- 01-10C#通過重寫Panel改變邊框顏色與寬度的
- 01-10C#實(shí)現(xiàn)讀取注冊表監(jiān)控當(dāng)前操作系統(tǒng)已
隨機(jī)閱讀
- 01-11ajax實(shí)現(xiàn)頁面的局部加載
- 01-10SublimeText編譯C開發(fā)環(huán)境設(shè)置
- 04-02jquery與jsp,用jquery
- 01-11Mac OSX 打開原生自帶讀寫NTFS功能(圖文
- 08-05dedecms(織夢)副欄目數(shù)量限制代碼修改
- 08-05織夢dedecms什么時(shí)候用欄目交叉功能?
- 01-10使用C語言求解撲克牌的順子及n個(gè)骰子
- 01-10C#中split用法實(shí)例總結(jié)
- 08-05DEDE織夢data目錄下的sessions文件夾有什
- 01-10delphi制作wav文件的方法