2009-12-25 66 views
0
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 

回答

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); 
     } 
    } 
} 
0

在C#,方法调用要求括号,省略括号,你基本上是通过该方法本身。

而且如果你在C#3.5和方法_signature_匹配的委托被分配到,委托将自动为您创建(类型inferrence的帮助下)

所以,这条线:

m_NotifyMainWindow = AddressOf MainWindow.ReceiveThreadMessage 

当转换为C#,将简单变为:

m_NotifyMainWindow = MainWindow.ReceiveThreadMessage; 

只要MainWindow是一个实例,ReceiveThreadMessage是一个实例方法它是一个静态类,ReceiveThreadMessage是一个静态方法。

基本上,只要删除 AddressOf运算符,在行的末尾添加一个分号,您将获得等效的C#版本。

0

如果它只是给你问题的部分地址,你应该在一秒钟内完成。只是把它留下。 C#允许你的方法分配给委托直接使

myDelegate = AddressOf obj.SomeMethod 
在VB.NET

成为

myDelegate = obj.SomeMethod; 

更多的见解,你可以使用工具,反射器(展鹏软件),在那里你可以反编译成两C#和VB。NET

0

看一看

TOP Code Converter

如何使用代码转换器从C#到VB和VB到C# 1开关转换方向挤压 2,粘贴或在左边输入代码。要转换整个文档,请选择上传文件。 3-点击橙色按钮。转换后的代码将显示在右侧,否则将存在指向要下载的转换文件的链接。