Imports System.Threading
Public Class Form1
'===========================方法1=========================================
Public Sub New()
InitializeComponent()
'Control.CheckForIllegalCrossThreadCalls = False '将其设置为true(默认为真),在me.close 处会报错,'设置为False,但是还是会抛出异常。
End Sub
'===========================方法1=========================================
Private firstThread As Thread
Private Sub Button1_Click(ByVal sender As System.Object,ByVal e As System.EventArgs) Handles Button1.Click
'启动calc
Dim myProcess As Process = System.Diagnostics.Process.Start("calc.exe")
'创建新线程
firstThread = New Thread(AddressOf xx)
firstThread.Start()
End Sub
Private Sub Button2_Click(ByVal sender As System.Object,ByVal e As System.EventArgs) Handles Button2.Click
writLog("xxx")
End Sub
Public Sub writLog(ByVal strParemeter)
My.Computer.FileSystem.WriteAllText("F:\1.log",strParemeter,True,System.Text.Encoding.UTF8)
End Sub
Public Sub xx()
Try
Do While 1
If My.Computer.FileSystem.FileExists("F:\1.log") Then
Thread.Sleep(1000)
'找到进程calc,并关掉
Dim findProcess As Process() = Process.GetProcessesByName("calc")
findProcess(0).Kill()
'关掉firstThread线程
'firstThread.Abort() '方法1
StartDelegate() '方法2
Exit Do
End If
Loop
Catch ex As Exception
MessageBox.Show(ex.Message)
Finally
'Me.Close '方法1
End Try
End Sub
'===========================方法2=========================================
Private Delegate Sub TestDelegate() ’这个方法本人还不是很懂,委托(不需要参数,没有返回值) 下阶段学习对象
Private Sub DelegateMethod()
firstThread.Abort()
Me.Close()
End Sub
Private Sub StartDelegate()
Me.BeginInvoke(New TestDelegate(AddressOf DelegateMethod))
End Sub
'===========================方法1=========================================
End Class