Silverlight 中的 BackgroundWorker 问题

发布于 2024-09-08 11:12:23 字数 521 浏览 2 评论 0原文

我提前为这个问题的漫无目的性质表示歉意,但我不知道从哪里开始。

我有一个问题,我试图在 Silverlight 中打开一个 excel 文件,并对该 excel 文件进行一些处理...但是在主线程上执行此操作有两个主要问题。

  1. 打开Excel文件时,UI
  2. 因几个异步调用而冻结我发现asynccompleted回调仅在UI线程结束时才会发生(在我的情况下,当消息框打开时)

所以我试图编写一个后台工作程序来完成我的处理任务上,但我找不到任何像样的例子(它们都非常简单)。是否有有关后台工作者模式的资源?

当我尝试编写一个时,我遇到了以下问题

  1. 我无法访问类级别的变量(如果我只想为我的backgroundworker使用变量,我应该有一个单独的backgroundworker类吗?)

  2. 显示消息框,并暂停中间后台线程过于复杂(这是否意味着我应该有很多小后台工作人员?)

  3. 与 2 如何显示错误消息相关。

I apologize in advance for the rambling nature of this question, but I have no idea where to start.

I have an issue where I am trying to open an excel file in Silverlight, and do some processing on that excel file... but I have two major issues with doing this on the main thread.

  1. while opening the excel file the UI freezes
  2. with several asynchronous calls I have found that the asynccompleted callback is only happening when the UI thread ends (in my case when a messagebox opens)

So I am trying to write a background worker to do my processing tasks on, but I can't find any decent examples (they all are very simplistic). Are there any resources around for backgroundworker patterns?

When I tried to write one I came up with the following issues

  1. I could not access variables at class level (if I only want the variable for my backgroundworker should I have a separate backgroundworker class?)

  2. Displaying messageboxes, and pausing mid background thread is overly complicated (does this mean I should have lots of little background workers?)

  3. Related to 2 how to display error messages.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

ˉ厌 2024-09-15 11:12:23

对于#3,您不能直接从BackgroundWorker 线程执行UI。您需要做的是使用事件参数中的所有必要信息来触发事件,并在 UI 线程上处理该事件。

因此,对于您的消息框,您将在事件参数中使用消息触发事件

public class MessageBoxEventArgs : EventArgs
{
    public MessageBoxEventArgs(string message)
    {
        this.Message = message;
    }

    public string Message
    {
        get; private set;
    }
}

:触发事件:

public event EventHandler<MessageBoxEventArgs> Message_Event;

...


        if (this.Message_Event!= null)
        {
            this.Message_Event(this, new MessageBoxEventArgs(message));
        }

然后处理它:

    private void MessageEventHandler(object sender, MessageBoxEventArgs e)
    {
        if (this.InvokeRequired)
        {
            this.Invoke((MethodInvoker)delegate { MessageBox.Show(e.Message); });
        }
        else
        {
            MessageBox.Show(e.Message);
        }
    }

With respect to #3 you can't do UI directly from the BackgroundWorker thread. What you need to do is fire an event with all the necessary information in the event arguments and handle that event on the UI thread.

So with regard to your message boxes you'd fire and event with the message in the event arguments

public class MessageBoxEventArgs : EventArgs
{
    public MessageBoxEventArgs(string message)
    {
        this.Message = message;
    }

    public string Message
    {
        get; private set;
    }
}

Fire the event:

public event EventHandler<MessageBoxEventArgs> Message_Event;

...


        if (this.Message_Event!= null)
        {
            this.Message_Event(this, new MessageBoxEventArgs(message));
        }

Then to handle it:

    private void MessageEventHandler(object sender, MessageBoxEventArgs e)
    {
        if (this.InvokeRequired)
        {
            this.Invoke((MethodInvoker)delegate { MessageBox.Show(e.Message); });
        }
        else
        {
            MessageBox.Show(e.Message);
        }
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文