开发者

WPF invoke from two threads

开发者 https://www.devze.com 2023-03-31 20:00 出处:网络
I try to invoke twice and then from anther object once more. public void Show() { if (mainGrid == null) return;

I try to invoke twice and then from anther object once more.

public void Show()
{
    if (mainGrid == null)
        return;
    if (!Dispatcher.CheckAccess())
    {
        Dispatcher.BeginInvoke(new ThreadStart(delegate() { Show(); }), DispatcherPriority.Background);
        return;
    }

    mainGrid.Children.Add(rec);
    rec.HorizontalAlignment = System.Windows.HorizontalAlignment.Stretch;
    rec.VerticalAlignment = System.Windows.VerticalAlignment.Str开发者_C百科etch;

    mainGrid.Children.Add(this);
    this.HorizontalAlignment = System.Windows.HorizontalAlignment.Center;
    this.VerticalAlignment = System.Windows.VerticalAlignment.Center;
}

two objects B, C inherit this function from A: when I call from processes B.show() then B.showStop() from Thread and C.show() I get exception on using privileged main.children.Add(..) from C.show() please help me solve this problem.


I think you should provide more info on you problem, for example:

  • which kind of class is A (it seems to be an UI object though)
  • what is rec

But for the moment i think the problem could be that you're instantiating those visual objects not in the UI Thread: if you create say a Button on a workerthread it will have that thread Dispatcher, which is different from the UI Thread Dispatcher.

I can't exactly tell you what's happening with so little info but i think that you're adding two controls which were created on different threads so one of the two Add will give you an exception.

0

精彩评论

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