.NET ParameterizedThreadStart 错误的返回类型
我刚刚开始尝试线程,但遇到了一个我自己无法解决的问题。我收到错误:错误 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
如果您阅读错误消息,您会发现问题在于该方法的返回类型错误。
具体来说,您的
UploadFile
方法返回bool
,但ParameterizedThreadStart
委托返回void
。要解决此问题,请将
UploadFile
方法更改为返回void
,并将其所有return xxx;
语句更改为return;.
或者,您可以将
UploadFile
包装在匿名方法中,如下所示: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 returnsbool
, but theParameterizedThreadStart
delegate returnsvoid
.To fix this, change the
UploadFile
method to returnvoid
, and change all of itsreturn xxx;
statements toreturn;
.Alternatively, you could wrap
UploadFile
in an anonymous method, like this:您不应该从您的方法中返回任何内容。 将返回类型设为 void - 如文档所述:
如果您需要了解您的方法的结果,您需要研究线程同步。
You are not supposed to return anything from your method. Make the return type void - as documented:
If you need to know results from your method you need to look into Thread Synchronization.
像这样使用任意委托:
Use an anynomous delegate like so:
尝试
try
我认为 ParameterizedThreadStart 需要一个返回类型为 void 的方法。
I think
ParameterizedThreadStart
is expecting a method with avoid
return type.