简体   繁体   English

如何转换线程代码(VB.net 到 C#)..?

[英]How to convert thread Code (VB.net to C#)..?

how to convert my VB.net code to C#..?如何将我的 VB.net 代码转换为 C#..? I want to use a looping function with a thread.我想在线程中使用循环函数。

  1. VB.Net Code VB.Net 代码

     For i As Integer = 0 To _port_count Dim worker As New Threading.Thread(AddressOf looping) worker.Start(_list(i)) commThread.Add(worker) Next

public sub looping(Byvar PortList As PortList) 'looping function public sub looping(Byvar PortList As PortList) '循环功能

  1. C# Code C# 代码

     for (int i = 0; i <= _port_count; i++) { Thread worker = new Thread(looping); worker.Start(_list[i]); commThread.Add(worker); }

public static void looping (PortList PortList) {}公共静态无效循环(PortList PortList){}

but C# code didn't work.但 C# 代码不起作用。 :( :(

thanks for your helps.感谢您的帮助。

What's declaration of looping function ?循环函数的声明是什么?

And some more info ?还有更多信息?

Here is an example from Microsoft Docs.这是 Microsoft Docs 中的一个示例。

using System;
using System.Threading;

public class Work
{
    public static void Main()
    {
        // Start a thread that calls a parameterized static method.
        Thread newThread = new Thread(Work.DoWork);
        newThread.Start(42);

        // Start a thread that calls a parameterized instance method.
        Work w = new Work();
        newThread = new Thread(w.DoMoreWork);
        newThread.Start("The answer.");
    }

    public static void DoWork(object data)
    {
        Console.WriteLine("Static thread procedure. Data='{0}'",
            data);
    }

    public void DoMoreWork(object data)
    {
        Console.WriteLine("Instance thread procedure. Data='{0}'",
            data);
    }
}
// This example displays output like the following:
//       Static thread procedure. Data='42'
//       Instance thread procedure. Data='The answer.'

link: https://msdn.microsoft.com/en-us/library/1h2f2459(v=vs.110)链接: https : //msdn.microsoft.com/en-us/library/1h2f2459(v=vs.110)

Change you looping-method signature to following:将循环方法签名更改为以下内容:

public static void looping (object PortList)

As documentation says, ParameterizedThreadStart passed as Thread constructor parameter has to be object .正如文档所说,作为 Thread 构造函数ParameterizedThreadStart传递的ParameterizedThreadStart必须是object

The Thread constructor will take either a ThreadStart delegate or a ParameterizedThreadStart delegate. Thread构造函数将采用ThreadStart委托或ParameterizedThreadStart委托。 The first has no parameters and the second has one parameter of type object .第一个没有参数,第二个有一个object类型的参数。 If you're using a named method then it has to match one of those two signatures, which yours does not.如果您使用的是命名方法,那么它必须匹配这两个签名之一,而您的则不匹配。 VB will allow you to do the wrong thing and attempt to clean up for you if it can but C# expects you to do the right thing yourself. VB 将允许您做错误的事情并尝试为您清理(如果可以),但 C# 期望您自己做正确的事情。 These days, if you want to call a method whose signature does not match, you can use a Lambda with a matching signature instead, then call your method in that:现在,如果您想调用签名不匹配的方法,您可以使用具有匹配签名的 Lambda,然后在其中调用您的方法:

Thread worker = new Thread(() => looping(_list[i]));
worker.Start();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM