本文介绍了这个线程代码看起来不错的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

第一次尝试线程。

这似乎挂了,我想知道它对你来说是否合适。

现在线程t只是一个表单一个取消按钮。

如果点击它会引发一个事件,在主要的

线程中将mCancelled设置为true。


- - 主线程包含

Private Sub UserCancelled(ByVal sender As Object,ByVal e As EventArgs)

mCancelled = True

End Sub


私人功能SetupAndPreview()As Boolean

SNIP

Dim mCancelled = False

Dim ch As新的FormCheckForCancel

ch.Show()

Dim t As New Thread(AddressOf ch.AskIfCancel)

t.Start()

AddHandler ch.Cancelled,AddressOf UserCancelled

SNIP


- t线程代码是一个表格

公共类FormCheckForCancel

继承System.Windows.Forms.Form

SNIP


公共事件已取消(ByVal发件人) As Object,ByVal e As EventArgs)


Public Sub AskIfCancel()

Do

Application.DoEvents()

System.Threading.Thread.Sleep(500)

循环


结束子


Private Sub ButtonCancel_Click(ByVal sender As Object,ByVal e As

System.EventArgs)处理ButtonCancel.Click

RaiseEvent取消(我,没什么)

Me.Close()

End Sub

结束班

解决方案










Trying threads for the first time.
This seems to hang and I wonder if it looks OK to you.
Right now the thread t is simply a Form with a Cancel Button.
If clicked it should raise an event that sets mCancelled to true in the main
thread.

---The main thread contains
Private Sub UserCancelled(ByVal sender As Object, ByVal e As EventArgs)
mCancelled = True
End Sub

Private Function SetupAndPreview() As Boolean
SNIP
Dim mCancelled = False
Dim ch As New FormCheckForCancel
ch.Show()
Dim t As New Thread(AddressOf ch.AskIfCancel)
t.Start()
AddHandler ch.Cancelled, AddressOf UserCancelled
SNIP

--The t thread code is a Form
Public Class FormCheckForCancel
Inherits System.Windows.Forms.Form
SNIP

Public Event Cancelled(ByVal sender As Object, ByVal e As EventArgs)

Public Sub AskIfCancel()
Do
Application.DoEvents()
System.Threading.Thread.Sleep(500)
Loop

End Sub

Private Sub ButtonCancel_Click(ByVal sender As Object, ByVal e As
System.EventArgs) Handles ButtonCancel.Click
RaiseEvent Cancelled(Me, Nothing)
Me.Close()
End Sub
End Class

解决方案










这篇关于这个线程代码看起来不错的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-03 19:42
查看更多