如何让keydown事件在vb.net中的不同线程上运行 [英] How to get a keydown event to operate on a different thread in vb.net

查看:130
本文介绍了如何让keydown事件在vb.net中的不同线程上运行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在做一个12年级的学校重大项目。我希望能够做的是在两个不同的字符串上运行我的程序,一个是keydown事件,另一个是程序的其余部分。



这是目前我的代码。我已经取出了无关紧要的代码。

I am currently doing a year 12 major project for school. The thing i want to be able to do is run my program on two different strings, the keydown event on one and the rest of the program on another.

This is currently the code i have. I have taken out the code that is irrelevant.

Dim thread1 As System.Threading.Thread
Dim thread2 As System.Threading.Thread

Private Sub Form1_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles Me.KeyDown
        Select Case e.KeyCode
            Case Keys.Up
                If Not direction = SnakeDirection.Down Then
                    direction = SnakeDirection.Up
                End If
            Case Keys.Right
                If Not direction = SnakeDirection.Left Then
                    direction = SnakeDirection.Right
                End If
            Case Keys.Down
                If Not direction = SnakeDirection.Up Then
                    direction = SnakeDirection.Down
                End If
            Case Keys.Left
                If Not direction = SnakeDirection.Right Then
                    direction = SnakeDirection.Left
                End If
            Case Keys.T
                duration += 50
        End Select
    End Sub

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        thread1 = New System.Threading.Thread(AddressOf Form1_Keydown)
        thread1.Start()
    End Sub





我目前收到的错误是:



I am currently getting an error saying:

Overload resolution failed because no accessible 'New' can be called with these arguments.





请帮助MEEEEE !!!!!!



PLEASE HELP MEEEEE!!!!!!

推荐答案

你在做什么?!您的代码在一个非UI线程上使用相同的方法,并且在按钮单击时调用相同的方法并在UI线程上运行。它可能不是你需要的。



最有可能的是,你需要通过按钮点击启动非UI线程。有不同的方法可以做到这一点。你可以调用 Thread.Start 。更好的是,您可以将线程保持在某个 EventWaitHandle 的等待状态,该按钮将调用 EventWaitHandle.Set 一旦这个线程有任务(表示为某些数据),就让线程继续运行。更常规的做法是使用阻塞队列将数据提供给线程,或者使用一些代表线程任务的委托实例。有关完整代码,代码示例和更详细的说明,请参阅我的提示/技巧文章:用于线程通信和线程间调用的简单阻塞队列 [ ^ ]。



另外,你可以系统.ComponentModel.BackgroundWorker 并按下按钮启动它。对于常规线程,我建议使用我在这里提供的线程包装器:如何将ref参数传递给线程 [ ^ ]。



现在,请记住,你永远不能直接使用非UI对象-UI线程。相反,您需要使用 Invoke BeginInvoke 的方法System.Windows.Forms.Control System.Threading.Dispatcher 类( Dispatcher 可以同时使用 Forms 和WPF。详情请参阅: Control.Invoke()与Control.BeginInvoke() [ ^ ]。



基本上,调用机制获取委托实例和调用所需的所有数据,放入在队列中,稍后UI线程(在其 Application.Run 主循环中)访问此队列以在UI线程中执行实际调用。



很抱歉,很多参考资料都解释了没有代码的技术,但是一些代码示例是在C#中。请理解,以获得良好的帮助或代码示例.N你需要知道至少一些C#的ET;你无法在VB.NET中获得如此多的帮助。



你也可以使用不需要C#编程技巧的C#代码:创建一个单独的类库在你的VB.NET项目中推出它并引用它。对于.NET,语言之间的界限不存在!



-SA
What are you doing?! You code uses the same method on one non-UI thread and also the same method is to be called on button click and run on UI thread. It cannot be what you need.

Most likely, you need to start non-UI thread by the button click. There are different ways of doing this. You can call Thread.Start. Better yes, you can keep the thread in the wait state at some EventWaitHandle, and the button will call EventWaitHandle.Set to let the thread go as soon as there is a "task" for this thread (expressed as some data). More regular way of doing that is using blocking queue to feed data to the thread, or some delegate instances representing thread tasks. See my Tips/Trick article on complete code, code samples and more detailed explanation: Simple Blocking Queue for Thread Communication and Inter-thread Invocation[^].

Alternatively, you can System.ComponentModel.BackgroundWorker and start it by your button click. For a regular thread, I recommend using thread wrapper I offered here: How to pass ref parameter to the thread[^].

Now, remember, you can never use UI object directly from non-UI thread. Instead, you need to use Invoke or BeginInvoke methods of System.Windows.Forms.Control or System.Threading.Dispatcher classes (the Dispatcher can work with both Forms and WPF. See for more detail: Control.Invoke() vs. Control.BeginInvoke()[^].

Basically, invocation mechanism gets delegate instances and all data needed for a call, put in a queue and this queue is later accessed by the UI thread (in its Application.Run main cycle) to perform the actual call in UI thread.

Sorry, many references explain the techniques without code, but some code samples are in C#. Please understand, to get good help or code samples on .NET you need to know at least some C#; you cannot get so much help in VB.NET along.

You can also use available C# code the way which does not require C# programming skills: make a separate class library project out of it and reference in your VB.NET project. For .NET the boundary between languages does not exist!

—SA


这篇关于如何让keydown事件在vb.net中的不同线程上运行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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