首先,让我说,这是很难详细解释我的问题,但我会尽我所能。我会用更详细的解释或更多的代码来更新,这些代码可能会导致异常。如果我的代码很混乱,我很抱歉。寻找是什么导致“没有发现任务的例外......”
有很多SO问题与我已阅读的相同标题,但我没有运气。我在这里对Thread/Task/Dispatcher有一点点的理解,所以如果你发现我的代码有问题,请指导我。
Intoduction
我的应用程序通过定时器做后台任务每ñ分钟。
后台任务:从API获取数据,然后生成窗口元件,以包含所述数据的形式,然后打印它们。
问题:此时异常已发生两次,这会阻止生成两个表单文档,因此它们不会被打印。
的详细的异常是从TaskScheduler.UnobservedTaskException
得到的是:
-
任务的异常(或多个)未观察到无论是等待在Task或访问其Exception属性。结果,未被观察到的异常被终结器线程重新抛出。
- 堆栈跟踪:N/A
- 内部异常:
System.Collections.ObjectModel.ReadOnlyCollection`1 [System.Exception的]
这里是我的一段代码,可能对您有用,可以找到问题的根源:
public void BackgroundTask(object sender, EventArgs e)
{
Application.Current.Dispatcher.Invoke(
new Action(GetInvoiceData),
DispatcherPriority.Background,
null
);
}
...其中GetInvoiceData
是:
public async void GetInvoiceData()
{
try
{
JsonData = await ApiHelperInstance.Post(ApiParam);
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
finally
{
if (!string.IsNullOrEmpty(JsonData))
{
var apiReturn = new ApiReturn();
try
{
apiReturn = JsonConvert.DeserializeObject<ApiReturn>(JsonData);
}
catch (JsonException ex)
{
Console.WriteLine(ex.Message);
}
finally
{
if (apiReturn.Result != null)
{
foreach (ApiResult apiResult in apiReturn.Result)
{
InvoiceQueue.Enqueue(new Invoice(apiResult));
}
var worker = new BackgroundWorker();
worker.DoWork += GenerateDocumentAndPrint;
worker.RunWorkerAsync();
}
}
}
}
}
...和GenerateDocumentAndPrint
是:
public void GenerateDocumentAndPrint(object sender, DoWorkEventArgs e)
{
while (InvoiceQueue.Count != 0)
{
Dispatcher.Invoke(() =>
{
Invoice invoice = InvoiceQueue.Dequeue();
var invoiceForm = new InvoiceForm();
var shippingLabelForm = new ShippingLabelForm();
invoiceForm.Dispatcher.Invoke(async() =>
{
var invoiceTmp = invoice;
var invoiceDoc = new FixedDocument();
try
{
invoiceDoc = await invoiceForm.CreateDocument(invoiceTmp);
}
finally
{
InvoiceDocumentName = PrintJobNameSub + " - Invoice #" + invoice.TransOrder.TransNumber;
PrintHelperInstance.SetPrinterByName(InvoicePrinterName);
PrintHelperInstance.PrintDocument(invoiceDoc.DocumentPaginator, InvoiceDocumentName);
invoiceForm.Close();
}
}, DispatcherPriority.ContextIdle);
shippingLabelForm.Dispatcher.Invoke(async() =>
{
var invoiceTmp = invoice;
var shippingLabelDoc = new FixedDocument();
try
{
shippingLabelDoc = await shippingLabelForm.CreateDocument(invoiceTmp);
}
finally
{
ShippingLabelDocumentName = PrintJobNameSub + " - Shipping Label #" + invoice.TransOrder.TransNumber;
PrintHelperInstance.SetPrinterByName(ShippingLabelPrinterName);
PrintHelperInstance.PrintDocument(shippingLabelDoc.DocumentPaginator, ShippingLabelDocumentName);
shippingLabelForm.Close();
}
}, DispatcherPriority.ContextIdle);
}, DispatcherPriority.Normal);
}
}
...和异步方法CreateDocument
从两个InvoiceForm
和ShippingLabelForm
包含await Task.Delay(delay)
。
有我从我的代码所做的任何错误?是否由于错误地使用了Dispatcher.Invoke
而引起?是否因使用DispatcherPriority
enum错误引起? Task.Delay
操作有问题吗?
如果发现由invoiceForm.CreateDocument(invoiceTmp)引发的任何异常,该怎么办? – mm8
@ mm8我已经捕捉到了所有未处理的异常,我刚才提到的异常是唯一被捕获的异常。 –
您在调用等待invoiceForm.CreateDocument(invoiceTmp)时没有catch子句。 – mm8