Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Error Making Method Async

Error Making Method Async

Scheduled Pinned Locked Moved C#
helpsysadmintutorialquestion
4 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • K Offline
    K Offline
    Kevin Marois
    wrote on last edited by
    #1

    My app has a FileSystemWatcher. Once a file is dropped into the target folder it is then Ftp'd to the server. I'm trying to make it async but I'm getting errors. I want to make the method async so that multiple files can be uploaded without blocking.

    class Program
    {
    private static string _address;
    private static string _userName;
    private static string _password;
    static void Main(string[] args)
    {
    }
    // A FileSystemWatcher passes the file name into this method
    public static async void FileReceived(string fileName)
    {
    Task t = await Task.Run(() => FTPFile(fileName)); // <=== ERROR HERE
    await t.ContinueWith((t1) =>
    {
    FileUploaded(t1.Result);
    });
    }
    // FTP the file to the server
    private static string FTPFile(string fileName)
    {
    string remoteFile = Path.GetFileName(fileName);
    SFTP sftp = new SFTP(_address, _userName, _password);
    sftp.UploadFileAsync(fileName, remoteFile);
    return fileName;
    }
    private static async void FileUploaded(string fileName)
    {
    // Once here, the file has been transfered.
    }
    }

    On the Task line I get the error:

    Cannot implicitly convert type 'string' to 'System.Threading.Tasks.Task'

    I'm not sure how to fix this. Can someone help? Thanks

    If it's not broken, fix it until it is. Everything makes sense in someone's mind. Ya can't fix stupid.

    G P Richard DeemingR 3 Replies Last reply
    0
    • K Kevin Marois

      My app has a FileSystemWatcher. Once a file is dropped into the target folder it is then Ftp'd to the server. I'm trying to make it async but I'm getting errors. I want to make the method async so that multiple files can be uploaded without blocking.

      class Program
      {
      private static string _address;
      private static string _userName;
      private static string _password;
      static void Main(string[] args)
      {
      }
      // A FileSystemWatcher passes the file name into this method
      public static async void FileReceived(string fileName)
      {
      Task t = await Task.Run(() => FTPFile(fileName)); // <=== ERROR HERE
      await t.ContinueWith((t1) =>
      {
      FileUploaded(t1.Result);
      });
      }
      // FTP the file to the server
      private static string FTPFile(string fileName)
      {
      string remoteFile = Path.GetFileName(fileName);
      SFTP sftp = new SFTP(_address, _userName, _password);
      sftp.UploadFileAsync(fileName, remoteFile);
      return fileName;
      }
      private static async void FileUploaded(string fileName)
      {
      // Once here, the file has been transfered.
      }
      }

      On the Task line I get the error:

      Cannot implicitly convert type 'string' to 'System.Threading.Tasks.Task'

      I'm not sure how to fix this. Can someone help? Thanks

      If it's not broken, fix it until it is. Everything makes sense in someone's mind. Ya can't fix stupid.

      G Offline
      G Offline
      George Swan
      wrote on last edited by
      #2

      You should start the Task and then await it.

      public static async void FileReceived(string fileName)
      {
      Task<string> t = Task.Run(() => FTPFile(fileName));
      await t.ContinueWith((t1) =>
      {
      FileUploaded(t1.Result);
      });
      }

      1 Reply Last reply
      0
      • K Kevin Marois

        My app has a FileSystemWatcher. Once a file is dropped into the target folder it is then Ftp'd to the server. I'm trying to make it async but I'm getting errors. I want to make the method async so that multiple files can be uploaded without blocking.

        class Program
        {
        private static string _address;
        private static string _userName;
        private static string _password;
        static void Main(string[] args)
        {
        }
        // A FileSystemWatcher passes the file name into this method
        public static async void FileReceived(string fileName)
        {
        Task t = await Task.Run(() => FTPFile(fileName)); // <=== ERROR HERE
        await t.ContinueWith((t1) =>
        {
        FileUploaded(t1.Result);
        });
        }
        // FTP the file to the server
        private static string FTPFile(string fileName)
        {
        string remoteFile = Path.GetFileName(fileName);
        SFTP sftp = new SFTP(_address, _userName, _password);
        sftp.UploadFileAsync(fileName, remoteFile);
        return fileName;
        }
        private static async void FileUploaded(string fileName)
        {
        // Once here, the file has been transfered.
        }
        }

        On the Task line I get the error:

        Cannot implicitly convert type 'string' to 'System.Threading.Tasks.Task'

        I'm not sure how to fix this. Can someone help? Thanks

        If it's not broken, fix it until it is. Everything makes sense in someone's mind. Ya can't fix stupid.

        P Offline
        P Offline
        Pete OHanlon
        wrote on last edited by
        #3

        Your FTPFile method returns a string. You're trying to put it in a Task of string.

        This space for rent

        1 Reply Last reply
        0
        • K Kevin Marois

          My app has a FileSystemWatcher. Once a file is dropped into the target folder it is then Ftp'd to the server. I'm trying to make it async but I'm getting errors. I want to make the method async so that multiple files can be uploaded without blocking.

          class Program
          {
          private static string _address;
          private static string _userName;
          private static string _password;
          static void Main(string[] args)
          {
          }
          // A FileSystemWatcher passes the file name into this method
          public static async void FileReceived(string fileName)
          {
          Task t = await Task.Run(() => FTPFile(fileName)); // <=== ERROR HERE
          await t.ContinueWith((t1) =>
          {
          FileUploaded(t1.Result);
          });
          }
          // FTP the file to the server
          private static string FTPFile(string fileName)
          {
          string remoteFile = Path.GetFileName(fileName);
          SFTP sftp = new SFTP(_address, _userName, _password);
          sftp.UploadFileAsync(fileName, remoteFile);
          return fileName;
          }
          private static async void FileUploaded(string fileName)
          {
          // Once here, the file has been transfered.
          }
          }

          On the Task line I get the error:

          Cannot implicitly convert type 'string' to 'System.Threading.Tasks.Task'

          I'm not sure how to fix this. Can someone help? Thanks

          If it's not broken, fix it until it is. Everything makes sense in someone's mind. Ya can't fix stupid.

          Richard DeemingR Offline
          Richard DeemingR Offline
          Richard Deeming
          wrote on last edited by
          #4

          You should avoid async void methods wherever possible: Avoid async void methods - You’ve Been Haacked[^] Async/Await - Best Practices in Asynchronous Programming[^] Also, sftp.UploadFileAsync implies that the method returns before the upload has completed. Does this method return a Task, or does it raise an UploadFileCompleted event? If it raises an event (EAP pattern), you can wrap that in a Task-returning method, and make your FTPFile method async as well. How to: Wrap EAP Patterns in a Task[^] Tasks and the Event-based Asynchronous Pattern | Parallel Programming with .NET[^]

          public static class SFTPExtensions
          {
          public static Task UploadFileTaskAsync(this SFTP sftp, string fileName, string remoteFile)
          {
          var tcs = new TaskCompletionSource<bool>();

              UploadFileCompletedEventHandler handler = null;
              handler = (sender, args) =>
              {
                  if (args.Cancelled)
                  {
                      tcs.TrySetCancelled();
                  }
                  else if (args.Error != null)
                  {
                      tcs.TrySetException(args.Error);
                  }
                  else
                  {
                      tcs.TrySetResult(true);
                  }
                  
                  sftp.UploadFileCompleted -= handler;
              };
              
              sftp.UploadFileCompleted += handler;
              
              try
              {
                  sftp.UploadFileAsync(fileName, remoteFile);
              }
              catch (Exception ex)
              {
                  sftp.UploadFileCompleted -= handler;
                  tcs.TrySetException(ex);
              }
              
              return tcs.Task;
          

          "These people looked deep within my soul and assigned me a number based on the order in which I joined" - Homer

          1 Reply Last reply
          0
          Reply
          • Reply as topic
          Log in to reply
          • Oldest to Newest
          • Newest to Oldest
          • Most Votes


          • Login

          • Don't have an account? Register

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • World
          • Users
          • Groups