C#背景工人阶级 [英] c# Background worker class
问题描述
我想将此方法放入后台工作人员类,我正在尝试但被卡住了,
有谁能帮助我如何将这种方法运行到后台工作程序类中:
我将这种方法调用到我的asp.net页面中,该页面在服务器上压缩文件,然后还原到客户端.但压缩文件可能需要更长的时间,并且用户会看到繁忙的屏幕,因此请避免使用后台工作人员类:
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;
}
推荐答案
似乎您的问题是从BackgroundWorker返回值.可以这样完成:
It seems your problem is returning the value from the BackgroundWorker. That can be done like this:
在工作人员的DoWork方法中,将e.Result
设置为要返回的内容:
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;
}
然后,在RunWorkerCompleted
方法中,您可以在主线程中访问此值:
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;
}
我假设File
在这里是字符串,但是您可以将其强制转换为所需的对象.
I have assumed that File
is string here, but you can cast it to your required object.
为什么在Web应用程序中需要它,我一无所知,但这至少是怎么做;)
Why you need it in a web application I have no clue, but this is how to do it at least ;)
这篇关于C#背景工人阶级的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!