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
A
回答
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
看一看
如何使用代码转换器从C#到VB和VB到C# 1开关转换方向挤压 2,粘贴或在左边输入代码。要转换整个文档,请选择上传文件。 3-点击橙色按钮。转换后的代码将显示在右侧,否则将存在指向要下载的转换文件的链接。
相关问题
- 1. 请帮我在Vb.NET
- 2. 请帮我在vb.net
- 3. 我需要帮助将VB.Net Linq转换为C#Linq
- 4. C#来VB.net转换
- 5. 帮助将c#匿名转换为vb.net
- 6. 请将下面的一段代码从python2转换为python3对我来说
- 7. 帮我处理代码转换为C#
- 8. VB.NET转换为C#
- 9. C#来VB.net字节转换
- 10. 对C#的XML帮助,请把它转换为C#
- 11. 请帮我转换到SQL LINQ
- 12. 将vb.net转换为c#
- 13. 分页兑现项目对我来说
- 14. 将C#代码转换为vb.net
- 15. 将VB.NET Lambda转换为C#
- 16. VB.NET - AddressOf返回Nothing?
- 17. 请帮我转换这个Java代码到C#
- 18. 请帮我把这个C#2.0片段转换成Linq
- 19. 转换vb.net代码转换为C#.NET
- 20. Stroustrup的C++:练习6.2.3字符类型转换对我来说不起作用
- 21. 帮我转换这行到C#
- 22. 将C#LINQ转换为VB.NET
- 23. 将c#转换为vb.net
- 24. 无法将代码转换为vb.net,请求帮助
- 25. 帮我将下面的VB/C++代码转换为C#
- 26. 请帮我将PDO从旧的mysql转换为此代码
- 27. C#VB.NET转换
- 28. 请谁能帮我这个正则表达式模式从JavaScript转换为C#
- 29. 将Linq查询从vb.net转换为c#
- 30. 需要一些帮助将VB.NET代码转换为C#