EmailDate = mail_Message.Date.ToString()

时间:2021-08-18 04:08:49

很多时候需要用到这些小常识点,做做条记一起生长

下面是需要异步执行的要领

//获取所有的邮件 private List<EmailModel> GetEmailOnlyCount(POP3_ClientMessageCollection pOP3Collection) { int i = 0; List<EmailModel> list = new List<EmailModel>(); foreach (POP3_ClientMessage message in pOP3Collection) { byte[] bytes = System.Text.Encoding.Default.GetBytes(message.HeaderToString()); Mail_Message mail_Message = Mail_Message.ParseFromByte(bytes); EmailModel emailModel = new EmailModel() { EmailFrom = mail_Message.From.ToString(), EmailDate = mail_Message.Date.ToString(), EmailSubject = mail_Message.Subject.ToString(), }; list.Add(emailModel);
    //异步线程中,转变主线程中的控件属于跨线程的挪用,需要操作代办代理要领 textBox5.Invoke(showProcessDelegate,
"已经读取" + (++i) + "/" + pOP3Collection.Count + "封邮件"); } return list; }

我们需要执行以上的要领,异步中执行由于带有参数,所以我们需要代办代理这个要领,用一个变量来取代这一个要领,并且声明异步要领执行结束的回调要领

private delegate List<EmailModel> GetMailDelegate(POP3_ClientMessageCollection pOP3_ClientMessage); private GetMailDelegate getMailDelegate = new GetMailDelegate(GetEmailOnlyCount);

private AsyncCallback asyncCallback;//执行完的回调要领,异步要领执行完后执行的要领

接着我们在需要执行此要领的处所,开始异步执行此要领,固然是操作代办代理要领执行

getMailDelegate.BeginInvoke(pOP3_ClientMessageCollection, asyncCallback, null);

等异步要领执行完之后我们需要在回调要领之中结束异步操纵所以我们需要在asynCallBack之中进行操纵

这里我们就是获取异步执行的要领的返回值的处所

asyncCallback = delegate (IAsyncResult result) { EmaliList = getMailDelegate.EndInvoke(result);//*异步,并且获取到要领的返回值 for (int i = EmaliList.Count; i > 0; i--) { ListViewItem listViewItem = new ListViewItem(); listViewItem.Text = EmaliList[i - 1].EmailFrom; listViewItem.SubItems.Add(EmaliList[i - 1].EmailSubject); listViewItem.SubItems.Add(EmaliList[i - 1].EmailDate); listViewItem.SubItems.Add(EmaliList[i - 1].AttchmentsCount.ToString()); listView1.Invoke(showListViewDelegate, listViewItem); } };

好了,这里我们的邮箱的异步读取邮件的操纵就根基完成了=v=