后台工作者类并使用来自 c# 中不同类的进度事件传递消息

发布于 2024-08-10 05:05:00 字数 954 浏览 8 评论 0原文

因此,我有一个类在新的后台工作人员中启动一个新类,并且后台工作人员使用 Progresschanged 部分将状态消息传回。

输入来使用它时,

classname.Dataworker.reportprogress(5)

当我尝试通过从单独的类中

我收到一个错误,表明我在定义之前使用了一个对象。我发现的示例都使用单个类和其中的不同函数。

这可能是一个愚蠢的简单错误,但我就是看不到它,感谢您提供的任何帮助!

我的代码的总体概述是:

//form class

public static BackgroundWorker bw = new BackgroundWorker();

onbuttonclick
{
        installer install = new installer();
        bw.WorkerReportsProgress = true;
        bw.WorkerSupportsCancellation = true;
        bw.DoWork += class2.aFunction;
        bw.ProgressChanged += new ProgressChangedEventHandler(mainForm_InstallerEvent);
        bw.RunWorkerAsync();
}

private void mainForm_InstallerEvent(object sender, ProgressChangedEventArgs e)
{

        lbl.Text = e.UserState.ToString();
}

////class2 后台工作者类

aFunction
{
        InstallerForm.bw.ReportProgress(5); //errors on this!
}

So i have one class which starts a new class in a new background worker, and the background worker passes status messages back using the progresschanged section.

When i try and and use this by typing

classname.Dataworker.reportprogress(5)

from a seperate class i get an error that I am using an object before definition.

The examples I have found all use a single class and different functions within that.

It may be a stupid easy mistake but i just can't see it, thanks for any help you can give!

A general overview of my code is:

//form class

public static BackgroundWorker bw = new BackgroundWorker();

onbuttonclick
{
        installer install = new installer();
        bw.WorkerReportsProgress = true;
        bw.WorkerSupportsCancellation = true;
        bw.DoWork += class2.aFunction;
        bw.ProgressChanged += new ProgressChangedEventHandler(mainForm_InstallerEvent);
        bw.RunWorkerAsync();
}

private void mainForm_InstallerEvent(object sender, ProgressChangedEventArgs e)
{

        lbl.Text = e.UserState.ToString();
}

////class2 the background worker class

aFunction
{
        InstallerForm.bw.ReportProgress(5); //errors on this!
}

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

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

发布评论

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

评论(4

烟花肆意 2024-08-17 05:05:00

如果您想做某事,您需要使用 UserState 调用 ReportProgress像这样:

lbl.Text = e.UserState.ToString();

然后你的调用看起来像这样:

aFunction
{
        InstallerForm.bw.ReportProgress(5, "5% Complete");
}

现在看起来你的 e.UserState 将为 null,并且调用 ToString() 将导致空引用异常。
这里是一个 UserState 为文本的示例。

You will need to call ReportProgress with a UserState if you want to do something like this:

lbl.Text = e.UserState.ToString();

Then your call would look something like this:

aFunction
{
        InstallerForm.bw.ReportProgress(5, "5% Complete");
}

Right now it looks like your e.UserState will be null, and calling ToString() will cause a null reference exception.
Here is an example where the UserState is text.

递刀给你 2024-08-17 05:05:00

感谢您的帮助,在这些答案和我发现我已经设法使其工作的答案之间,我缺少的一行是:
后台工作人员 = (后台工作人员)发送者;

然后引用该工作对象
worker.reportprogress(..)

我发现有用的指南是: http://www .nerdparadise.com/tech/coding/csharp/backgroundworker/

完美,谢谢大家:)

Thanks for the help, between those answers and one I found I've managed to get it work, the line I was missing is:
BackgroundWorker worker = (BackgroundWorker)sender;

and then reference that worker object with
worker.reportprogress(..)

The guide i found useful is: http://www.nerdparadise.com/tech/coding/csharp/backgroundworker/

perfect, thanks guys :)

愿与i 2024-08-17 05:05:00

ReportProgress 由后台工作线程用来将百分比值传递给 Progress_Changed 委托。这篇文章向您展示了同步和异步示例。

ReportProgress is used by the background worker thread to pass a percentage value to the Progress_Changed delegate(s). This article shows you examples both synchronous and asynchronous.

笙痞 2024-08-17 05:05:00

到目前为止,这是我的懒惰解决方法(因为我不想使用额外的事件处理程序)。当时我也不想理解 userstate ;)因此我使用了一个包含所有警报/消息的列表来执行特定的长时间操作。消息字符串保存在应用程序内强大的资源存储库App.Properties.Settings中。由于 ReportProgress 采用整数,因此我将 ReportProgress 中的列表索引发送到 Progress_Changed

例子:
do_work 中调用了以下方法。

 private void LongOperation()
 {
        try
        {
          //the operation
          if (success){
             //write a message to a status label
             bgWorker.ReportProgress(1);
          }
          else{
             //write a message to a status label
             bgWorker.ReportProgress(2); 
          }              
        }
        catch(){...}
  }

  public void bgWorker_ProgressChanged(object sender, ProgressChangedEventArgs p)
  {
      int lstIndex = p.ProgressPercentage;
      lblStatus.Text = mssglist[lstIndex].ToString(); 
  }

This was my lazy workaround (because I didn't want to use an extra event handler) so far. At the that time I also didn't want to understand the userstate ;) So I used a list with all the alerts/messages for a particular long operation. Message strings were saved in App.Properties.Settings, the mighty resource repository within the app. Since ReportProgress is taking an integer, I send list index within ReportProgress to Progress_Changed.

example:
Following method was called in do_work.

 private void LongOperation()
 {
        try
        {
          //the operation
          if (success){
             //write a message to a status label
             bgWorker.ReportProgress(1);
          }
          else{
             //write a message to a status label
             bgWorker.ReportProgress(2); 
          }              
        }
        catch(){...}
  }

  public void bgWorker_ProgressChanged(object sender, ProgressChangedEventArgs p)
  {
      int lstIndex = p.ProgressPercentage;
      lblStatus.Text = mssglist[lstIndex].ToString(); 
  }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文