BackgroundWorker 跟踪另一个类的方法

发布于 2024-11-29 00:04:01 字数 2279 浏览 0 评论 0原文

[编辑]

我用完整的代码和解释编辑了我的问题,希望能给我更清晰的解释。

我有以下类,它有一个后台工作者来跟踪循环的百分比进度并更新 ProgressWin 的 XAML 上标签上的百分比进度。下面的代码可以完美运行。 (我的问题远远低于代码之后。)

public partial class ProgressWin : Window
{
    BackgroundWorker backgroundWorker1 = new BackgroundWorker();

    public ProgressWin()
    {
        InitializeComponent();
        InitializeBackgroundWorker();
        startAsync();
    }

    // Set up the BackgroundWorker object by 
    // attaching event handlers. 
    private void InitializeBackgroundWorker()
    {
        backgroundWorker1.WorkerReportsProgress = true;
        backgroundWorker1.WorkerSupportsCancellation = true;
        backgroundWorker1.DoWork += new DoWorkEventHandler(backgroundWorker1_DoWork);
        backgroundWorker1.RunWorkerCompleted += new RunWorkerCompletedEventHandler(backgroundWorker1_RunWorkerCompleted);
        backgroundWorker1.ProgressChanged += new ProgressChangedEventHandler(backgroundWorker1_ProgressChanged);
    }

    private void startAsync()
    {
        backgroundWorker1.RunWorkerAsync();
    }

    public void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
    {
        BackgroundWorker worker = sender as BackgroundWorker;

        for (int i = 1; i <= 10; i++)
        {
            System.Threading.Thread.Sleep(500);
            worker.ReportProgress(i * 10);
        }
    }

    // This event handler updates the progress.
    public void backgroundWorker1_ProgressChanged(object sender, ProgressChangedEventArgs e)
    {
        label1.Content = (e.ProgressPercentage.ToString() + "%");
    }

    // This event handler deals with the results of the background operation.
    public void backgroundWorker1_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
    {
         resultLabel.Content = "Done!";
    }
}

我的问题来了,现在我需要跟踪 MainWindow 中的循环,而不是跟踪 ProgressWin 中的循环:

public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();

        for (int i = 1; i <= 10; i++)
        {
            System.Threading.Thread.Sleep(500);
            ????.ReportProgress(i * 10);
        }
    }
}

而且我不知道从这里到哪里去。我尝试从 ProgressWin 实例化一个对象并调用 DoWork,但最终冻结了 ProgressWin 窗口。

[EDIT]

I edited my question with complete code and explanation and hope something can give me clearer explanation.

I have the following Class that has a backgroundworker to track the percentage progress of a loop and update the percentage progress on a Label on ProgressWin's XAML. The following code works perfectly. (My question is far below, after the code.)

public partial class ProgressWin : Window
{
    BackgroundWorker backgroundWorker1 = new BackgroundWorker();

    public ProgressWin()
    {
        InitializeComponent();
        InitializeBackgroundWorker();
        startAsync();
    }

    // Set up the BackgroundWorker object by 
    // attaching event handlers. 
    private void InitializeBackgroundWorker()
    {
        backgroundWorker1.WorkerReportsProgress = true;
        backgroundWorker1.WorkerSupportsCancellation = true;
        backgroundWorker1.DoWork += new DoWorkEventHandler(backgroundWorker1_DoWork);
        backgroundWorker1.RunWorkerCompleted += new RunWorkerCompletedEventHandler(backgroundWorker1_RunWorkerCompleted);
        backgroundWorker1.ProgressChanged += new ProgressChangedEventHandler(backgroundWorker1_ProgressChanged);
    }

    private void startAsync()
    {
        backgroundWorker1.RunWorkerAsync();
    }

    public void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
    {
        BackgroundWorker worker = sender as BackgroundWorker;

        for (int i = 1; i <= 10; i++)
        {
            System.Threading.Thread.Sleep(500);
            worker.ReportProgress(i * 10);
        }
    }

    // This event handler updates the progress.
    public void backgroundWorker1_ProgressChanged(object sender, ProgressChangedEventArgs e)
    {
        label1.Content = (e.ProgressPercentage.ToString() + "%");
    }

    // This event handler deals with the results of the background operation.
    public void backgroundWorker1_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
    {
         resultLabel.Content = "Done!";
    }
}

Here comes my problem, now instead of tracking the loop within the ProgressWin, I need to track the loop in my MainWindow:

public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();

        for (int i = 1; i <= 10; i++)
        {
            System.Threading.Thread.Sleep(500);
            ????.ReportProgress(i * 10);
        }
    }
}

And I have no idea where to go from here. I tried instantiating an object from ProgressWin and call the DoWork but I end up frozen the ProgressWin window.

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

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

发布评论

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

评论(2

落日海湾 2024-12-06 00:04:01

由于问题被重写,我也重写了我的整个答案。

为了让它工作,你的MainWindow有一个 ProgressWindow 并且应该像后台工作人员一样使用它:

public partial class MainWindow : Window
{
    private ProgressWindow _Progress;

    public MainWindow()
    {
        InitializeComponent();

        _Progress = new ProgressWindow();
        _Progress.ProgressChanged += OnProgressChanged;

    }

    private void OnProgressChanged(object sender, ProjectChangedEventArgs e)
    {
        //ToDo: Update whatever you like in your MainWindow
    }
}

要完成此操作,您的 ProgressWindow 应该简单地订阅工作人员事件并重新抛出它:

public partial class ProgressWin : Window
{
    // Add this to your class above in your question
    public event ProgressChangedEventHandler ProgressChanged;

    // Change or merge this with your existing function
    private void backgroundWorker1_ProgressChanged(object sender, ProjectChangedEventArgs e)
    {
        var temp = ProgressChanged;
        if(temp !=null)
            temp(this, e);
    }
}

Due to the fact that the question was rewritten, i also rewrote my whole answer.

To get this to work your MainWindowhas a ProgressWindow and should use it like a background worker:

public partial class MainWindow : Window
{
    private ProgressWindow _Progress;

    public MainWindow()
    {
        InitializeComponent();

        _Progress = new ProgressWindow();
        _Progress.ProgressChanged += OnProgressChanged;

    }

    private void OnProgressChanged(object sender, ProjectChangedEventArgs e)
    {
        //ToDo: Update whatever you like in your MainWindow
    }
}

To accomplish this your ProgressWindow should simply subscribe to the worker event and rethrow it:

public partial class ProgressWin : Window
{
    // Add this to your class above in your question
    public event ProgressChangedEventHandler ProgressChanged;

    // Change or merge this with your existing function
    private void backgroundWorker1_ProgressChanged(object sender, ProjectChangedEventArgs e)
    {
        var temp = ProgressChanged;
        if(temp !=null)
            temp(this, e);
    }
}
ゞ花落谁相伴 2024-12-06 00:04:01

您可以通过执行

backgroudnWorker.DoWork += new DoWorkEventHandler(SomeClass.SomeStaticMethod);

backgroudnWorker.DoWork += new DoWorkEventHandler(SomeClassInstance.SomeMethod); 来简单地调用另一个类的方法>

要从另一个类 ProgressScreen 调用 MainWindow 类的方法,您应该引用 MainWindow 的实例ProgressScreen 中的类,然后使用该实例,您可以从 ProgressScreen 类调用 MainWindow 的任何公共方法

,正如奥利弗所说,您将需要以下实例backgroundworker 从其他方法发送进度更新。

You can simply call method of another class by doing

backgroudnWorker.DoWork += new DoWorkEventHandler(SomeClass.SomeStaticMethod);

or

backgroudnWorker.DoWork += new DoWorkEventHandler(SomeClassInstance.SomeMethod);

for calling a method of MainWindow class from another class ProgressScreen you should have reference of instance of MainWindow class in ProgressScreen then using that instance you can call any public method of MainWindow from ProgressScreen class

and as Oliver Said, you will need the instance of backgroundworker to send the progress updates from other method.

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