[英]ThreadPool C#, result of worker item is too slow, why?
我不知道自己是否做錯了什么,我正在使用SmartThreadPool來管理線程。 在這種情況下,我要調用SmartThreadPool的一個實例,然后獲取工作項的結果(發送電子郵件的方法)。 “字符串結果”接收方法的輸出。 如果確定,則顯示確定,否則顯示異常。
SmartThreadPool smartThreadPool = new SmartThreadPool();
String result = (String)smartThreadPool.QueueWorkItem(x => emailHelper.sendEmail(whichMail.Text, emailTo.Text, subject.Text, bodyMsg.Text)).Result;
if (result != "True")
helper.showMessageBox(Properties.Resources.emailNotSent + result, "x");
else
aux.setRadDesktop(Properties.Resources.emailSent, "", false);
問題是,當我將結果強制轉換為String時,速度太慢,甚至我的UI也暫停了大約2秒鍾,我不知道為什么。 如果我通過執行此操作忽略工人項目結果:
smartThreadPool.QueueWorkItem(x => emailHelper.sendEmail(whichMail.Text, emailTo.Text, subject.Text, bodyMsg.Text));
一切正常,有任何線索嗎?
問題不在於轉換為字符串,而是在於使用Result
屬性,該屬性在返回值之前等待工作項完成。
我最終做了一個嵌套線程,它的工作有缺陷。 謝謝大家
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.