Compile eror in WinForms code using WinRT and await
-
2012年2月6日 上午 10:38
Hi,maybe I'm not quite right here in this forum, but anyway:I'm trying to compile a WinForms app that uses WinRT. Framework Version 4.5 is selected. Referencing the WinRT assemblies and calling asynchronous APIs works when using a delegate.But when I try await I get a Compiler error:error CS4001: Cannot await 'Windows.Storage.StatusOperation'Here's an excerpt from my source code:void Init() { var docfolder = KnownFolders.DocumentsLibrary; StorageFileRetrievalOperation op = docfolder.CreateFileAsync("sample.txt"); op.Completed += new AsyncOperationCompletedHandler<StorageFile>(create_completed); op.Start(); } private void create_completed(IAsyncOperation<StorageFile> asyncInfo) { if (asyncInfo.Status == AsyncStatus.Completed) { Invoke(new MethodInvoker(()=> { label1.Text = "File created: " + asyncInfo.GetResults().Path; } )); deleteit(asyncInfo.GetResults()); } } private async void deleteit(StorageFile storageFile) { StatusOperation op; var res = await storageFile.DeleteAsync(); }The error occurs on the line with await.Someone has an idea?Thanks,Thomas- 已移動 Rob CaplanMicrosoft Employee 2012年2月6日 下午 05:44 Not a Metro app question: how to use async in a desktop app (From:Building Metro style apps with C# or VB )
- 已移動 Michael Sun [MSFT]Microsoft Employee 2012年2月7日 上午 05:21 (From:Visual C# General)
所有回覆
-
2012年2月6日 上午 11:21
first make a try{}catch (Exception ex) {} around the DeleteAsync method - the operation may fail, e.g. because you don't have write permissions or the file is in use.
Also try this:
StatusOperation op; op = storageFile.DeleteAsync(); await op; //now you can check for success using op.Status..
good luck^ -
2012年2月6日 上午 11:25
... doesn't help ...
-
2012年2月7日 上午 08:41
Try this:
StatusOperation op; var res = await Task<StatusOperation>.Factory.StartNew(()=>storageFile.DeleteAsync());
Hope It Helps.
Javier Torrecilla
Para el correcto funcionamiento, y que otros usuarios se puedan beneficiar de la solucion de esta pregunta por favor marca las respuestas que te hayan ayudado como "Respuesta".
Si la respuesta te ha sido util Votala.
Mi Blog: Jtorrecilla
Enlace a Faq de Winforms en Ingles Muy bueno
TabControl con Mejoras -
2012年2月7日 下午 02:14
Javier,
this line compiles, but when I run it, it doesn't wait to finish the task. Instead res.Status is Created and the delete operation is not executed. The type of the result, StatusOperation, doesn't fit. Shouldn't await return the type of the operation's GetResults() member, i.e. void here?
Thomas
-
2012年2月7日 下午 02:27
Umh Sorry I have a mistake:
StatusOperation op; var res = await Task<StatusOperation>.Factory.StartNew(()=>storageFile.DeleteAsync()); op = res.Result;
I forget to assing to OP.
Untill the DeleteAsync is not finished the op must be null.
Javier Torrecilla
Para el correcto funcionamiento, y que otros usuarios se puedan beneficiar de la solucion de esta pregunta por favor marca las respuestas que te hayan ayudado como "Respuesta".
Si la respuesta te ha sido util Votala.
Mi Blog: Jtorrecilla
Enlace a Faq de Winforms en Ingles Muy bueno
TabControl con Mejoras -
2012年2月7日 下午 04:05版主
The extension methods that allow you to await WinRT async actions and operations are in the System.Runtime.WindowsRuntime.dll in the System namespace. Make sure you've referenced that assembly and imported the right namespace.- 已提議為解答 Stephen Toub - MSFTMicrosoft Employee, Moderator 2012年2月7日 下午 04:05
- 已標示為解答 thomas_schmidt 2012年2月8日 上午 06:54
-
2012年2月8日 上午 07:01
Stephen,
thanks for your helpful reply.
Are there other (in Metro apps invisible) references one might need in WinForms apps? Is there a sample for an unmanaged desktop application using WinRT?
I'm trying to evaluate how much use I could make of WinRT in our existing apps, for example in order to speed up things that are currently implemented in managed code to or use new features like the sensors.
Thanks,
Thomas

