0

I'm using Excel interop and it seems that this functionality needs to run in the User Interface 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 User Interface 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();
        }
    }
2
Contributors
3
Replies
30
Views
2 Years
Discussion Span
Last Post by gusano79
0

Is there a way to have the dowork event (that invoked the progress changed event) interrupt execution until the ProgressChanged event is finished?

I think the short answer is that no, there isn't. And I wouldn't recommend it, either; using a progress reporting feature to do something other than report progress seems like a bad idea.

I recommend (if you haven't already) that you verify in detail exactly which part of the Excel PIA needs to be run in the UI thread and why. If it's not really required, you should be able to do everything in the background task. If it is, I'd want to break the background task up into parts, calling saveLedgers after the end of the first one and before spawning the next.

Edited by gusano79

0

Thanks Gusano. You're right. I could refactor so as not to need the wait. It's been suggested to me to use the manualreset event, but I haven't tried yet.

The part that needs the ui thread, I'm almost ashamed to admit, is where I use the clipboard to past a string into a worksheet. It's very fast and old habits .... :)

Edited by PerplexedB

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.