質問

I want to introduce a BackgroundWorker for time intensive tasks in my application. The BackgroundWorker is supposed to update a progress bar in my status strip any time one of this tasks is performed.

No problem with the general implementation, as long as the time intensive task is handled by a method in my form class.

But I have a class which looks like the following:

public class JobFile
{
    public string FullName { get; private set; }
    public string FileName { get; private set; }
    public string Extension { get; private set; }
    public string Path { get; private set; }
    public Icon Icon { get; private set; }
    public DateTime Date { get; private set; }
    public long Size { get; private set; }
    public string MD5Hash { get; private set; }

    ...

    public string getMD5Hash()
    {
        // Time intensive operation occurring
    }
}

I want to use a method in my JobFile object to perform the calculation, yet I want this object to communicate with my UI asynchronously, and to update its progress bar.

How can I pull this off? Do I need to use a BackgroundWorker from within my JobFile class?

These are my first steps in asynchronous events, and I'm still not that much into custom events, so please bear with me!

Thank you :)

役に立ちましたか?

解決

Please see: How to: Use a Background Worker

UI Class

public class UI
{
    private JobFile JobFile { get; set; }
    public int ProgressPercentage { get; set; }


    public void getMD5Hash()
    {
        BackgroundWorker bw = new BackgroundWorker();
        bw.WorkerReportsProgress = true;
        bw.DoWork += JobFile.bw_DoWork;
        bw.ProgressChanged += bw_ProgressChanged;
        bw.RunWorkerCompleted += bw_RunWorkerCompleted;
        bw.RunWorkerAsync();
    }


    private void bw_ProgressChanged(object sender, ProgressChangedEventArgs e)
    {
        // This would have to run on dispatcher in order to update UI
        this.ProgressPercentage = e.ProgressPercentage;
    }

    private void bw_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
    {
        // If you need to do anything opn completion
    }
}

And The JobFIle

public class JobFile
{
    public string FullName { get; private set; }
    public string FileName { get; private set; }
    public string Extension { get; private set; }
    public string Path { get; private set; }
    public DateTime Date { get; private set; }
    public long Size { get; private set; }
    public string MD5Hash { get; private set; }

    public void bw_DoWork(object sender, DoWorkEventArgs e)
    {
        BackgroundWorker worker = sender as BackgroundWorker;
        StringBuilder hash = new StringBuilder();
        for (int i = 1; (i <= 10); i++)
        {
            // Perform a time consuming operation and report progress.
            // Such as computing part of the hash.
            hash.Append(i);

            //Report progress here
            worker.ReportProgress((i * 10)); // 
        }

        MD5Hash = hash.ToString();
    }
}

Although I would highly recommend using a Task for this.

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top