views:

28

answers:

1
    var t = new Thread(new ParameterizedThreadStart(DoWork));
    t.SetApartmentState(ApartmentState.STA);
    t.IsBackground = true;
    t.Start(App.Current.MainWindow);

    public static void DoWork(object owner)
    {
        var progressDlg = new ProgressBarDialog();

        // progressDlg.Owner = (Window)owner; // This doesn't work

        progressDlg.ShowDialog();
    }

Now, tell me please is it possible to make it work?

App.Current.MainWindow in the example not accessible from another thread.

And also I've heard about new cool way of Parallel.Invoke() but I don't know is that applicable for this situation or not. I appreciate if you show me how it works.

A: 

It is not possible to access the UI from a background thread, all your updates must be on the main thread. This includes setting dependency properties, like you are trying to do in the above case. You can get around this by using the Dispatcher.

Something like this

 Action x = (Action)delegate {
       //do my UI updating
    };
    Dispatcher.Invoke(x, new object[] { });

which is the same answer i gave here...

Aran Mulholland
it doesn't' work because I can't get any access to the (Window)owner from separate thread. But theoretically it isn't something impossible. There should be a way to create a window in another thread and get the owner of that window from the main thread.
Ike