0

When I added the below code to perform async & await,

 namespace loadmultipleimages
 {
    public partial class Form1 : Form
    {
        VideoCapture capture = new VideoCapture();
        delegate void delHugeTask();
     
        public Form1()
        {
           InitializeComponent();
        }
    
        public static /*async*/ Task CopyToAsync( this Stream input, Stream output, CancellationToken cancellationToken = default(CancellationToken))
        {
           return CopyToAsyncTasks(input, output, cancellationToken).ToTask();
        }

        private  IEnumerable<Task> CopyToAsyncTasks(Stream input, Stream output, CancellationToken cancellationToken)
        {
          byte[] buffer = new byte[0x1000];   // 4 KiB
          while (true)
          {
            cancellationToken.ThrowIfCancellationRequested();
            var bytesReadTask = input.Read(buffer, 0, buffer.Length);
            yield return bytesReadTask;
            if (bytesReadTask.Result == 0) break;

            cancellationToken.ThrowIfCancellationRequested();
            yield return output.Write(buffer, 0, bytesReadTask.Result);
          }
       }
    }
 }

This error pops up & blue underline under Form 1 at public partial class Form1 : Form

   Error    1   Extension method must be defined in a non-generic static class

I know that if you want to use static methods, Class must be defined as static too. But how to work my way around this problem?

  • 2
    The easiest way, is upgrade – TheGeneral Jul 02 '21 at 10:26
  • 3
    Does this answer your question? [async/await keywords not available in .net 4.0](https://stackoverflow.com/questions/19423251/async-await-keywords-not-available-in-net-4-0) – Sinatr Jul 02 '21 at 10:27
  • Or maybe [this one](https://stackoverflow.com/q/9110472/1997232)? – Sinatr Jul 02 '21 at 10:28
  • 2
    The problem is that you write an Extension Method because of `this Stream input`. Also that static method calls a non static member method. That is not possible. These kind of problems have nothing to do with async/await. – Andre Kampling Jul 02 '21 at 10:31
  • 2
    .Net 4.0 hasn't been supported for more than 5 years. Time to upgrade! (.Net 4.8 will support what you want; you don't need .net 5) – Matthew Watson Jul 02 '21 at 10:32
  • 1
    You simply can't use the async/await keywords in Pre-4.5-.NET and pre-C#5.0. So either upgrade the framework, or implement the async calls with another pattern/lib such as APM, use a background workers or Task.Run, TaskFactory etc. – lidqy Jul 02 '21 at 10:46
  • Note: there's no such thing as .NET Framework 5.0. The last one that will ever be is 4.8. – Paulo Morgado Jul 02 '21 at 12:11
  • @AndreKampling Yeah, I do understand that, It is just that when using the above method to sort of simulate async/await in .NET 4.5 & above, I have to use static method or else another different error will occur at **ToTask()** as below `return CopyToAsyncTasks(input, output, cancellationToken).ToTask();` Error:'System.Collections.Generic.IEnumerable' does not contain a definition for 'ToTask' and no extension method 'ToTask' accepting a first argument of type 'System.Collections.Generic.IEnumerable' could be found – nickshim06 Jul 04 '21 at 18:02
  • @Sinatr thank you for the link reference, yes I have read that Microsoft.Bcl.Async NuGet package can get async/await working. Will try on that one. – nickshim06 Jul 04 '21 at 18:10
  • @MatthewWatson Yeah I thought of upgrading, but the OS that I will be using this application on is Windows 7/XP if I am not mistaken. That is why I am not looking to upgrade the .NET Framework. Thanks for the suggestion though!! Appreciate it – nickshim06 Jul 04 '21 at 18:14

1 Answers1

2

The best solution is to change the target framework of your project to something like 4.8. The only reason I know of to support .NET 4.0 is to continue support for Windows XP. And in that case I recommend you push back to whoever made that business decision: are you absolutely sure you want to support an OS that Microsoft hasn't supported for more than 7 years? To me, that seems to have some serious liability questions.

But, assuming you can't convince your boss that supporting an ancient OS isn't a good idea, you can use the Microsoft.Bcl.Async NuGet package to get async/await working. Note that this only works for desktop apps, not ASP.NET apps.

Stephen Cleary
  • 437,863
  • 77
  • 675
  • 810
  • I wish I could change to 4.8 but I can't because the OS used is Windows 7/XP so I can only use .NET 4.0. And yeah probably should talk with my boss regarding this. Nonetheless, thank you Stephen, I haven't try the package yet. But, will put my hands on it. And see if I ever encounter any further problem, will come back again. – nickshim06 Jul 04 '21 at 18:22
  • @nickshim06 What makes you think you can't use .Net Framework 4.8 on Windows 7? – Matthew Watson Jul 05 '21 at 08:29
  • @MatthewWatson long story that I can't tell it here. Nonetheless, I have tried to add 'Microsoft.Bcl.Async.' into my project through cmd prmpt. However, I cannot add this package as VS2010 doesn't have NuGet. Any advice on this? – nickshim06 Jul 06 '21 at 08:00
  • this is the error I got `Error while adding package 'Microsoft.Bcl.Async' to project ' '. The project does not support adding package references through the add package command` – nickshim06 Jul 06 '21 at 08:06
  • @nickshim06: It's not likely you'll find much help working with tech over a decade old when that tech has been updated multiple times a year. I used to work with VS2010, sure, but I barely remember all its quirks at this point. IIRC, you needed VS2012 for `Microsoft.Bcl.Async`; in the VS2010 era, `async` had to update the compiler, and there's no known way of getting the Async CTP on VS2010 working these days even if you can find the downloads. – Stephen Cleary Jul 06 '21 at 11:57