主题:请问一个关于线程的问题,谢谢.
yusiyuan
[专家分:10] 发布于 2005-12-26 12:05:00
我有一个button事件,启动一个线程调用一个函数:
private void button1_Click(...)
{
Thread thDeleteFile = new Thread(new ThreadStart(DeleteFile));
thDeleteFile.Start();
}
但是在我的DeleteFile()函数中,在执行到某一块时,需要停止该线程.怎么办呢?
在函数中没办法用thDeleteFile.abort.
回复列表 (共3个回复)
沙发
listen12345 [专家分:260] 发布于 2005-12-26 16:52:00
把DeleteFile拆分成兩個函數,分別放到兩個线程裡面,再定義一個公共全局變量,用來控制兩個函數的運行與否就可以了。
板凳
yusiyuan [专家分:10] 发布于 2005-12-26 18:51:00
能再详细一点吗?谢谢
3 楼
listen12345 [专家分:260] 发布于 2005-12-27 15:29:00
Dim i As Int16 = 0 '一個公共全局變量
Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
Dim thDeleteFile1 As New Thread(New ThreadStart(AddressOf DeleteFile1)) 'DeleteFile拆分成兩個函數
thDeleteFile1.Start()
End Sub
Private Sub DeleteFile1() 'DeleteFile拆分成兩個函數
MsgBox(1)
i = 1 '一個公共全局變量,開啟線程2
'i = 2 '一個公共全局變量,關閉線程2
If i = 2 Then
Exit Sub
End If
Dim thDeleteFile2 As New Thread(New ThreadStart(AddressOf DeleteFile2)) 'DeleteFile拆分成兩個函數
thDeleteFile2.Start()
End Sub
Private Sub DeleteFile2() 'DeleteFile拆分成兩個函數
MsgBox(2)
End Sub
Private Sub DeleteFile() 'DeleteFile拆分成兩個函數
MsgBox(1)
MsgBox(2)
End Sub
我来回复