首页 > 代码库 > 步步为营-64-进程&线程

步步为营-64-进程&线程

1 进程

技术分享
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace 进程线程
{
    class Program
    {
        static void Main(string[] args)
        {
            //获取当前进程
            Process p1 = Process.GetCurrentProcess();
            Console.WriteLine(p1.Id);
            Console.WriteLine(p1.ProcessName);

            //打开新的进程
            Process p2 = Process.Start("cmd.exe");
            string  key = Console.ReadLine();
            if (key=="k")
            {
                //杀死进程
                p2.Kill();
            }
            Console.ReadLine();


        }
    }
}
View Code

技术分享

2 应用程序域

技术分享
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace 应用程序域
{
    class Program
    {
        static void Main(string[] args)
        {
            //01 获取当前应用程序域
            AppDomain ad = AppDomain.CurrentDomain;
            Console.WriteLine(ad.FriendlyName);
            //02 在当前程序域中打开程序集,不会开启新进程
            AppDomain ap2= AppDomain.CreateDomain("xyxtl");
           int id = ap2.ExecuteAssembly("进程线程.exe");
            Console.Write(id);

           Console.ReadKey();
        }
    }
}
View Code

技术分享

3 线程

技术分享
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

namespace 线程
{
    class Program
    {
        static void Main(string[] args)
        {
            #region 01 获取当前线程-默认程序启动后,会有一个主线程
            Thread t1 = Thread.CurrentThread;
            Console.WriteLine(t1.ManagedThreadId);
            #endregion


            #region 02 开辟一个新线程 - 02-01 无参;02-02 有参
            //02-01 有参
            Thread t2 = new Thread(() =>
            {
                Console.WriteLine(Thread.CurrentThread.ManagedThreadId);//输出当前线程编号
                Console.WriteLine("无参,构造函数!");
            });
            t2.Start();

            //02-02 有参
            #endregion

            Thread t3 = new Thread((p) =>
            {
                //由于参数是object类型,如果想访问对象特有成员,需要进行类型转换
                Person p2 = p as Person;
                if (p2!=null)
                {
                    ;
                    Console.WriteLine(p2.Age);
                }
                
                Console.WriteLine(p.ToString());
            });
            t3.Start(new Person()
            {
                Name = "张三",
                Age = 18,
            });
            Console.Read();
        }

        public class  Person
        {
            public string Name { get; set; }
            public int Age { get; set; }

            public override string ToString()
            {
                return string.Format("姓名:{0},年龄{1}",Name,Age);
            }
        }
    }
}
View Code

技术分享

3.2 IsBackground属性

技术分享
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace 前台线程与后台线程
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            Thread t1 = new Thread(() =>
            {
                while (true)
                {
                    Console.WriteLine(1);
                }
            });
            //休息5秒
           // Thread.Sleep(5000);
           
            t1.Start();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            Thread t1 = new Thread(() =>
            {
                while (true)
                {
                    Console.WriteLine(2);
                }
            });
            t1.IsBackground = true;
            t1.Start();
        }
    }
}
View Code

当线程是后台线程时,主线程关闭,后台线程也随之关闭;
当线程是前台线程时,主线程关闭,前台线程不关闭;

技术分享

步步为营-64-进程&线程