时间:2019-03-17 标签:c#Backgroundworkerclass

发布于 2024-10-16 12:45:45 字数 664 浏览 6 评论 0原文

我想将此方法放入后台工作类中,我正在尝试但卡住了, 任何人都可以帮助我如何将此方法运行到后台工作类中:
我将此方法调用到我的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

昇り龍 2024-10-23 12:45:45

看来您的问题是从BackgroundWorker 返回值。可以这样完成:

在工作线程的 DoWork 方法中,将 e.Result 设置为您想要返回的内容:

private void backgroundWorker_DoWork(object sender, DoWorkEventArgs e)
{   
    ...
    e.Result = File;
}

然后,在 RunWorkerCompleted 方法中,您可以访问此主线程中的值:

private void backgroundWorker1_RunWorkerCompleted( object sender, RunWorkerCompletedEventArgs e)
{
    string result = e.Result as string;
}

我假设 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:

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 ;)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文