UI线程和后台线程 [英] UI Thread and Background thread

查看:132
本文介绍了UI线程和后台线程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

UI线程是否与后台线程相同?我的应用程序是控制台或Windows服务。我没有UI界面,所以所有进程都是后台线程?



UI线程是I / O绑定还是CPU绑定?

解决方案

主线程或UI线程是操作系统调用以启动程序的线程。



后台线程是事物你开始并行做其他事情。



线程可以是IO绑定或CPU绑定,具体取决于它正在做什么。


< blockquote>我会定义UI线程是调用 Application.Run 的线程。这与解决方案1的定义不同,解决方案1几乎正确。



我可以解释其中的区别。可以有两个(或更多)不同的线程,并在不同的线程上运行不同的UI。这些UI将在不同的线程中执行,并且彼此无关。这些线程之间的通信需要调用 BeginInvoke System.Windows.Forms的方法) .Control Dispatcher )。这种情况非常奇特,很少使用(对于那些可能不相信我的人:是的,我试过了!你也可以尝试一下)。通常,UI线程与最初创建的线程是相同的线程,其中执行入口点。因此,通常,UI线程与入口点线程相同。但这不是法律,你可以使这些线程不同。



严格来说,没有UI线程的官方术语。我只是指出入口点线程和UI线程可以不同。 (顺便说一下,在WPF中,两个线程正在为UI工作。事情比许多人想象的要复杂得多。)



现在,所有其他线程如何。不,他们不必是后台线程(因此解决方案1在这里完全不正确),它们只是一些额外的线程。为什么?因为这个术语在.NET中具有非常特殊的含义:这是将属性 System.Threading.Thread.IsBackground 赋值为true的线程。什么是?当主线程终止时,后台线程不会保持进程,就像通常线程那样。请参阅:

http://msdn.microsoft.com/en-us/library/system.threading.thread.isbackground%28v=vs.110%29.aspx [ ^ ]。



-SA


Is UI thread same as Background thread? My application is a console or windows service. I don't have a UI interface, so all processes are Background threads?

Also is UI thread a I/O bound or CPU bound?

解决方案

Main thread or UI thread is the thread that the OS calls into to start your program.

Background threads are things you start to do other stuff in parallel.

A thread can be IO bound or CPU bound depending on what it is doing.


I would define UI thread is the thread where Application.Run is called. This is different from the definition of Solution 1, which is almost correct.

I can explain the difference. It is possible to have two (or more) different threads, and run different UI on different thread. Those UI will execute in different threads and unrelated to each other. Communication between those thread would require Invoke or BeginInvoke (the method of System.Windows.Forms.Control or Dispatcher). This scenario is really exotic and rarely used (for those who may not believe me: yes, I tried it! you can try it, too). Usually, the UI thread is the same thread as the thread initially created, where the entry-point is executed. So, typically, the UI thread is the same as entry-point thread. But this is not the law, you could make those threads different.

Strictly speaking, there is no the "official" term for "UI thread". I just pointed out that the entry-point thread and UI thread can be different. (By the way, in WPF, two threads are working for UI. Things are a bit more complex than many think.)

Now, how about all other threads. No, they don't have to be "background threads" (so the Solution 1 is totally incorrect here), they are just some extra threads. Why? Because this term has a very specific meaning in .NET: this is the thread with the property System.Threading.Thread.IsBackground assigned to true. What is does? Background thread don't hold the process when the main thread is terminated, as the "usual" thread does. Please see:
http://msdn.microsoft.com/en-us/library/system.threading.thread.isbackground%28v=vs.110%29.aspx[^].

—SA


这篇关于UI线程和后台线程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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