Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: VB.NET
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.
 
PLEASE HELP MEEEEE!!!!!!
Posted 6-Mar-11 18:01pm
tobyf93425

1 solution

Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

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

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 OriginalGriff 210
1 Richard MacCutchan 100
2 kbrandwijk 90
3 ProgramFOX 80
4 Mukesh Bhagat 65
0 Sergey Alexandrovich Kryukov 9,050
1 OriginalGriff 8,151
2 CPallini 2,613
3 Richard MacCutchan 2,221
4 Abhinav S 1,928


Advertise | Privacy | Mobile
Web03 | 2.8.140827.1 | Last Updated 7 Mar 2011
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100