开发者

Run a modal dialog on a non-UI thread

开发者 https://www.devze.com 2022-12-10 16:34 出处:网络
I\'m writing a simple data UI using standard .Net databinding to a typed DataSet from SQL Server. I have a reload button which calls Fill on all of the DataAdapters to get new data from the database

I'm writing a simple data UI using standard .Net databinding to a typed DataSet from SQL Server.

I have a reload button which calls Fill on all of the DataAdapters to get new data from the database (in case another user changed the data).

This takes some time, during which the UI is frozen. It must be run on the UI thread or the databinding event handlers throw cross-thread exceptions.

I'd like to show a modal "Please Wait" dialog on a background thread (so that it can be animated) while the UI thread connects to the database.

How can I show a modal dialog box on the non-UI thread?


EDIT: I'm aware that best practice is to run the operation in the background, but I can't do that because of开发者_运维知识库 the databinding events.


You should do the opposite. Run your long-running process on a background thread and leave the UI thread free to respond to the user actions.

If you want to block any user actions while it is processing you have a number of options, including modal dialogs. Once the background thread completes processing you can inform the main thread about the outcome


The code running in the databinding events need to be decoupled from the UI, probably using some kind of data transfer object.

Then you can run the query operation in a separate thread or a BackgroundWorker, and leave the UI thread as it was.

Edit: The really quick way to fix this is to get the events to run in their own delegate using InvokeRequired and .Invoke. That will give the methods UI context. My co-worker does this like it's going out of style and it annoys me to no end because it's rarely a good idea to do it this way... but if you want a fast solution this will work. (I'm not at work so I don't have a sample with me; I'll try to come up with something.)

Edit 2: I'm not sure what you're asking for is possible. I made a sample app that created a modal dialog in another thread, and it ends up being modeless. Instead of using a modal dialog, could you use some other control or set of controls to indicate progress change, most likely directly on the same form?


using( var frmDialog = new MyPleasWaitDialog() ) {
    // data loading is started after the form is shown
    frmDialog.Load += (_sender, _e) {
        // load data in separate thread
        ThreadPool.QueueWorkItem( (_state)=> {
            myAdapter.Fill( myDataSet );
            // refresh UI components in correct (UI) thread
            frmDialog.Invoke( (Action)myDataControl.Refresh );
            // close dialog
            frmDialog.Invoke( (Action)frmDialog.Close() );
        }
    }

    // shows dialog
    frmDialog.ShowDialog( this );
}


Here is an example of using BackgroundWorker to do the loading of data and running a user friendly form to show 'Loading records' or similar...

    public void Run()
    {
        bgWorkrFillDS = new BackgroundWorker();
        bgWorkrFillDS.RunWorkerCompleted += new RunWorkerCompletedEventHandler(bgWorkrFillDS_RunWorkerCompleted);
        bgWorkrFillDS.DoWork += new DoWorkEventHandler(bgWorkrFillDS_DoWork);
        bgWorkrFillDS.RunWorkerAsync();
    }

    void bgWorkrFillDS_DoWork(object sender, DoWorkEventArgs e)
    {
        BackgroundWorker bgWrkrFillDS = (BackgroundWorker)sender as BackgroundWorker;
        if (bgWrkrFillDS != null)
        {
            // Load up the form that shows a 'Loading....'
            // Here we fill in the DS
            // someDataSetAdapter.Fill(myDataSet);
        }
    }


    void bgWorkrFillDS_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
    {
        // Hide or unload the form when the work is done
    }

Hope this helps... Take care, Tom.


I solved this problem by creating a new DataSet, loading in in the background, then calling DataSet.Merge on the UI thread. Thanks everyone for your advice, which led to this solution.

As an added bonus, this runs much faster than it used to (calling Fill in the background, which only worked with no grids open). Does anyone know why?

0

精彩评论

暂无评论...
验证码 换一张
取 消