C# 任务中的等待方法

C# 任务中的等待方法,c#,.net,windows-store-apps,task,C#,.net,Windows Store Apps,Task,我在windows应用商店应用程序中使用文档下载程序,但任务有问题。 下面是我的代码示例: 任务已创建并启动 ... HttpDownloader httpDownloader = new HttpDownloader(server); Action<object> action = (object doc ) => httpDownloader.DownloadDocument((Document)doc); Task t1 = new Task(action,selec

我在windows应用商店应用程序中使用文档下载程序,但任务有问题。 下面是我的代码示例:

任务已创建并启动

...
HttpDownloader httpDownloader = new HttpDownloader(server);

Action<object> action = (object doc ) => httpDownloader.DownloadDocument((Document)doc);

Task t1 = new Task(action,selection.doc);
t1.Start();
...
...
FileSavePicker savePicker = new FileSavePicker();
savePicker.SuggestedStartLocation = PickerLocationId.DocumentsLibrary;
// Dropdown of file types the user can save the file as
savePicker.FileTypeChoices.Add("Application/pdf", new List<string>() { ".pdf" });
// Default file name if the user does not type one in or select a file to replace
savePicker.SuggestedFileName = doc.name+"_"+doc.version;

StorageFile file = await savePicker.PickSaveFileAsync(); // Here an exception is launch.
...
。。。
HttpDownloader HttpDownloader=新的HttpDownloader(服务器);
动作动作=(对象单据)=>httpDownloader.DownloadeDocument((单据)单据);
任务t1=新任务(action,selection.doc);
t1.Start();
...
下载文档方法

...
HttpDownloader httpDownloader = new HttpDownloader(server);

Action<object> action = (object doc ) => httpDownloader.DownloadDocument((Document)doc);

Task t1 = new Task(action,selection.doc);
t1.Start();
...
...
FileSavePicker savePicker = new FileSavePicker();
savePicker.SuggestedStartLocation = PickerLocationId.DocumentsLibrary;
// Dropdown of file types the user can save the file as
savePicker.FileTypeChoices.Add("Application/pdf", new List<string>() { ".pdf" });
// Default file name if the user does not type one in or select a file to replace
savePicker.SuggestedFileName = doc.name+"_"+doc.version;

StorageFile file = await savePicker.PickSaveFileAsync(); // Here an exception is launch.
...
。。。
FileSavePicker savePicker=新FileSavePicker();
savePicker.SuggestedStartLocation=PickerLocationId.DocumentsLibrary;
//用户可以将文件保存为的文件类型下拉列表
savePicker.FileTypeChoices.Add(“Application/pdf”,newlist(){.pdf});
//如果用户未键入文件名或未选择要替换的文件,则为默认文件名
savePicker.SuggestedFileName=doc.name+“”+doc.version;
StorageFile file=wait savePicker.PickSaveFileAsync();//这里有一个例外是启动。
...
每次我得到:

未找到元素(HRESULT异常:0x80070490)


如果没有任务,我的代码可以正常工作,但由于我想使用任务来管理不同的下载,因此出现了此错误。

您的
操作
在随机池线程上运行,该线程与主线程不同(由
任务.启动
计划)。在这里,您可以访问
doc
对象,我认为它是在主线程上创建的。这可能是故障的原因

通常,您不应该跨不同的线程访问对象(尤其是UI元素),除非它们是专门设计为线程安全的

已编辑:此处可能不需要任务。只需等待savePicker.PickSaveFileAsync()并将外部方法标记为
async
(当前创建任务的方法)

为了更好地了解您所在的线程,添加如下调试跟踪可能会有所帮助:

Debug.Print("<Method name>, Thread: {0}", Thread.CurrentThread.ManagedThreadId);
Debug.Print(“,线程:{0}”,Thread.CurrentThread.ManagedThreadId);

谢谢您的回答。我将对此进行检查。不,对象
doc
未参与此错误。它显然来自调用
wait savePicker.PickSaveFileAsync()
。是的,它在没有任务的情况下工作正常,只使用异步方法。但我正在寻找一种方法来管理所有这些生成的任务,以便取消它们或显示它们的列表。有没有办法访问默认线程池或TaskFactory并获得对当前任务的控制?我只能说FileSavePicker是在与主UI线程不同的线程上创建和调用的。我不确定它是否是为此而设计的,因为它的线程上没有UI(比如父窗体)。在您发布的代码中,
DownloadDocument
操作所做的唯一事情就是显示用户界面,让用户选择文件名。接下来,它是否真的保存了文件?您可能希望为IO绑定操作而不是UI操作使用后台任务。我猜您希望管理实际执行保存/上载文件任务的任务,对吗?