Vb2017多线程:随机按下按钮...... [英] Vb2017 multithreading: press buttons at random ...
问题描述
要好好学习多线程我在表单上有三个按钮,两个文本框和一个图片框,三个中的每个都做完全不同的事情(添加,减去和显示图片)。
我第一次尝试这个程序时使用了子程序:
Me.CheckForIllegalCrossThreadCalls = False我想你知道那个frase。
我可以按下任何按钮,当我想要时,适当的线程再次开始。那就是我想要的。
但是,嘿,我知道这不是我应该编程的方式!
所以现在我使用以下内容:
To learn well "Multithreading" I have three buttons, two text boxes and a picture box on a form, each of the three doing completely different things (adding, subtracting and showing pictures).
The first time I tried this programm I used the sub:
Me.CheckForIllegalCrossThreadCalls = False I think You know that frase.
I could pushed wich ever button and when I want, the appropiate thread started immidiately again. That was excactly what I want.
But hey, I've learned that is not the way I should programm!
So now I'm using the following:
If Label1.InvokeRequired Then
Me.BeginInvoke(New UpdateLabel1Delegate(AddressOf UpdateLabel1Text), message)
Else
DoSomething
End If
End Sub
三个线程通过按下适当的按钮开始。工作正常!
我可以整齐地按下按钮,但我必须等到线程准备就绪然后启动下一个线程。我不能随意按任何按钮,随时重启相关线程。
是否有人可以给我一些线索或具体提示如何做到这一点?希望有一段相关的代码。我在互联网上搜索了很多,它必须在那里,但在哪里?
提前致谢,
Eric
PS编辑代码:
The three "Threads" are started by pressing the appropriate button. Works fine!
I can press the buttons neatly, but I have to wait until a thread is ready and then start a next thread. I can NOT press any button at random and at any time to restart the relevant thread.
Is there someone who can give me a clue or specific hint how to do this? Hopefully with a piece of relevant code. I've search a lot on the internet, it must be there, but where?
Thanks in advance,
Eric
P.S. Edited code:
Imports System.Threading
Public Class Form1
Dim Number1 As Integer
Dim Number2 As Integer
Dim Thread1 As System.Threading.Thread
Dim Thread2 As System.Threading.Thread
Dim Thread3 As System.Threading.Thread
Private Delegate Sub UpdateLabel1Delegate(ByVal Number1 As Integer)
Private Delegate Sub UpdateLabel2Delegate(ByVal Number2 As Integer)
Private Delegate Sub UpdatePicturebox1Delegate()
Private Delegate Sub Button1_Clickdelegate(sender As Object, e As EventArgs)
Private Delegate Sub Button2_Clickdelegate(sender As Object, e As EventArgs)
Private Delegate Sub Button3picturebox1delegate(sender As Object, e As EventArgs)
Dim FirstTime1 As Boolean = True
Dim FirstTime2 As Boolean = True
Dim FirstTime3 As Boolean = True
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Thread1.Abort()
If Button1.InvokeRequired Then
Me.BeginInvoke(New Button1_Clickdelegate(AddressOf Button1_Click))
Else
Thread1 = New System.Threading.Thread(AddressOf CountUp)
Thread1.Start() T
End If
End Sub
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
If Button2.InvokeRequired Then
Me.BeginInvoke(New Button2_Clickdelegate(AddressOf Button2_Click))
Else
Thread2 = New System.Threading.Thread(AddressOf CountDown)
Thread2.Start()
End If
End Sub
Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click
If PictureBox1.InvokeRequired Then
Me.BeginInvoke(New Button3picturebox1delegate(AddressOf Button3_Click))
Else
Thread3 = New System.Threading.Thread(AddressOf UpdatePicturebox1)
Thread3.Start()
End If
End Sub
Private Sub CountUp()
Number1 = 0
Do Until Number1 = 1000
Number1 = Number1 + 1
UpdateLabel1Text(Number1)
Loop
End Sub 'CountUp
Private Sub CountDown()
Number2 = 1000
Do Until Number2 = 0
Number2 = Number2 - 1
UpdateLabel2Text(Number2)
Loop
End Sub 'CountDown
Private Sub UpdateLabel1Text(ByVal Number1 As Integer)
If Label1.InvokeRequired Then
Me.BeginInvoke(New UpdateLabel1Delegate(AddressOf UpdateLabel1Text), Number1)
Else
Label1.Text = Number1
Me.Refresh()
End If
End Sub
Private Sub UpdateLabel2Text(ByVal Number2 As Integer)
If Label2.InvokeRequired Then
Me.BeginInvoke(New UpdateLabel2Delegate(AddressOf UpdateLabel2Text), Number2)
Else
Label2.Text = Number2
Me.Refresh()
End If
End Sub
Private Sub UpdatePicturebox1()
Dim Aantal As Integer = 1000, x As Integer
If PictureBox1.InvokeRequired Then
Me.BeginInvoke(New UpdatePicturebox1Delegate(AddressOf UpdatePicturebox1))
Else
For x = 1 To Aantal
If PictureBox1.Visible = True Then
PictureBox1.Visible = False
'Me.Refresh()
Else
PictureBox1.Visible = True
Me.Refresh()
End If
Next 'x
End If
End Sub
End Class
我尝试过:
如果是Label1.Inv okeRequired然后
Me.BeginInvoke(New UpdateLabel1Delegate(AddressOf UpdateLabel1Text),message)
Else
DoSomething
End如果
What I have tried:
If Label1.InvokeRequired Then
Me.BeginInvoke(New UpdateLabel1Delegate(AddressOf UpdateLabel1Text), message)
Else
DoSomething
End If
推荐答案
Invoke和CheckForIllegalCrossThreadCalls仅在您尝试从原始UI线程以外的线程访问Control时才相关:如果您从一个线程中访问它们你创建,然后你应该使用Invoke - 它将代码移回原始的UI线程。这与立即启动的线程无关,而Invoke不会取代实际创建的线程!
看看你的代码(我们显然不能看到它):按下按钮应创建并启动一个线程。在线程方法中,使用Invoke访问标签/文本框/图片框。
Invoke and CheckForIllegalCrossThreadCalls are only relevant when you try to access a Control from a thread other than your original UI thread: if you access them from within a thread that you create, then you should use Invoke - which moves the code back to the original UI thread. That has nothing to do with "threads starting immediately", and Invoke does not replace the actual creating of a thread!
Look at your code (we obviously can't see it): your button press should create and start a thread. In the thread method, you use Invoke to access your labels / textboxes / picturebox.
这篇关于Vb2017多线程:随机按下按钮......的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!