后台工作程序类和使用来自C#中不同类的进度事件传递消息 [英] Background worker class and passing messages using progress events from a different class in c#

查看:74
本文介绍了后台工作程序类和使用来自C#中不同类的进度事件传递消息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

因此,我有一个班级,它将在新的后台工作人员中开始新的班级,而后台工作人员会使用progresschanged部分将状态消息传回.

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后台工作者类

////class2 the background worker class

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

推荐答案

感谢您的帮助,在这些答案和我发现可以成功解决的问题之间,我所缺少的内容是: BackgroundWorker worker =(BackgroundWorker)sender;

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;

,然后使用 worker.reportprogress(..)

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

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

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

完美,谢谢大家:)

这篇关于后台工作程序类和使用来自C#中不同类的进度事件传递消息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆