0

In my application I used to add a lot of text in Richtextbox. It takes a lot of time, so i have added a progress bar. I need to close that as soon as GUI is updated. Is there is any to to catch after GUI is updated. I have tried with App.Current.MainWindow.IsLoaded == true, but it seems even the gui is not updated, but MainWindow.IsLoaded property is true for main window.

Herewith the code for the same.

   private void OnWorkerMethodstart()
    {
        pbw = new ProgressBarWindow();
        pbw.Dispatcher.Invoke(System.Windows.Threading.DispatcherPriority.Normal,
        new Action(
        delegate()
        {
            pbw.ShowDialog();
        }
        ));
    }

private void OnWorkerMethodStart()
    {
        ThreadStart tStart = new ThreadStart(OnWorkerMethodstart);
        t = new Thread(tStart);
        t.SetApartmentState(ApartmentState.STA);
        t.Start();
        this.OpenW();

        BackgroundWorker _BackgroundWorker1 = new BackgroundWorker();
        _BackgroundWorker1.DoWork += new DoWorkEventHandler(
        delegate(object o, DoWorkEventArgs args)
        {
            this.Dispatcher.Invoke(System.Windows.Threading.DispatcherPriority.Normal,
                new Action(
                delegate()
                {
                    DateTime da = DateTime.Now.AddMinutes(30);
                    MainWindow mw;
                    while (DateTime.Now != da)
                    {

                        mw = new MainWindow();
                        //if (mw.Editor.Workflow.Steps.Count > 1)
                        if (App.Current.MainWindow.IsLoaded == true)
                        {
                            t.Abort();
                            break;
                        }
                        else
                        {
                            //Thread.Sleep(1000);
                        }
                        mw = null;
                    }
                }
                ));
        });

        _BackgroundWorker1.RunWorkerAsync();


    }
har07
  • 88,338
  • 12
  • 84
  • 137
user958802
  • 63
  • 2
  • 7
  • Could any one plz check it, as i am running out of deadline – user958802 Dec 26 '13 at 10:54
  • App.Current.MainWindow.IsLoaded is running in your Mainwindow thread, as for the backgroundworker runs in a different thread. If you want to show where the background progress lies, you have to open it up to the mainwindow thread. Look at this post http://stackoverflow.com/questions/8785416/showing-modal-window-while-backgroundworker-runs-without-getting-sta-mta-issue – Schuere Dec 26 '13 at 11:16
  • 1
    If the only thing you do in the DoWork event is a call to Dispatcher.Invoke to do the work, it's really the same as doing the work directly on the UI thread, so you don't need a BackgroundWorker at all... – Thomas Levesque Dec 26 '13 at 12:55
  • If you're running this on the main UI thread at `DispatcherPriority.Normal`, then it should finish executing before any of the later dispatcher priorities get executed, so just run the code on the `Dispatcher` at any of the lower [DispatcherPriorities](http://msdn.microsoft.com/en-us/library/system.windows.threading.dispatcherpriority(v=vs.110).aspx). But it would probably be better to run your code on the background worker thread, and use the `RunWorkerCompleted` event to update the UI after the background call finishes. – Rachel Dec 27 '13 at 18:27

0 Answers0