Imports System.Threading
Public Class clsThread
'This variable will hold the Thread Index that are passed from the main window, when we created it
Private m_ThreadIndex As Integer
'This is local thread variable. We will send the value of this variable to the parent form
Private m_Counter As Integer = 0
'We will need this variable to pass argument to the method of the main window
Private m_Args(1) As Object
'This will hold the ref to the main window,
Private m_MainWindow As Form
'Here we are going to call the method ReceiveThreadMessage() of the main form.
'So the declaration of the delete should be same as ReceiveThreadMessage()
Private Delegate Sub NotifyMainWindow(ByVal ThreadIndex As Integer, ByVal Counter As Integer)
'We need an object of this deletegate
Private m_NotifyMainWindow As NotifyMainWindow
Public Sub New(ByVal ThreadIndex As Integer, ByRef MainWindow As frmtest)
m_ThreadIndex = ThreadIndex
m_MainWindow = MainWindow
'We need to point our delegate to the Method, which we want to call from this thread
m_NotifyMainWindow = AddressOf MainWindow.ReceiveThreadMessage
End Sub
Public Sub StartThread()
While True
m_Counter = m_Counter + 1
'we need to create this array such a way, that it should contains the no of elements, that we need
'to pass as the arguments.
'Here we will pass two arguments ThreadIndex and Counter, so we took the array with 2 elements.
'Now we need to place the variable to the appropriate position of this array.
'Like : Our First Argument is ThreadIndex, so we will put ThreadIndex into the first element and
'm_counter into the second element.
'Basically you have to put the variables into the array with the same sequence that is there in the
'argument list
ReDim m_Args(1)
m_Args(0) = m_ThreadIndex
m_Args(1) = m_Counter
'Call the notificaiton method on the main window by the delegate
m_MainWindow.Invoke(m_NotifyMainWindow, m_Args)
'wait for some time before continuing loop
Thread.Sleep(1000)
End While
End Sub
End Class
views:
196answers:
5If you are lazy you could use this tool http://www.developerfusion.com/tools/convert/vb-to-csharp/
Have a look at
To create delegate instances in C#, you just specify the delegate type, the method, and (if you want to create a delegate targetting a different instance or type from the current one) the target. For instance, each of these creates a ThreadStart delegate:
ThreadStart x1 = new ThreadStart(SomeInstanceMethod); ThreadStart x2 = new ThreadStart(AnotherType.SomeStaticMethod); ThreadStart x3 = new ThreadStart(someVariable.SomeInstanceMethod);
Also keep this as a reference
Complete Comparison for VB.NET and C#
This converted code should help you
using System.Threading;
using System.Windows.Forms;
public class clsThread
{
//This variable will hold the Thread Index that are passed from the main window, when we created it
private int m_ThreadIndex;
//This is local thread variable. We will send the value of this variable to the parent form
private int m_Counter = 0;
//We will need this variable to pass argument to the method of the main window
private object[] m_Args = new object[2];
//This will hold the ref to the main window,
private Form m_MainWindow;
//Here we are going to call the method ReceiveThreadMessage() of the main form.
//So the declaration of the delete should be same as ReceiveThreadMessage()
private delegate void NotifyMainWindow(int ThreadIndex, int Counter);
//We need an object of this deletegate
private NotifyMainWindow m_NotifyMainWindow;
public clsThread(int ThreadIndex, ref frmtest MainWindow)
{
m_ThreadIndex = ThreadIndex;
m_MainWindow = MainWindow;
//We need to point our delegate to the Method, which we want to call from this thread
m_NotifyMainWindow = MainWindow.ReceiveThreadMessage;
}
public void StartThread()
{
while (true)
{
m_Counter = m_Counter + 1;
//we need to create this array such a way, that it should contains the no of elements, that we need
//to pass as the arguments.
//Here we will pass two arguments ThreadIndex and Counter, so we took the array with 2 elements.
//Now we need to place the variable to the appropriate position of this array.
//Like : Our First Argument is ThreadIndex, so we will put ThreadIndex into the first element and
//m_counter into the second element.
//Basically you have to put the variables into the array with the same sequence that is there in the
//argument list
int[] m_Args = new int[2];
m_Args[0] = m_ThreadIndex;
m_Args[1] = m_Counter;
//Call the notificaiton method on the main window by the delegate
m_MainWindow.Invoke(m_NotifyMainWindow, m_Args);
//wait for some time before continuing loop
Thread.Sleep(1000);
}
}
}
Sorry Boss.. I'm getting the error in below line m_NotifyMainWindow = MainWindow.ReceiveThreadMessage;
The error message is ....Method 'test.frmTest.ReceiveThreadMessage(int, int)' referenced without parentheses
The structure for the method is given below
public void ReceiveThreadMessage(int ThreadIndex, int Counter) { ....... }
In C#, method invocation requires a parenthesis, and by omitting the parenthesis you're basically passing the method itself.
And if you're on C# 3.5 and *the method signature match the delegated being assigned to*, a delegate will be created automatically for you (with the help of type inferrence)
So, this line:
m_NotifyMainWindow = AddressOf MainWindow.ReceiveThreadMessage
When converted to C#, will simply becomes:
m_NotifyMainWindow = MainWindow.ReceiveThreadMessage;
Provided that MainWindow
is either an instance and ReceiveThreadMessage
is an instance method or it's a static class and ReceiveThreadMessage
is a static method.
Basically, just remove the AddressOf operator, add a semicolon at the end of the line and you'll get the equivalent C# version.
If it's just the adressof part thats giving you problems you should be done in a sec. Just leave it out. c# allows you to assign a method to a delegate directly so
myDelegate = AddressOf obj.SomeMethod
in VB.NET becomes
myDelegate = obj.SomeMethod;
for more insights you could use tools as Reflector (RedGate Software) where you can decompile into both c# and VB.NET