zoukankan      html  css  js  c++  java
  • 默认情况下程序启动后到底是几个线程?

    默认情况下,在调试状态下我们可以看到下面的效果

    image

    这里面的线程中

    • 主线程 不必多说,就是程序的主要工作线程
    • SystemEvents线程之前探讨过是系统事件触发之后的工作线程
    • 另外几个线程,很奇怪。但至少那个vshost.RunParkingWindow,从字面上说可能与vshost有关

    接下来,我们尝试禁用掉vshost(下图中禁用“启用Visual Studio宿主进程”)

    image

    再来看线程信息

    image

    整个世界清净多了,现在就剩下了一个线程。这与我们一般意义上讲的单线程程序是相吻合的。

    那么,为什么连那个System Events线程都没有了呢?这是因为我们当前程序并没有监控有关的SystemEvents事件,所以它默认是不会创建这个线程的

    如果我们添加了如下的代码

    using System;
    using System.Threading;
    using Microsoft.Win32;
    
    namespace ConsoleApplication1
    {
        class Program
        {
            
            static void Main(string[] args)
            {
    
                Console.WriteLine("Main Thread ID:{0}", Thread.CurrentThread.ManagedThreadId);
    
                SystemEvents.TimerElapsed += new TimerElapsedEventHandler(SystemEvents_TimerElapsed);
                SystemEvents.CreateTimer(1000);
                Console.Read();
    
    
            }
    
            static void SystemEvents_TimerElapsed(object sender, TimerElapsedEventArgs e)
            {
                Console.WriteLine("System Events Thread ID:{0}",Thread.CurrentThread.ManagedThreadId);
            }
    
        }
    }
    

    那么,此时就可以看到那个辅助线程了

    image

    那么,到这里为止,你应该知道了,一个应用程序开起来之后到底会有几个线程了吧?

  • 相关阅读:
    为图片指定区域添加链接
    数值取值范围问题
    【leetcode】柱状图中最大的矩形(第二遍)
    【leetcode 33】搜索旋转排序数组(第二遍)
    【Educational Codeforces Round 81 (Rated for Div. 2) C】Obtain The String
    【Educational Codeforces Round 81 (Rated for Div. 2) B】Infinite Prefixes
    【Educational Codeforces Round 81 (Rated for Div. 2) A】Display The Number
    【Codeforces 716B】Complete the Word
    一个简陋的留言板
    HTML,CSS,JavaScript,AJAX,JSP,Servlet,JDBC,Structs,Spring,Hibernate,Xml等概念
  • 原文地址:https://www.cnblogs.com/chenxizhang/p/1685269.html
Copyright © 2011-2022 走看看