Compile eror in WinForms code using WinRT and await

已鎖定 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
     

所有回覆

  • 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.
  • 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