1、进程

我们可以把计算机中每一个运行的应用程序都当做是一个进程。
而一个进程又是由多个线程组成的。

  1. static void Main(string[] args)
  2. {
  3. //获得当前程序中所有正在运行的进程
  4. Process[] pros = Process.GetProcesses();
  5. foreach (var item in pros)
  6. {
  7. Console.WriteLine(item);
  8. }
  9. //通过进程打开一些应用程序
  10. //Process.Start("calc");
  11. //Process.Start("mspaint");
  12. //Process.Start("notepad");
  13. //Process.Start("iexplore", "http://www.baidu.com");
  14. //通过一个进程打开指定的文件
  15. //ProcessStartInfo psi = new ProcessStartInfo(@"C:\Users\SpringRain\Desktop\1.exe");
  16. //第一:创建进程对象
  17. //Process p = new Process();
  18. //p.StartInfo = psi;
  19. //p.Start();
  20. // p.star
  21. Console.ReadKey();
  22. }

2、单线程给我们带来的问题

        public Form1()
        {
            InitializeComponent();
        }
        Thread th;
        private void button1_Click(object sender, EventArgs e)
        {
            //创建一个线程去执行这个方法
            th = new Thread(Test);
            //标记这个线程准备就绪了,可以随时被执行。具体什么时候执行这个线程,
            //由cpu决定
            //将线程设置为后台线程
            th.IsBackground = true;
            th.Start();//带参数在start里面执行赋值
            //th.Abort();
            //th.Start();

        }

        private void Test()
        {
            for (int i = 0; i < 10000; i++)
            {
                //Console.WriteLine(i);
                textBox1.Text = i.ToString();
            }
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            //取消跨线程的访问
            Control.CheckForIllegalCrossThreadCalls = false;
        }

        private void Form1_FormClosing(object sender, FormClosingEventArgs e)
        {
            //当你点击关闭窗体的时候,判断新线程是否为null
            if (th != null)
            {
                //结束这个线程
                th.Abort();
            }
        }