zoukankan      html  css  js  c++  java
  • WPF实现多线程加载数据

    背景:最近自己用WPF做了一个邮件接收和发送系统,在获取邮件列表的时候整个界面会卡主,所以想办法解决这个问题。

    演示:

    test

    实现代码:

    这是写在ViewModel里的一个方法,用于获取邮件列表。ViewModel并没有Dispatcher,这是使用App.Current.Dispatcher去获取到UI的线程。

    public void FetchAllMessages2(object paraNumber)
            {
                string num = paraNumber.ToString();
                string hostname = MailAccount.hostname;
                int port = MailAccount.port;
                bool useSsl = MailAccount.useSsl;
                string username = MailAccount.username;
                string password = MailAccount.password;
                // The client disconnects from the server when being disposed
                using (Pop3Client client = new Pop3Client())
                {
                    // Connect to the server
                    client.Connect(hostname, port, useSsl);
    
                    // Authenticate ourselves towards the server
                    client.Authenticate(username, password);
    
                    // Get the number of messages in the inbox
                    int messageCount = client.GetMessageCount();
    
                    // We want to download all messages
                    List<Message> allMessages = new List<Message>(messageCount);
    
                    // Messages are numbered in the interval: [1, messageCount]
                    // Ergo: message numbers are 1-based.
                    // Most servers give the latest message the highest number
                    int number = 20;
                    if (!string.IsNullOrEmpty(num))
                    {
                        try
                        {
                            number = Int32.Parse(num);
                        }
                        catch (Exception e)
                        {
                            System.Windows.MessageBox.Show(e.Message);
                        }
                    }
                    for (int i = messageCount; i > messageCount - number; i--)
                    {
                        allMessages.Add(client.GetMessage(i));
    
                    }
    
                    // Now return the fetched messages
                    List<Mail> maillist = new List<Mail>();
                    maillist = ChangeMessageToMail(allMessages);
                    App.Current.Dispatcher.BeginInvoke((Action)delegate()
                    {
                        MailList = maillist;
                    });
                }
            }
    public MailViewModel()
            {
                //写下面这语句时会报“的重载均与委托‘System.Threading.ParameterizedThreadStart’不匹配”,后来把函数
                //FetchAllMessages2的变量改成Object类型就可以了
                Thread newThread = new Thread(new ParameterizedThreadStart(FetchAllMessages2));
                newThread.Start("15");
            }
  • 相关阅读:
    luogu P1486 [NOI2004]郁闷的出纳员
    Luogu P1894 [USACO4.2]The Perfect Stall
    关于中间6个月停更通知
    Luogu P1381油滴扩展
    没有上司的舞会(题解)
    幂的模运算(题解)
    闭合区域面积统计(题解)
    字符序列(题解)
    最大连续和(题解)
    排列问题
  • 原文地址:https://www.cnblogs.com/peijia/p/6801422.html
Copyright © 2011-2022 走看看