时间:2019-03-17 标签:c#Backgroundworkerclass
我想将此方法放入后台工作类中,我正在尝试但卡住了, 任何人都可以帮助我如何将此方法运行到后台工作类中:
我将此方法调用到我的 asp.net 页面中,其中文件在服务器上压缩,然后返回到客户端。但压缩文件可能需要更长的时间,并且用户会看到一个繁忙的屏幕,因此为了避免我想使用后台工作类:
[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;
}
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;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
看来您的问题是从BackgroundWorker 返回值。可以这样完成:
在工作线程的 DoWork 方法中,将
e.Result
设置为您想要返回的内容:然后,在
RunWorkerCompleted
方法中,您可以访问此主线程中的值:我假设
File
在这里是字符串,但您可以将其转换为所需的对象。为什么你在网络应用程序中需要它我不知道,但这至少是如何做到的;)
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:Then, in the
RunWorkerCompleted
method, you can access this value in the main thread: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 ;)