WPF UI update of a multi-threaded Solutions

So how to solve this problem? The usual practice is time-consuming to perform functions in the thread pool, and then cut back to the main UI thread to update the display. UpdateTime foregoing functions rewritten as follows:

    private async void updateTime()
    {
        while (true)
        {
            await Task.Run(() => Thread.Sleep(900));
            textBlock.Text = DateTime.Now.ToString();
            await Task.Delay(100);
        }
    }

In this way we can meet most needs. However, some operations are relatively time consuming. For example, when the multi-window real-time monitoring, we need to screen more than a dozen of dozens of times per second, each refreshed image is updated this operation must be performed on the UI thread, and it has a very time-consuming, this time Caton situation will return to the beginning.

This problem can not be solved seems, in fact, WPF is just not allowed to cross-thread access to the program, is not allowed to update multi-threaded interface. We could have a separate its separate thread for each video monitoring window, update operations in the thread, then it will not affect the main thread. There are articles on MSDN describes the detailed operation: the Multithreaded the UI: HostVisual . In this manner, the original program is rewritten as follows:

 e)
    {
        ();

        (hostVisual);
        .Content = content;

        (() =>
        {
            (hostVisual);
            ();
            control.Arrange((), content.RenderSize));
            visualTarget.RootVisual = control;

            System.Windows.Threading..Run();

        }));

        thread.SetApartmentState(.STA);
        thread.IsBackground = ;
        thread.Start();
    }

    
    {
         child;

         child)
        {
            )
                );

            .child = child;
            AddVisualChild(child);
        }

         index)
        {
            ;
        }

         VisualChildrenCount
        {
             1; }
        }
    }

The inside for two new classes: HostVisual, VisualTarget. And a VisualHost wrote it myself. MSDN on related interpretations, it is not difficult to understand here is not introduced. Finally, again refactoring the code, to create a new thread in the control mode to read as follows:

 e)
    {
        createChildInNewThread<);
    }

     container) 
        ()
    {
        ();

        (hostVisual);
        container.Content = content;

        (() =>
        {
            (hostVisual);

            ();
            control.Arrange((), content.RenderSize));

            visualTarget.RootVisual = control;
            System.Windows.Threading..Run();

        }));

        thread.SetApartmentState(.STA);
        thread.IsBackground = ;
        thread.Start();
    }

Of course, my function more unnecessary limitations: containers must be ContentControl, the child element must be a UIElement. It can perform modified according to actual needs.

Guess you like

Origin www.cnblogs.com/xietianjiao/p/11239332.html