我使用MailKit库从新到达的电子邮件中每隔x秒保存附件。我需要在一个单独的线程上运行这个进程,这样它就不会阻塞我的UI。每当我的代码到达ImapClient实例化时,它就会抛出一个错误:“线程被中止”。
ExceptionState:“无法计算表达式,因为代码经过优化,或者本机框架位于调用堆栈的顶部。”。
堆栈跟踪:
at MailKit.MailService..ctor(IProtocolLogger protocolLogger)
at MailKit.Net.Imap.ImapClient..ctor(IProtocolLogger protocolLogger)
at MailKit.Net.Imap.ImapClient..ctor()
at Domain.ApInvoicePrinting.<>c.<Run>b__4_0() in C:\...\MyClass.cs:line 29

代码:
var task = new Task(() =>
{
try
{
using (var client = new MailKit.Net.Imap.ImapClient()) // Exception thrown at this line
{
...
}
}
catch(Exception e)
{
...
}
}
task.Start();我尝试使用System.Threading.Timer运行它,但仍然得到相同的错误。但是,在主线程上,一切都很好。我在这里错过了什么?
发布于 2018-12-11 20:34:42
结果发现,问题是使用MS Test在单元测试项目中运行该代码。当我使用控制台应用程序运行完全相同的代码时,一切都很好!不过,我不知道这背后的原因是什么。
https://stackoverflow.com/questions/53730006
复制相似问题