Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

.NET ParameterizedThreadStart wrong return type

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; 

}
like image 645
warbio Avatar asked Feb 16 '10 14:02

warbio


3 Answers

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();
like image 81
SLaks Avatar answered Sep 20 '22 02:09

SLaks


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.

like image 39
ziya Avatar answered Sep 22 '22 02:09

ziya


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
}
like image 37
Flater Avatar answered Sep 18 '22 02:09

Flater