• WPF使用ThreadPool.QueueUserWorkItem线程池防界面假死


    其实并不光是WPF,在WinForm中也会经常遇到这种导致界面假死的现象,在目前,防止假死的方法几乎都是使用线程来进行,比如使用System.ComponentModel.BackgroundWorker,现在还有一种方法是使用线程池ThreadPool.QueueUserWorkItem来防止界面假死,而至于说使用这两个类哪一个更好,其实还是要根据需求的不同而选择更合适的方法。这里只是举一个基本的例子,用来更新UI上的实时数据,而采用ThreadPool.QueueUserWorkItem的方式。

    ThreadPool.QueueUserWorkItem((o) =>
    {
        for (long i = 1; i < 1000000; i++)
        {
            t1.Dispatcher.Invoke(new Action(() =>
            {
                t1.Text = i.ToString();
            }));
        }
    });

      上面这段代码中t1是UI中的一个TextBlock控件,这样,在更新的时候就解决了假死的问题。

      本文来自luacloud的博客,原文地址:http://luacloud.com/2012/wpf-threadpool-queueuserworkitem.html

  • 相关阅读:
    csp-s模拟103
    csp-s模拟102
    csp-s模拟101
    csp-s模拟100
    csp-s模拟99
    csp-s模拟98
    csp-s模拟97
    csp-s模拟96
    csp-s模拟95
    csp-s模拟94
  • 原文地址:https://www.cnblogs.com/javalinux/p/14422249.html
Copyright © 2020-2023  润新知