开发者

Show window with progressbar while doing work

Because I have a tool that needs to do a lot of work at one point, I want to show a window with some text and a progressbar while doing the work.

My problem is, that because of the huge load of the work, the window never gets drawn or updated.

I know that I usually should use an extra thread for the work, but I have to use 2 collections and the database service from the开发者_Go百科 current thread.

The current code is something like

StatusWindow SW = new StatusWindow();
StatusViewModel SVM = new StatusViewModel();

SVM.MaxNum = BigNumber;

SW.Show();

for (int i=0; i<BigNumber; i++)
{
    List<AType> ItemsToCreate = Func1();
    List<AType> ItemsToDelete = Func2();

    foreach (AType cItem in ItemsToCreate)
        DB.CreateItem(cItem);

    foreach (AType cItem in ItemsToDelete)
        DB:DeleteItem(cItem);

    SVM.CurrentNum = i;
}
SW.Close();

I also read about the Dispatcher, but i don't think it is very usable in this scenario, since there would be too much access to the main thread.

Any suggestions?

Thanks


I recommend reading the article (Build More Responsive Apps With The Dispatcher) from MSDN magazine that describes how WPF works with the Dispatcher when using BackgroundWorker.


You can pass the two collections and whatever else you'd like to worker threads as long as the objects you are accessing can be accessed from different threads.

int[] arrayofInt = new int[10]
ThreadPool.QueueUserWorkItem(s =>
        {
            //access the array in the worker thread
            arrayofInt[0] = 10;
        });

When finished with the work, you need to send a message to the dialog that it should be closed now. Do that by firing an event in your thread that is handled by your dialog.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜