I know how to make a new thread, it looks something like this:
Dim thread as System.Threading.Thread
Then to call it, i use this code:
thread = New System.Threading.Thread(AddressOf *Function*) thread.Start()
The function is part of a Global Keyboard Hook (http://jo0ls-dotnet-stuff.blogspot.com/2008/12/vbnet-global-keyboard-hook-to-detect.html) and the purpose is to detect when a certain key is pressed and fire an event.
The function that i want to run in a separate thread is called HookCallBack, and it requires certain variables to be passed to it, here is the full code:
Option Strict On Option Explicit On Imports System.Runtime.InteropServices Public Class Form1 Private Const WH_KEYBOARD_LL As Integer = 13 Private Const WM_KEYUP As Integer = &H101 Private Const WM_SYSKEYUP As Integer = &H105 Private proc As LowLevelKeyboardProcDelegate = AddressOf HookCallback Private hookID As IntPtr Private Delegate Function LowLevelKeyboardProcDelegate(ByVal nCode As Integer, ByVal wParam As IntPtr, _ ByVal lParam As IntPtr) As IntPtr <DllImport("user32")> _ Private Shared Function SetWindowsHookEx(ByVal idHook As Integer, ByVal lpfn As LowLevelKeyboardProcDelegate, _ ByVal hMod As IntPtr, ByVal dwThreadId As UInteger) As IntPtr End Function <DllImport("user32.dll")> _ Private Shared Function UnhookWindowsHookEx(ByVal hhk As IntPtr) As <MarshalAs(UnmanagedType.Bool)> Boolean End Function <DllImport("user32.dll")> _ Private Shared Function CallNextHookEx(ByVal hhk As IntPtr, ByVal nCode As Integer, ByVal wParam As IntPtr, _ ByVal lParam As IntPtr) As IntPtr End Function <DllImport("kernel32.dll", CharSet:=CharSet.Unicode)> _ Private Shared Function GetModuleHandle(ByVal lpModuleName As String) As IntPtr End Function Sub New() ' This call is required by the Windows Form Designer. InitializeComponent() ' Add any initialization after the InitializeComponent() call. hookID = SetHook(proc) End Sub Private Sub Form1_FormClosing(ByVal sender As Object, ByVal e As FormClosingEventArgs) Handles Me.FormClosing UnhookWindowsHookEx(hookID) End Sub Private Function SetHook(ByVal proc As LowLevelKeyboardProcDelegate) As IntPtr Using curProcess As Process = Process.GetCurrentProcess() Using curModule As ProcessModule = curProcess.MainModule Return SetWindowsHookEx(WH_KEYBOARD_LL, proc, GetModuleHandle(curModule.ModuleName), 0) End Using End Using End Function Private Function HookCallback(ByVal nCode As Integer, ByVal wParam As IntPtr, ByVal lParam As IntPtr) As IntPtr ' we check keyup for standard printscreen, and syskeyup incase it is alt+printscreen ' we aren't checking keydown, as that fires before the screenshot is taken. If nCode >= 0 AndAlso (wParam.ToInt32 = WM_KEYUP OrElse wParam.ToInt32 = WM_SYSKEYUP) Then Dim vkCode As Integer = Marshal.ReadInt32(lParam) If vkCode = Keys.PrintScreen Then Dim data As IDataObject = Clipboard.GetDataObject() If data.GetDataPresent(GetType(Bitmap)) Then Me.BackgroundImage = DirectCast(data.GetData(GetType(Bitmap)), Bitmap) End If End If End If Return CallNextHookEx(hookID, nCode, wParam, lParam) End Function End Class
In this code, the Print screen Button is pressed and the function HookCallBack is called.
The problem is that the function has variables that need to be passed and returned, and while searching, i found a website that had an answer for this, and it required the function to be put in a class, and then it could be run on a separate thread while still passing all the variables needed. (http://www.devx.com/DevX/10MinuteSolution/20365/0/page/3)
But the code cannot be placed in a class without giving errors and it seems to me like i did something wrong. The code is meant to be used in a form and not a class.
My question is, how would i go about running the HookCallBack Function in a Separate thread? Any help whatsoever is greatly appreciated!
Thanks in advance!