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. Using await/async Correctly

Using await/async Correctly

Scheduled Pinned Locked Moved C#
sharepointxmljsonquestion
5 Posts 3 Posters 1 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

    I'm a bit confused on something. I have a Sharepoint class with a DownloadFileAsync method:

    using System.IO;
    using System.Threading.Tasks;

    namespace WpfApp1
    {
    internal class Sharepoint
    {
    public async Task DownloadFileAsync(string folderName, string downloadPath = null)
    {
    var url = _clientContext.Web.ServerRelativeUrl + "/Shared%20Documents/" + folderName;
    Folder targetFolder = _clientContext.Web.GetFolderByServerRelativeUrl(url);
    _clientContext.Load(targetFolder.Files);

            await \_clientContext.ExecuteQueryAsync();
    
            foreach (var file in targetFolder.Files)
            {
                FileInformation sharepointFile = Microsoft.SharePoint.Client.File.OpenBinaryDirect(\_clientContext, file.ServerRelativeUrl);
                await \_clientContext.ExecuteQueryAsync();
    
                var path = string.IsNullOrEmpty(downloadPath) ? DEFAULT\_DIRECTORY : downloadPath;
                path = path + @"\\" + file.Name;
                using (FileStream filestream = new FileStream(path, FileMode.Create))
                {
                    sharepointFile.Stream.CopyTo(filestream);
                }
            }
        }
    }
    

    }

    It is called from a Repository class

    using System.IO;
    using System.Threading.Tasks;

    namespace WpfApp1
    {
    internal class Repository
    {
    string _appPath;
    Sharepoint _sharePoint;

        public Repository()
        {
            \_sharePoint = new Sharepoint();
            \_appPath = $"{Path.GetTempPath()}RDSPaymentProcessing";
    
        }
    
        public async Task GETDataAsync()
        {
            var results = new MyData();
    
            // If the local file already exists, delete it
            var fileName = $"{Path.GetTempPath()}{LOCAL\_APP\_DATA\_FOLDER}\\\\sharepointdata.xml";
            if (File.Exists(fileName))
            {
                File.Delete(fileName);
            }
    
            // Download all files from the Settings folder into the local user's app path
            await \_sharePoint.DownloadFile(SETINGS\_FOLDER, \_appPath);
    
            // If the file was downloaded...
            if (File.Exists(fileName))
            {
                // Deserialize it
                results = Serialization.DeSerializeObject(fileName);
            }
            else
            {
                //TODO: Replace with custom exception
                throw new FileNotFoundException(fi
    
    L Richard DeemingR 2 Replies Last reply
    0
    • K Kevin Marois

      I'm a bit confused on something. I have a Sharepoint class with a DownloadFileAsync method:

      using System.IO;
      using System.Threading.Tasks;

      namespace WpfApp1
      {
      internal class Sharepoint
      {
      public async Task DownloadFileAsync(string folderName, string downloadPath = null)
      {
      var url = _clientContext.Web.ServerRelativeUrl + "/Shared%20Documents/" + folderName;
      Folder targetFolder = _clientContext.Web.GetFolderByServerRelativeUrl(url);
      _clientContext.Load(targetFolder.Files);

              await \_clientContext.ExecuteQueryAsync();
      
              foreach (var file in targetFolder.Files)
              {
                  FileInformation sharepointFile = Microsoft.SharePoint.Client.File.OpenBinaryDirect(\_clientContext, file.ServerRelativeUrl);
                  await \_clientContext.ExecuteQueryAsync();
      
                  var path = string.IsNullOrEmpty(downloadPath) ? DEFAULT\_DIRECTORY : downloadPath;
                  path = path + @"\\" + file.Name;
                  using (FileStream filestream = new FileStream(path, FileMode.Create))
                  {
                      sharepointFile.Stream.CopyTo(filestream);
                  }
              }
          }
      }
      

      }

      It is called from a Repository class

      using System.IO;
      using System.Threading.Tasks;

      namespace WpfApp1
      {
      internal class Repository
      {
      string _appPath;
      Sharepoint _sharePoint;

          public Repository()
          {
              \_sharePoint = new Sharepoint();
              \_appPath = $"{Path.GetTempPath()}RDSPaymentProcessing";
      
          }
      
          public async Task GETDataAsync()
          {
              var results = new MyData();
      
              // If the local file already exists, delete it
              var fileName = $"{Path.GetTempPath()}{LOCAL\_APP\_DATA\_FOLDER}\\\\sharepointdata.xml";
              if (File.Exists(fileName))
              {
                  File.Delete(fileName);
              }
      
              // Download all files from the Settings folder into the local user's app path
              await \_sharePoint.DownloadFile(SETINGS\_FOLDER, \_appPath);
      
              // If the file was downloaded...
              if (File.Exists(fileName))
              {
                  // Deserialize it
                  results = Serialization.DeSerializeObject(fileName);
              }
              else
              {
                  //TODO: Replace with custom exception
                  throw new FileNotFoundException(fi
      
      L Offline
      L Offline
      Lost User
      wrote on last edited by
      #2

      Use a BackgroundWorker; it's easier to get one's head around. About the only thing one is interested in with async is: 1) Is it finished (callback) 2) When can I use the data (use "concurrent" collections and events).

      "Before entering on an understanding, I have meditated for a long time, and have foreseen what might happen. It is not genius which reveals to me suddenly, secretly, what I have to say or to do in a circumstance unexpected by other people; it is reflection, it is meditation." - Napoleon I

      K 1 Reply Last reply
      0
      • L Lost User

        Use a BackgroundWorker; it's easier to get one's head around. About the only thing one is interested in with async is: 1) Is it finished (callback) 2) When can I use the data (use "concurrent" collections and events).

        "Before entering on an understanding, I have meditated for a long time, and have foreseen what might happen. It is not genius which reveals to me suddenly, secretly, what I have to say or to do in a circumstance unexpected by other people; it is reflection, it is meditation." - Napoleon I

        K Offline
        K Offline
        Kevin Marois
        wrote on last edited by
        #3

        Hmm. I always thought background workers were 'outdated'. Seems to me that all you hear about these days is async/await.

        In theory, theory and practice are the same. But in practice, they never are.” If it's not broken, fix it until it is. Everything makes sense in someone's mind.

        L 1 Reply Last reply
        0
        • K Kevin Marois

          Hmm. I always thought background workers were 'outdated'. Seems to me that all you hear about these days is async/await.

          In theory, theory and practice are the same. But in practice, they never are.” If it's not broken, fix it until it is. Everything makes sense in someone's mind.

          L Offline
          L Offline
          Lost User
          wrote on last edited by
          #4

          Wood working tools are also "outdated" by that logic.

          "Before entering on an understanding, I have meditated for a long time, and have foreseen what might happen. It is not genius which reveals to me suddenly, secretly, what I have to say or to do in a circumstance unexpected by other people; it is reflection, it is meditation." - Napoleon I

          1 Reply Last reply
          0
          • K Kevin Marois

            I'm a bit confused on something. I have a Sharepoint class with a DownloadFileAsync method:

            using System.IO;
            using System.Threading.Tasks;

            namespace WpfApp1
            {
            internal class Sharepoint
            {
            public async Task DownloadFileAsync(string folderName, string downloadPath = null)
            {
            var url = _clientContext.Web.ServerRelativeUrl + "/Shared%20Documents/" + folderName;
            Folder targetFolder = _clientContext.Web.GetFolderByServerRelativeUrl(url);
            _clientContext.Load(targetFolder.Files);

                    await \_clientContext.ExecuteQueryAsync();
            
                    foreach (var file in targetFolder.Files)
                    {
                        FileInformation sharepointFile = Microsoft.SharePoint.Client.File.OpenBinaryDirect(\_clientContext, file.ServerRelativeUrl);
                        await \_clientContext.ExecuteQueryAsync();
            
                        var path = string.IsNullOrEmpty(downloadPath) ? DEFAULT\_DIRECTORY : downloadPath;
                        path = path + @"\\" + file.Name;
                        using (FileStream filestream = new FileStream(path, FileMode.Create))
                        {
                            sharepointFile.Stream.CopyTo(filestream);
                        }
                    }
                }
            }
            

            }

            It is called from a Repository class

            using System.IO;
            using System.Threading.Tasks;

            namespace WpfApp1
            {
            internal class Repository
            {
            string _appPath;
            Sharepoint _sharePoint;

                public Repository()
                {
                    \_sharePoint = new Sharepoint();
                    \_appPath = $"{Path.GetTempPath()}RDSPaymentProcessing";
            
                }
            
                public async Task GETDataAsync()
                {
                    var results = new MyData();
            
                    // If the local file already exists, delete it
                    var fileName = $"{Path.GetTempPath()}{LOCAL\_APP\_DATA\_FOLDER}\\\\sharepointdata.xml";
                    if (File.Exists(fileName))
                    {
                        File.Delete(fileName);
                    }
            
                    // Download all files from the Settings folder into the local user's app path
                    await \_sharePoint.DownloadFile(SETINGS\_FOLDER, \_appPath);
            
                    // If the file was downloaded...
                    if (File.Exists(fileName))
                    {
                        // Deserialize it
                        results = Serialization.DeSerializeObject(fileName);
                    }
                    else
                    {
                        //TODO: Replace with custom exception
                        throw new FileNotFoundException(fi
            
            Richard DeemingR Offline
            Richard DeemingR Offline
            Richard Deeming
            wrote on last edited by
            #5

            You should generally avoid async void methods[^]. As per David Fowler's async guidance[^], you can either use Task.Run to push the execution onto a background thread, or call a Task-returning method and "discard" the returned task. Either option will allow exceptions to fire the TaskScheduler.UnobservedTaskException event[^] rather than crashing the application from the finalizer thread. In this case:

            internal class MainWindowViewModel
            {
            private MyEntity _data;

            private async Task LoadData()
            {
                \_data = await \_repository.GETDataAsync();
                // TODO: Raise any "property changed" events to notify the view that the data has loaded.
            }
            
            public void WindowLoadedExecuted()
            {
                \_ = LoadData();
            }
            

            }


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

            "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