0

I created a wpf application. In my application, i open a windows and i copy in background some files. I would like to display and update a progressbar during thos copy.

I tried to use a BackgroundWorker :

public partial class FenetreProgressBar : Window
{

    private readonly BackgroundWorker worker = new BackgroundWorker();


    public FenetreProgressBar(ObservableCollection<Evenement.FichierJoint> CollectionFicJointsToAdd)
    {
        InitializeComponent();

        worker.WorkerReportsProgress = true;
        worker.DoWork += worker_DoWork;
        worker.RunWorkerCompleted += worker_RunWorkerCompleted;
        worker.ProgressChanged +=worker_ProgressChanged;

        worker.RunWorkerAsync(CollectionFicJointsToAdd);

    }

    private void ProgressChanged(double Persentage, ref bool Cancel)
    {
        if (Cancel)
            this.Close();

        worker.ReportProgress((int)Persentage);
    }

    private void Completedelegate()
    {
        this.Close();
    }

    private void worker_DoWork(object sender, DoWorkEventArgs e)
    {
        ObservableCollection<Evenement.FichierJoint> collection = e.Argument as ObservableCollection<Evenement.FichierJoint>;

        //2) On ajoute les fichiers joints à l'évènements ( ce qui va donc les copier dans le répertoire paramétré)

        foreach (Evenement.FichierJoint FichierJoint in collection)
        {
            if (FichierJoint.strPathFichier.Length > 0)
            {
                Evenement.FichierJoint monFichierJoint = new Evenement.FichierJoint(FichierJoint.strPathFichier, App.obj_myEvenement.strEvtNumeroString);

                MaProgressBar.Minimum = 0;
                MaProgressBar.Maximum = 100;

                monFichierJoint.copyObject.OnProgressChanged += ProgressChanged;
                monFichierJoint.copyObject.OnComplete += Completedelegate;

                monFichierJoint.AddFichierJoint();

            }
        }
    }

    private void worker_RunWorkerCompleted(object sender,RunWorkerCompletedEventArgs e)
    {
        // Traitement terminé, on ferme la fenetre
        this.Close();
    }

    private void worker_ProgressChanged(object sender, ProgressChangedEventArgs e)
    {
        MaProgressBar.Value = e.ProgressPercentage;
    }

}

When the programme go here :

MaProgressBar.Minimum = 0;
MaProgressBar.Maximum = 100;

I have an exception : " The calling thread cannot access this object because a different thread owns it".

I have read severals answer on google and stackoverflow but i don't try to use this approach with the BackgroundWorker.

Anyone coule help me in order to avoid this exception and solve the problem please ?

Thanks a lot,

Best regards :)

4

3 回答 3

1

You cannot modify UI objects from background worker. You need to invoke the methods on UI dispatcher like this -

App.Current.Dispatcher.Invoke((Action)delegate
            {
                MaProgressBar.Minimum = 0;
                MaProgressBar.Maximum = 100;
            });

But since you only need to set maximum an minimum values, i would suggest you to set these values outside of backgroundWorker in constructor -

public FenetreProgressBar(ObservableCollection<Evenement.FichierJoint> 
                      CollectionFicJointsToAdd)
{
    InitializeComponent();

    MaProgressBar.Minimum = 0;
    MaProgressBar.Maximum = 100;

    worker.WorkerReportsProgress = true;
    worker.DoWork += worker_DoWork;
    worker.RunWorkerCompleted += worker_RunWorkerCompleted;
    worker.ProgressChanged +=worker_ProgressChanged;

    worker.RunWorkerAsync(CollectionFicJointsToAdd);

}
于 2013-03-27T16:15:50.083 回答
0

Why don't you use C# new async and await new feature/keywords? they were specifically designed to make the UI responsive while doing lengthy operations.

于 2013-03-27T16:12:59.497 回答
0

You should try this:

_backgroundWorker.OnProgressChanged += new ProgressChangedEventHandler(backgroundWorker_ProgressChanged);

and then

// Event handler
private void backgroundWorker_ProgressChanged(object sender, ProgressChangedEventArgs e)
{
    // Update main thread here
}
于 2013-03-27T16:15:46.023 回答