创建参数化线程时出现问题

发布于 2024-10-19 23:25:31 字数 469 浏览 1 评论 0原文

我在尝试使用 ParameterizedThreadStart 创建线程时遇到问题。这是我现在的代码:

public class MyClass
{
    public static void Foo(int x)
    {
        ParameterizedThreadStart p = new ParameterizedThreadStart(Bar); // no overload for Bar matches delegate ParameterizedThreadStart
        Thread myThread = new Thread(p);
        myThread.Start(x);
    }

    private static void Bar(int x)
    {
        // do work
    }
}

我不太确定我做错了什么,因为我在网上找到的示例似乎在做同样的事情。

I'm having problems trying to create a thread with a ParameterizedThreadStart. Here's the code I have now:

public class MyClass
{
    public static void Foo(int x)
    {
        ParameterizedThreadStart p = new ParameterizedThreadStart(Bar); // no overload for Bar matches delegate ParameterizedThreadStart
        Thread myThread = new Thread(p);
        myThread.Start(x);
    }

    private static void Bar(int x)
    {
        // do work
    }
}

I'm not really sure what I'm doing wrong since the examples I found online appear to be doing the same thing.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

不爱素颜 2024-10-26 23:25:31

令人沮丧的是,ParameterizedThreadStart 委托类型具有接受一个 object 参数的签名。

基本上你需要做这样的事情:

// This will match ParameterizedThreadStart.
private static void Bar(object x)
{
    Bar((int)x);
}

private static void Bar(int x)
{
    // do work
}

Frustratingly, the ParameterizedThreadStart delegate type has a signature accepting one object parameter.

You'd need to do something like this, basically:

// This will match ParameterizedThreadStart.
private static void Bar(object x)
{
    Bar((int)x);
}

private static void Bar(int x)
{
    // do work
}
可是我不能没有你 2024-10-26 23:25:31

这是 ParameterizedThreadStart 的样子:

public delegate void ParameterizedThreadStart(object obj); // Accepts object

这是您的方法:

private static void Bar(int x) // Accepts int

要使其正常工作,请将您的方法更改为:

private static void Bar(object obj)
{
    int x = (int)obj;
    // todo
}

This is what ParameterizedThreadStart looks like:

public delegate void ParameterizedThreadStart(object obj); // Accepts object

Here is your method:

private static void Bar(int x) // Accepts int

To make this work, change your method to:

private static void Bar(object obj)
{
    int x = (int)obj;
    // todo
}
雨后彩虹 2024-10-26 23:25:31

它需要一个对象参数,因此您可以传递任何变量,然后您必须将其转换为您想要的类型:

private static void Bar(object o)
{
    int x = (int)o;
    // do work
}

It is expecting an object argument so you can pass any variable, then you have to cast it to the type you want:

private static void Bar(object o)
{
    int x = (int)o;
    // do work
}
放血 2024-10-26 23:25:31

您需要将 Bar 更改为

private static void Bar(object ox)
{
   int x = (int)ox;
   ...
}

您传递给 ParameterizedThreadStart 的函数需要有 1 个对象类型的单个参数。没有别的了。

You need to change Bar to

private static void Bar(object ox)
{
   int x = (int)ox;
   ...
}

The function you pass to ParameterisedThreadStart needs to have 1 single parameter of type Object. Nothing else.

西瓜 2024-10-26 23:25:31

方法 Bar 应接受 object 参数。您应该在内部强制转换为 int 。我会在这里使用 lambda 来避免创建无用的方法:

public static void Foo(int x)
{
    ParameterizedThreadStart p = new ParameterizedThreadStart(o => Bar((int)o));
    Thread myThread = new Thread(p);
    myThread.Start(x);
}

private static void Bar(int x)
{
    // do work
}

Method Bar should accept object parameter. You should cast to int inside. I would use lambdas here to avoid creating useless method:

public static void Foo(int x)
{
    ParameterizedThreadStart p = new ParameterizedThreadStart(o => Bar((int)o));
    Thread myThread = new Thread(p);
    myThread.Start(x);
}

private static void Bar(int x)
{
    // do work
}
我的鱼塘能养鲲 2024-10-26 23:25:31

Bar 参数中必须采用 object,而不是 int

private static void Bar(object x)
{ 
    // do work
}

Bar must take an object in parameter, not an int

private static void Bar(object x)
{ 
    // do work
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文