English 中文(简体)
Call BackgroundWorker.ReportProgress() by a function paraile
原标题:Call BackgroundWorker.ReportProgress() by a function parameter

我是一位无情的学者,就像其他人一样:以及为什么我试图用静态的方法在任何地方使用背景工作者。 这种方法是:

public static void RunInThread( DoWorkEventHandler method
                                    ,ProgressChangedEventHandler progressHandler
                                    ,RunWorkerCompletedEventHandler completed)
    {
        var bkw = new BackgroundWorker();
        bkw.DoWork += method;
        bkw.ProgressChanged += progressHandler;
        bkw.RunWorkerCompleted += completed;
        bkw.WorkerReportsProgress = true;
        bkw.RunWorkerAsync();
    }

but i have a problem for use the BackgroundWorker.ReportProgress method. Is there anyone here who can help me? tnx

最佳回答

如果不再重回<条码>,从<条码>中删除,请将<条码>-BackgroundWorker 类别(bkw) 退回,然后在<条码>上填写: 利用这一灵丹妙药:backgroundWorker.ReportProgress();

问题回答

虽然Haris的解决方案是可以接受的“固定装置”,但“你”与你的固定方法有重大设计缺陷;BackgroundWorker 。 类别,即:Component, 执行<>/code><<>/code><<<>/code><<>>>>>>,因此应在生命结束时处理。

改变您的格局的最佳办法是以人工方式在另一个方面援引你的工作职能。 与此相关的唯一一条是:must <>/strong>打Displer.Invoke,以便在你更新进展时与任何方面互动。 这里是你如何做的样本。

using System.Threading;

public static void RunInThread<TProgress>(
    Action<Action<TProgress>> worker,
    Action<TProgress> updateProgress,
    Action workerCompleted)
{
    Thread workerThread = new Thread(() =>
    {
        worker(updateProgress);
        workerCompleted();
    });

    workerThread.Start();
}

由于这是一种通用的方法,你可以使用这种方法报告你喜欢的任何类型的进展。 下面是你可以称之为:

RunInThread<double>(
    updateProgress =>
    {
        Thread.Sleep(500);
        updateProgress(0.5);

        Thread.Sleep(500);
        updateProgress(1);
    },
    progress =>
    {
        this.Dispatcher.Invoke(() =>
        {
            progressLabel.Text = progress.ToString();
        });
    },
    () =>
    {
        this.Dispatcher.Invoke(() =>
        {
            progressLabel.Text = "Finished!";
        });
    }
);

你们也可以非常容易地定制这一方法,以便在使用单一参数或回报值时,制造超负荷,增加方便。

public static void RunInThread<TProgress, TParameter>(
    Action<Action<TProgress>, TParameter> worker,
    Action<TProgress> updateProgress,
    Action workerCompleted,
    TParameter parameter)
{
    Thread workerThread = new Thread(() =>
    {
        worker(updateProgress, parameter);
        workerCompleted();
    });

    workerThread.Start();
}

public static void RunInThread<TProgress, TResult>(
    Func<Action<TProgress>, TResult> worker,
    Action<TProgress> updateProgress,
    Action<TResult> workerCompleted)
{
    Thread workerThread = new Thread(() =>
    {
        TResult result = worker(updateProgress);
        workerCompleted(result);
    });

    workerThread.Start();
}

public static void RunInThread<TProgress, TParameter, TResult>(
    Func<Action<TProgress>, TParameter, TResult> worker,
    Action<TProgress> updateProgress,
    Action<TResult> workerCompleted,
    TParameter parameter)
{
    Thread workerThread = new Thread(() =>
    {
        TResult result = worker(updateProgress, parameter);
        workerCompleted(result);
    });

    workerThread.Start();
}

我也建议,如果在你的工作职能范围内出现任何打击,你会增加一些处理错误的代码,但我将这样做留给你。





相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...