在Unity/C#中,.Net的async/await是否确实从另一个线程开始? [英] In Unity / C#, does .Net's async/await start, literally, another thread?

查看:1017
本文介绍了在Unity/C#中,.Net的async/await是否确实从另一个线程开始?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

重要,对于专门在Unity中专门研究此困难主题的人

Important for anyone researching this difficult topic in Unity specifically,

确保看到我问的另一个问题,它提出了相关的关键问题:

be sure to see another question I asked which raised related key issues:

在Unity中,具体而言,"where"表示真的会返回吗?

对于C#专家而言,Unity是单线程的 1

For C# experts, Unity is single-threaded1

通常在另一个线程上执行计算等.

It's common to do calculations and such on another thread.

当您在另一个线程上执行某些操作时,您经常会使用async/wait,因为,所有好的C#程序员都说这是简单的方法!

When you do something on another thread, you often use async/wait since, uh, all the good C# programmers say that's the easy way to do that!

void TankExplodes() {

    ShowExplosion(); .. ordinary Unity thread
    SoundEffects(); .. ordinary Unity thread
    SendExplosionInfo(); .. it goes to another thread. let's use 'async/wait'
}

using System.Net.WebSockets;
async void SendExplosionInfo() {

    cws = new ClientWebSocket();
    try {
        await cws.ConnectAsync(u, CancellationToken.None);
        ...
        Scene.NewsFromServer("done!"); // class function to go back to main tread
    }
    catch (Exception e) { ... }
}

好的,所以当您执行此操作时,当您在Unity/C#中以更常规的方式启动线程(因此使用Thread或其他方法,一个本机插件或OS或其他任何情况).

OK, so when you do this, you do everything "just as you normally do" when you launch a thread in a more conventional way in Unity/C# (so using Thread or whatever or letting a native plugin do it or the OS or whatever the case may be).

一切都很好.

作为一个la脚的Unity程序员,他只知道足够的C#来结束一天,我一直假定,上面上的async/await模式实际上会启动另一个线程 >.

As a lame Unity programmer who only knows enough C# to get to the end of the day, I have always assumed that the async/await pattern above literally launches another thread.

实际上,上面的代码是从字面上启动了另一个线程,还是c#/.Net在使用自然的异步/等待模式时是否使用其他方法来完成任务?

In fact, does the code above literally launch another thread, or does c#/.Net use some other approach to achieve tasks when you use the natty async/wait pattern?

它在Unity引擎中的工作方式可能与一般使用C#"有所不同或不同? (IDK?)

Maybe it works differently or specifically in the Unity engine from "using C# generally"? (IDK?)

请注意,在Unity中,它是否是线程会极大地影响您处理后续步骤的方式.因此是问题.

Note that in Unity, whether or not it is a thread drastically affects how you have to handle the next steps. Hence the question.

问题:我意识到关于正在等待线程"的讨论很多,但是,(1)我从未见过在Unity设置中讨论过或回答过的问题 (这有什么区别吗?IDK吗?)(2)我只是从未见过明确的答案!

Issue: I realize there's lots of discussion about "is await a thread", but, (1) I have never seen this discussed / answered in the Unity setting (does it make any difference? IDK?) (2) I simply have never seen a clear answer!

1 一些辅助计算(例如,物理等)是在其他线程上完成的,但是实际的基于框架的游戏引擎"是一个纯线程. (无论如何都无法访问"主机框架线程:例如,在编程本机插件或对另一个线程进行某种计算时,您只需在引擎框架线程上留下组件的标记和值即可查看和在运行每帧时使用.)

推荐答案

我不喜欢回答自己的问题,但事实证明,这里没有一个答案是完全正确的. (但是,这里的许多/所有答案在不同方面都非常有用).

I don't like answering my own question, but as it turns out none of the answers here is totally correct. (However many/all of the answers here are hugely useful in different ways).

实际上,实际答案可以概括地说:

In fact, the actual answer can be stated in a nutshell:

就是这样.

因此,在任何特定版本的Unity中(请注意,截至撰写2019年,它们正在彻底更改Unity- https://unity.com/dots )-或实际上根本没有任何C#/.Net环境-可以正确回答此页面上的问题.

Thus in any particular version of Unity (and note that, as of writing 2019, they are drastically changing Unity - https://unity.com/dots) - or indeed any C#/.Net environment at all - the question on this page can be answered properly.

https://stackoverflow.com/a/55614146/294884

这篇关于在Unity/C#中,.Net的async/await是否确实从另一个线程开始?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆