.NET ParameterizedThreadStart 错误的返回类型

发布于 2024-08-21 16:54:32 字数 473 浏览 2 评论 0原文

我刚刚开始尝试线程,但遇到了一个我自己无法解决的问题。我收到错误:错误 1 ​​'bool projekt.ftp.UploadFil (object)' 的返回类型错误

我使用此代码使用方法 ftp.Uploadfile 启动线程:

Thread ftpUploadFile = new Thread(new ParameterizedThreadStart(ftp.UploadFile));
ftpUploadFile.Start(e.FullPath);

这是我使用的方法。

public static bool UploadFile(object filename)
{
    string file = Convert.ToString(filename);

    /* blah blah fricken blah snip */

    return false; 

}

I just started experimenting with Threads and I ran in to a problem I'm not able to solve on my own. I get the error: Error 1 'bool projekt.ftp.UploadFil (object)' has the wrong return type

I use this code to start a thread using the method ftp.Uploadfile:

Thread ftpUploadFile = new Thread(new ParameterizedThreadStart(ftp.UploadFile));
ftpUploadFile.Start(e.FullPath);

And this is the method I used.

public static bool UploadFile(object filename)
{
    string file = Convert.ToString(filename);

    /* blah blah fricken blah snip */

    return false; 

}

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

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

发布评论

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

评论(5

沦落红尘 2024-08-28 16:54:32

如果您阅读错误消息,您会发现问题在于该方法的返回类型错误。

具体来说,您的 UploadFile 方法返回 bool,但 ParameterizedThreadStart 委托返回 void

要解决此问题,请将 UploadFile 方法更改为返回 void,并将其所有 return xxx; 语句更改为 return;.

或者,您可以将 UploadFile 包装在匿名方法中,如下所示:

Thread ftpUploadFile = new Thread(delegate { ftp.UploadFile(e.FullPath); });
ftpUploadFile.Start();

If you read the error message, you'll see that the problem is that the method has the wrong return type.

Specifically, your UploadFile method returns bool, but the ParameterizedThreadStart delegate returns void.

To fix this, change the UploadFile method to return void, and change all of its return xxx; statements to return;.

Alternatively, you could wrap UploadFile in an anonymous method, like this:

Thread ftpUploadFile = new Thread(delegate { ftp.UploadFile(e.FullPath); });
ftpUploadFile.Start();
我乃一代侩神 2024-08-28 16:54:32

您不应该从您的方法中返回任何内容。 将返回类型设为 void - 如文档所述

public delegate void ParameterizedThreadStart(Object obj)

如果您需要了解您的方法的结果,您需要研究线程同步

You are not supposed to return anything from your method. Make the return type void - as documented:

public delegate void ParameterizedThreadStart(Object obj)

If you need to know results from your method you need to look into Thread Synchronization.

随波逐流 2024-08-28 16:54:32

像这样使用任意委托:

bool result = false;    
ThreadStart s = delegate
{
    result = UploadFile(ftp.UploadFile);
};
Thread t = new Thread(s);
s.Start();
s.Join();
// now you have your result    
if (result)
{ // do something useful
}

Use an anynomous delegate like so:

bool result = false;    
ThreadStart s = delegate
{
    result = UploadFile(ftp.UploadFile);
};
Thread t = new Thread(s);
s.Start();
s.Join();
// now you have your result    
if (result)
{ // do something useful
}
手心的温暖 2024-08-28 16:54:32

尝试

public static void UploadFile(object filename)

try

public static void UploadFile(object filename)
醉生梦死 2024-08-28 16:54:32

我认为 ParameterizedThreadStart 需要一个返回类型为 void 的方法。

I think ParameterizedThreadStart is expecting a method with a void return type.

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