简体   繁体   English

主线程中可能有后台工作人员DoWork吗?

[英]Is possible backgroundworker DoWork in main thread?

Is possible backgroundworker DoWork in main thread? 主线程中可能有后台工作人员DoWork吗? I meant if some device only have one CPU , Is it possible to happen? 我的意思是,如果某些设备只有一个CPU,有可能发生吗?

PS: I'm working on unity , So run Android & IOS & Windows Platform. PS:我正在团结一致,所以请运行Android&IOS&Windows Platform。

    System.ComponentModel.BackgroundWorker bw = new System.ComponentModel.BackgroundWorker();
    bool inMainThread = false;
    int mainThreadId = System.Threading.Thread.CurrentThread.ManagedThreadId;

    bw.DoWork += (object sender, System.ComponentModel.DoWorkEventArgs e) =>
        {   // Is it possible equal true ??
            inMainThread = mainThreadId == System.Threading.Thread.CurrentThread.ManagedThreadId;
        }

No it is not possible. 不,这是不可能的。 A BackgroundWorker will always do its work on an seperate thread. BackgroundWorker将始终在单独的线程上进行工作。

To the point with an system with only one CPU. 对于只有一个CPU的系统而言。 An OS runs many programs asynchronous and every program can have many threads. 操作系统异步运行许多程序,每个程序可以有许多线程。 The OS will schedule all this threads to run at somepoint on any available CPU. 操作系统将安排所有这些线程在任何可用CPU上的某个时间运行。 This is crucial to maintain a smooth running system, especially when a system has only one CPU. 这对于维持平稳运行的系统至关重要,尤其是在系统只有一个CPU的情况下。

If you want to know more you could look here or here and for a start I would recommend this video. 如果您想了解更多信息,可以在这里这里开始,我推荐这个视频。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM