问题
I want to put this method into background worker class, i am trying but stuck,
can any one help me how to run this method into background worker class:
I am calling this method into my asp.net page, where file are zipped on server and then returend to the client. but zipping of file may take longer and user will see a busy screen, so to avoid that i want to use background worker class:
[Ajax.AjaxMethod(Ajax.HttpSessionStateRequirement.ReadWrite)]
public string Zip(string f, bool original)
{
string zip = "";
try
{
files = HttpContext.Current.Server.UrlDecode(files);
string[] fileCollection = files.Split('*');
zipFile = class1.zipfile(fileCollection, IsOriginal);
int fileLength = files.Length;
}
catch (Exception ex)
{
Console.WriteLine("Exception during processing {0}", ex);
}
return File;
}
回答1:
It seems your problem is returning the value from the BackgroundWorker. That can be done like this:
In the worker's DoWork method, set the e.Result
to what you want to return:
private void backgroundWorker_DoWork(object sender, DoWorkEventArgs e)
{
...
e.Result = File;
}
Then, in the RunWorkerCompleted
method, you can access this value in the main thread:
private void backgroundWorker1_RunWorkerCompleted( object sender, RunWorkerCompletedEventArgs e)
{
string result = e.Result as string;
}
I have assumed that File
is string here, but you can cast it to your required object.
Why you need it in a web application I have no clue, but this is how to do it at least ;)
来源:https://stackoverflow.com/questions/4931422/c-sharp-background-worker-class