Search Unity

System.Threading.Tasks.Task not found

Discussion in 'Scripting' started by dfl_unity, Jan 8, 2020.

  1. dfl_unity

    dfl_unity

    Joined:
    Sep 5, 2019
    Posts:
    4
    Hi all,

    I'm new to Unity, trying to write a managed DLL that will host my game's various services (comm, io etc). Using windows 10 with the latest .net.
    When I try to use asynchronous constructs, specifically as async/await and Tasks, I get errors. Async/await causes unity to notify me that the DLL is broken and is unloaded, which subsequently makes the symbols from the DLL invisible to the game scripts, despite being available in the assets folder and in the references section of the unity VS project.
    When I use Tasks (from system.threading.Tasks), the code compiles and the unity scripts see my public symbols, but I get a runtime error that System.Threading.Tasks.Task was not found. If I use the same Tasks code *inside* the unity VS project, it works well.

    For now I've resorted to the Threads .net api, which does what I want and works well in this setup. However, I would prefer the convenience of async/await and Tasks.

    I suspect that I need to use some special libs for these async constructs, but no search that I've run produced clear answers. Any help will be appreciated.
     
  2. olejuer

    olejuer

    Joined:
    Dec 1, 2014
    Posts:
    211
    Hi,

    can you try to set the API compatability level to .NET 4.x under "Edit/Project Settings/Player/Other Settings"? Can you check if your dll is imported targeting .NET 4.x? If you select the dll in Unity, the inspector should show that information.
     
  3. dfl_unity

    dfl_unity

    Joined:
    Sep 5, 2019
    Posts:
    4
    Thanks for the quick reply.
    The DLL's inspector says that the DLL targets .NET 4.x.
    In the project itself, the target framework is .net core 3.1.
    Another project with the same settings is imported with no problem (but doesn't reference System.Threading.Tasks).
     
  4. olejuer

    olejuer

    Joined:
    Dec 1, 2014
    Posts:
    211
    Yeah, I think async await is only supported in .net 4.x
     
  5. olejuer

    olejuer

    Joined:
    Dec 1, 2014
    Posts:
    211
    What do the player settings say in Unity? There is only 2.0 and 4.x as choices, I think
     
  6. dfl_unity

    dfl_unity

    Joined:
    Sep 5, 2019
    Posts:
    4
    Hi

    the player settings (API Compatibility level) target .net 4.x
    the project targets .net core 3.1 (I also tried .net core 2.0, same result).
    The dll is identified by unity as targeting .net 4.x
    Again, the dll is
    1. not imported (identified as broken) if it uses async/await
    2. Generates a runtime error upon access to system.threading.tasks.task

    Thanks again
     
  7. olejuer

    olejuer

    Joined:
    Dec 1, 2014
    Posts:
    211
    It has to be either some issue with creating the dll or importing it, right? Since there isn't much to configure when importing, I guess something is off with creating the dll. Those are all guesses that are as good as anyones, I am afraid. I have no experience with importing a dll that makes use of async/await. Might also be that it just does not work for some reason we cannot fix.
    Can you somehow include system.threading in your dll when building it? Dunno if that's a good idea.
     
  8. dfl_unity

    dfl_unity

    Joined:
    Sep 5, 2019
    Posts:
    4
    I did some more digging around.
    When I instantiate a System.Threading.Tasks.Task in a C# file included in the unity project, it is taken from mscorlib.dll provided by unity (in my case, under C:\Program Files\Unity\Hub\Editor\2019.2.16f1\Editor\Data\MonoBleedingEdge\lib\mono\unityjit).
    I suspect that I need to compile my DLL against the same mscorlib, but despite adding it to my references and setting <NoStdLib>true</NoStdLib> in the project file, I get the same errors.

    Again, the problem is specifically with System.Threading.Tasks. Other DLLs that I've written are loaded and executed in my unity project with no problems.
     
  9. Samasab

    Samasab

    Joined:
    May 13, 2018
    Posts:
    13
    Did you happen to find a solution for this @dfl_unity? I am facing the same problem with a Dll using System.Threading.Tasks
     
  10. pallamadhukar

    pallamadhukar

    Joined:
    Dec 2, 2021
    Posts:
    2
    Did anyone find a solution for this
     
  11. beardhero

    beardhero

    Joined:
    Jun 29, 2013
    Posts:
    1
    Please - any solution?
     
  12. unity_2jxHNVY36zr2mg

    unity_2jxHNVY36zr2mg

    Joined:
    Jan 17, 2020
    Posts:
    86
    using Task = System.Threading.Tasks.Task;

    The type 'Task' exists in both 'Unity.Tasks, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null' and 'netstandard, Version=2.1.0.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51' Assembly-CSharp C:\UnityProjects\HCProto - Copy (3)\Assets\UI\UserDialogManager.cs 10 Active
     
  13. Gillissie

    Gillissie

    Joined:
    May 16, 2011
    Posts:
    305
    I'm facing this with an older project that I just upgraded to Unity 2021. I have another project with the same code in it that works fine. I can't figure out what's different about the project settings that might cause this.

    [edit] ok, in my case I had another class in the project named Task, so it was clashing. Like I said, it's an older project, otherwise I wouldn't have name the class that. ha
     
    Last edited: Oct 7, 2022
    ahsanrocks95 likes this.
  14. ahsanrocks95

    ahsanrocks95

    Joined:
    Nov 18, 2022
    Posts:
    2
    Hey
    any one still facing the issue read here
    i was facing the same issue, so there are two "task" classes that are conflicting
    i resolved by deleting a file in the plugins folder called "unity.task"
    and the issue was resolved
     
  15. sin8392

    sin8392

    Joined:
    Wednesday
    Posts:
    1
    you save my life
     
  16. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    11,458
    Please use the Like button to show appreciation so as to not necro the thread.