Await alternative in .NET 4.0?

前端 未结 2 1884
生来不讨喜
生来不讨喜 2021-01-04 18:32

What would be the best alternative for the await keyword in .NET 4.0 ? I have a method which needs to return a value after an asynchronous operation. I noticed the wait() me

2条回答
  •  無奈伤痛
    2021-01-04 19:00

    You could implement a behaviour like await with the yield coroutines, i'm using this in non-4.5 code. You need a YieldInstruction class which is retrieved from the method which should run async:

    public abstract class YieldInstruction
    {
        public abstract Boolean IsFinished();
    }
    

    Then you need some implementations of the YieldInstruction ( a.e. TaskCoroutine which handles a task ) and use it this way ( Pseudo code ):

    public IEnumerator DoAsync()
    {
        HttpClient client = ....;
        String result;
        yield return new TaskCoroutine(() => { result = client.DownloadAsync(); });
        // Process result here
    }
    

    Now you need a scheduler which handles the execution of the instructions.

    for (Coroutine item in coroutines)  
    {  
        if (item.CurrentInstruction.IsFinished())  
        {
             // Move to the next instruction and check if coroutine has been finished 
             if (item.MoveNext()) Remove(item);
        }
    }
    

    When developing WPF or WinForms applications you are also able to avoid any Invoke calls if you are updating the coroutines at the right time. You also might be able to extend the idea to make your life even easier. Sample:

    public IEnumerator DoAsync()
    {
        HttpClient client = ....;
        client.DownloadAsync(..);
    
        String result;
        while (client.IsDownloading)
        {
            // Update the progress bar
            progressBar.Value = client.Progress;
            // Wait one update
            yield return YieldInstruction.WaitOneUpdate;
        }
        // Process result here
    }
    

提交回复
热议问题