这个线程代码看起来不错 [英] Does this thread code look OK

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

问题描述

第一次尝试线程。

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

现在线程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

结束班

解决方案

第一件事跳出来的是变量m_Cancelled。你在一个函数中暗淡了它的b $ b,但是尝试在另一个子函数中更改它的值。你可能想要在你的子程序和函数之外声明它,所以它可以是

所有东西都可以访问。

"只是我 < GR **** @ a-znet.com>在消息中写道

news:u1 ************** @ TK2MSFTNGP12.phx.gbl ...

尝试第一个线程时间。
这似乎挂了,我想知道它对你来说是否合适。
现在线程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 New 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发送者作为对象,ByVal e作为EventArgs)

公共子AskIfCancel()

Application.DoEvents()
System.Threading.Thread.Sleep (500)
循环

私有子ButtonCancel_Click(ByVal sender As Object,ByVal e As
System.EventArgs)处理ButtonCancel.Click
RaiseEvent取消(我,没什么)
Me.Close()
End Sub
End Class



对不起,我试图缩短代码并引入了一个bug。我有:

朋友mCancelled As Boolean


使用表单作为主题是否正确?


谢谢


" OpticTygre" <运算******** @ adelphia.net>在消息中写道

新闻:sZ ******************** @ adelphia.com ...

跳出来的第一件事是变量m_Cancelled。你在一个函数中暗淡它,但尝试在另一个子函数中更改它的值。您可能希望在子程序和函数之外声明它,以便所有内容都可以访问它。

"只是我 < GR **** @ a-znet.com>在消息中写道
新闻:u1 ************** @ TK2MSFTNGP12.phx.gbl ...

第一次尝试线程。
这似乎挂了,我想知道它对你来说是否合适。
现在线程t只是一个带有取消按钮的表单。
如果点击它会引发一个将mCancelled设置为true的事件在
主线程中。

---主线程包含
Private Sub UserCancelled(ByVal sender As Object,ByVal e As EventArgs)
mCancelled = True <结束子

私人函数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

- -T线程代码是一个表格
公共类FormCheckForCancel
继承System.Windows.Forms.Form
SNIP

公共事件取消lled(ByVal sender As Object,ByVal e As EventArgs)

Public Sub AskIfCancel()

Application.DoEvents()
System.Threading.Thread.Sleep (500)
循环

私有子ButtonCancel_Click(ByVal sender As Object,ByVal e As
System.EventArgs)处理ButtonCancel.Click
RaiseEvent取消(我,没什么)
Me.Close()
End Sub
End Class






"只是我 < GR **** @ a-znet.com>在消息中写道

news:ea *************** @ TK2MSFTNGP15.phx.gbl ...

我是对不起,我试图简化代码并引入了一个错误。我有:
朋友mCancelled As Boolean
并且没有Dim on mCancelled = False

我将其删除下面
是否真的使用了一个Form for the thread好的?

谢谢

OpticTygre <运算******** @ adelphia.net>在消息中写道
新闻:sZ ******************** @ adelphia.com ...

跳跃的第一件事在我身边是变量m_Cancelled。你在一个函数中调暗它,但是尝试在另一个子函数中改变它的值。
你可能想要在子例程和函数之外声明它,以便它可以被所有东西访问。

"只是我 < GR **** @ a-znet.com>在消息中写道
新闻:u1 ************** @ TK2MSFTNGP12.phx.gbl ...

第一次尝试线程。
这似乎挂了,我想知道它对你来说是否合适。
现在线程t只是一个带有取消按钮的表单。
如果点击它会引发一个将mCancelled设置为true的事件在
主线程中。

---主线程包含
Private Sub UserCancelled(ByVal sender As Object,ByVal e As EventArgs)
mCancelled = True <结束子

私有函数SetupAndPreview()As Boolean
SNIP
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

- t线程代码是一个表格
公共类FormCheckForCancel
继承System.Windows.Forms.Form
SNIP

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

Public Sub AskIfCancel()
做Application.DoEvents()
System.Threading.Thread.Sleep( 500)
循环

私有子ButtonCancel_Click(ByVal sender As Object,ByVal e As
System.EventArgs)处理ButtonCancel.Click <取消了RaiseEvent(我,没什么)
Me.Close()
End Sub
End Class





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

解决方案

The first thing that jumps out at me is the variable m_Cancelled. You Dim
it in one function, but try to change the value of it in another sub. You
may want to declare it outside your subroutines and functions so it can be
accessible to everything.
" Just Me" <gr****@a-znet.com> wrote in message
news:u1**************@TK2MSFTNGP12.phx.gbl...

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



I''m sorry, I tried to make the code short and introduced a bug. I do have:
Friend mCancelled As Boolean

Is it true that using a Form for the thread is OK?

Thanks

"OpticTygre" <op********@adelphia.net> wrote in message
news:sZ********************@adelphia.com...

The first thing that jumps out at me is the variable m_Cancelled. You Dim
it in one function, but try to change the value of it in another sub. You
may want to declare it outside your subroutines and functions so it can be
accessible to everything.
" Just Me" <gr****@a-znet.com> wrote in message
news:u1**************@TK2MSFTNGP12.phx.gbl...

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





" Just Me" <gr****@a-znet.com> wrote in message
news:ea***************@TK2MSFTNGP15.phx.gbl...

I''m sorry, I tried to make the code short and introduced a bug. I do have:
Friend mCancelled As Boolean and no Dim on mCancelled = False
I removed it below
Is it true that using a Form for the thread is OK?

Thanks

"OpticTygre" <op********@adelphia.net> wrote in message
news:sZ********************@adelphia.com...

The first thing that jumps out at me is the variable m_Cancelled. You
Dim it in one function, but try to change the value of it in another sub.
You may want to declare it outside your subroutines and functions so it
can be accessible to everything.
" Just Me" <gr****@a-znet.com> wrote in message
news:u1**************@TK2MSFTNGP12.phx.gbl...

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
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





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

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆