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
答案 0 :(得分:1)
看看
要在C#中创建委托实例, 你只需指定委托类型, 方法,和(如果你想创建 代表不同的代表 实例或来自当前的类型) 目标。例如,每个 这些创建了一个ThreadStart委托:
ThreadStart x1 = new ThreadStart(SomeInstanceMethod); ThreadStart x2 = new ThreadStart(AnotherType.SomeStaticMethod); ThreadStart x3 = new ThreadStart(someVariable.SomeInstanceMethod);
同时将此作为参考
Complete Comparison for VB.NET and C#
此转换后的代码可以帮助您
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);
}
}
}
答案 1 :(得分:0)
在C#中,方法调用需要括号,并且通过省略括号,您基本上都是传递方法本身。
如果您使用的是C#3.5和方法_signature_匹配委派给的委托,则会自动为您创建一个委托(借助类型推理)
所以,这一行:
m_NotifyMainWindow = AddressOf MainWindow.ReceiveThreadMessage
转换为C#时,只会变为:
m_NotifyMainWindow = MainWindow.ReceiveThreadMessage;
如果MainWindow
是实例而ReceiveThreadMessage
是实例方法或,则它是静态类,而ReceiveThreadMessage
是静态方法。
基本上,只需删除 AddressOf运算符,在该行的末尾添加一个分号,您将获得等效的C#版本。
答案 2 :(得分:0)
如果它只是给你带来问题的部分,你应该在一秒钟内完成。把它留下来吧。 c#允许您直接为委托分配方法
myDelegate = AddressOf obj.SomeMethod
VB.NET中的变为
myDelegate = obj.SomeMethod;
有关更多见解,您可以使用工具作为Reflector(RedGate软件),您可以在其中反编译为c#和VB.NET