Plateforme Level Extreme
Abonnement
Profil corporatif
Produits & Services
Support
Légal
English
Can a backgroundworker be told to wait?
Message
Information générale
Forum:
ASP.NET
Catégorie:
Windows Presentation Foundation (WPF)
Versions des environnements
Environment:
C# 4.0
OS:
Windows Server 2012
Network:
Windows 2008 Server
Database:
MS SQL Server
Application:
Web
Divers
Thread ID:
01614173
Message ID:
01614225
Vues:
25
To the best of my knowledge it does yes. I use Excel via interop. I think I will refactor my logic so that this situation won't hurt, or put a "wait".


>Does the saveLedgers() method need to run on the UI thread?
>
>If not then you can do something like:
   public class Test
>    {
>        public void Doit()
>        {
>            var bg36 = new BackgroundWorker();
>            bg36.DoWork += bg36_DoWork;
>            bg36.RunWorkerAsync(this);
>        }
>
>
>        public void SaveLedgers()
>        {
>        }
>
>        private  void bg36_DoWork(Object sender, DoWorkEventArgs e)
>        {
>            var t = (Test) e.Argument;
>            t.SaveLedgers();
>        }
>
>    }
>
>>I'm using Excel interop and it seems that this functionality needs to run in the UI thread.
>>So I execute that from the ProgressChanged backgroundworker event.
>>Is there a way to have the dowork event (that invoked the progress changed event) interrupt execution until the ProgressChanged event is finished?
>>
>>Here's how the bg is initialized:
>>
>>
>>       private void initializeBackgroundWorker()
>>        {
>>            bg36 = new BackgroundWorker();
>>            bg36.DoWork += bg36_DoWork;
>>            bg36.RunWorkerCompleted += bg36_RunWorkerCompleted;
>>            bg36.WorkerReportsProgress = true;
>>            bg36.ProgressChanged += bg36_ProgressChanged;
>>            bg36.WorkerSupportsCancellation = true;
>>        }
>>
>>
>>This is how the process is started:
>>
>>
>>       private void bg36_DoWork(object sender, DoWorkEventArgs e)
>>        {
>>            BackgroundWorker bw = (BackgroundWorker)sender;
>>
>>
>>This is the call to the UI thread
>>
>>
>>            bw.ReportProgress(0, "save ledgers");
>>
>>
>>And this is the routine I would like to complete before the worker continues
>>
>>
>>       private void bg36_ProgressChanged(object sender, ProgressChangedEventArgs e)
>>        {
>>            statusTextBlock.Text = (string)e.UserState + "\n\n" + Monitor.peek();
>>            if ((string)e.UserState== "save ledgers")
>>	        {
>>		         saveLedgers();
>>            }
>>        }
>>

If things have the tendency to go your way, do not worry. It won't last. Jules Renard.
Précédent
Suivant
Répondre
Fil
Voir

Click here to load this message in the networking platform