views:

35

answers:

2

Hi.
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 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

+1  A: 

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.

testalino
Could you please give an short example of how to pass them to the new thread?
Marks
I edited my answer a little.
testalino
+2  A: 

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.

Zamboni