C#网络编程(2)-进程、线程和应用程序域

       进程,就是正在运行的程序,是操作系统层面的,比如我们打开任务管理器,看到很多进程,有前台带界面的,还有后台进程。进程之间是相互独立的,就是不同的进程无法访问另外进程的数据。

       线程,就是进程内负责具体任务执行的,一个进程可以只有一个线程,也可以是多线程,但是现在的操作系统都是多核心,多线程肯定是最好的选择。

        进程管理类(Process类),这个类是专门做进程相关的,可以启动进程,结束线程,也可以获取进程的相关信息。

        线程的类是Thread,开启,终止等等,另外一个就是线程池ThreadPool,线程池最大的好处是降低了创建线程的资源开销,比如线程池中的任务完成,就返回队列,等待被再次使用,而不是直接销毁。

class Program
    {
        const int cycleNum = 10;

        static void Main(string[] args)
        {
            ThreadPool.SetMinThreads(1, 1);
            ThreadPool.SetMaxThreads(5, 5);
            for (int i = 1; i <= cycleNum; i++)
            {
                ThreadPool.QueueUserWorkItem(new WaitCallback(testFun), i.ToString());
            }
            Console.WriteLine("主线程执行!");
            Console.WriteLine("主线程结束!");
            Console.ReadKey();
        }
        public static void testFun(object obj)
        {
            Console.WriteLine(string.Format("{0}:第{1}个线程", DateTime.Now.ToString(), obj.ToString()));
            Thread.Sleep(5000);
        }
    }

       最后就是应用程序域,应用程序域是针对单个进程来说的,用的比较多的,就是在一个程序中,打开另外一个应用程序,使这个应用程序看起来就是打开他的应用程序中的一样。 

   <Grid>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="20*"></ColumnDefinition>
            <ColumnDefinition Width="60*"></ColumnDefinition>
        </Grid.ColumnDefinitions>
        <StackPanel>
            <Button Click="Button_Click">打开PR</Button>
            <Button>打开网易云音乐</Button>
            <Button>打开迅雷</Button>
        </StackPanel>
        <Frame Name="f1" Grid.Column="1" Background="yellow"></Frame>
    </Grid>
var c = AppDomain.CurrentDomain;
            string exePath = c.BaseDirectory + @"\Adobe Premiere Pro.exe";
            string typeName = "Adobe Premiere Pro.MainWindow";
            var mainWindow = (Window)c.CreateInstanceFromAndUnwrap(exePath, typeName);
            f1.Content = mainWindow.Content;

猜你喜欢

转载自blog.csdn.net/whjhb/article/details/88862642