Note that there are some explanatory texts on larger screens.

plurals
  1. POException when update a progressbar in a Background Worker
    text
    copied!<p>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.</p> <p>I tried to use a BackgroundWorker :</p> <pre><code>public partial class FenetreProgressBar : Window { private readonly BackgroundWorker worker = new BackgroundWorker(); public FenetreProgressBar(ObservableCollection&lt;Evenement.FichierJoint&gt; 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&lt;Evenement.FichierJoint&gt; collection = e.Argument as ObservableCollection&lt;Evenement.FichierJoint&gt;; //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 &gt; 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; } } </code></pre> <p>When the programme go here :</p> <pre><code>MaProgressBar.Minimum = 0; MaProgressBar.Maximum = 100; </code></pre> <p>I have an exception : " The calling thread cannot access this object because a different thread owns it".</p> <p>I have read severals answer on google and stackoverflow but i don't try to use this approach with the BackgroundWorker.</p> <p>Anyone coule help me in order to avoid this exception and solve the problem please ?</p> <p>Thanks a lot,</p> <p>Best regards :)</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload