Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Bug Async await freeze after Winform event fires

Discussion in 'Scripting' started by Threeyes, Mar 28, 2023.

  1. Threeyes

    Threeyes

    Joined:
    Jun 19, 2014
    Posts:
    76
    I use winform dll to create contextmenu for my windows app, and subscribe context button's MouseDown event to open a UI Window:
    Code (CSharp):
    1.  
    2. void Init
    3. {
    4.       notifyIcon.MouseDown +=OnNotifyIcon_MouseDown;
    5. }
    6. .
    7. .
    8. void OnNotifyIcon_MouseDown(object sender, MouseEventArgs e)
    9. {
    10.        UIWindow.Open; //Open UI Window
    11. }
    upload_2023-3-28_23-31-33.png

    but after I click any of the context button and open the UI, all async codes freeze on await Task (but the program not freeze), which all running well before the clicking:
    Code (CSharp):
    1.     async void QeuryPageItems(int pageIndex)
    2.     {
    3.         await Task.Yield();//Freezed here
    4.         ...//Can't be executed
    5.     }
    Did anyone encounter this problem? Thanks!

    Btw, I am using Unity2021.3.5f1
     
    Last edited: Mar 30, 2023
  2. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,744
    My guess is that the app is "awaiting" without telling Unity it can continue rendering frames/etc, which would indeed just result in a freeze. Unity isn't really built with the default C# async/await in mind - the "official" way to do asynchronous code is with coroutines.

    Try using the UniTask library. It's honestly been one of the best recent additions to my own Unity repertoire, and it's easily the best way to do asynchronous code in Unity, and IMO it's only a matter of time before it gets integrated into Unity. With that library your code would become:
    Code (csharp):
    1. async UniTask QueryPageItems(int pageIndex)
    2. {
    3. await UniTask.DelayFrame(1);
    4. // etc
    5. }
    And that much I can confidently say, does not freeze Unity.
     
  3. Threeyes

    Threeyes

    Joined:
    Jun 19, 2014
    Posts:
    76
    Thanks for your reply, I had try UniTask before and it works well, but some of my plugins has using Unity async/await for a long time and it's hard to support extra library, so I wonder if there are any solution for this?
     
  4. AdamLiu

    AdamLiu

    Joined:
    Mar 19, 2014
    Posts:
    71
    Did you manage to solve this at last?
     
  5. bugfinders

    bugfinders

    Joined:
    Jul 5, 2018
    Posts:
    763
    if you rock over to 2023.1 the "await Awaitable.yourchoice" works exceptionally well.
     
  6. Threeyes

    Threeyes

    Joined:
    Jun 19, 2014
    Posts:
    76
    Sorry but not luck, Unity 2022.3 still have the same problem, so I have to ditch all winfrom UI. maybe you can try 2023.1 as #5 bugfinders mentioned.