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
以上是内存溢出为你收集整理的2哥学Vb.net--关于线程全部内容,希望文章能够帮你解决2哥学Vb.net--关于线程所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)