进程

using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace _02进程和线程
{
    class Program
    {
        static void Main(string[] args)
        {
            //一个程序是一个进程,一个进程有多个线程
            //Process process = new Process();//可以实例化
            //获取本机程序的进程
            Process[] pros = Process.GetProcesses();
            //for (int i=0;i<pros.Length;i++)
            //{
            //    Console.WriteLine(pros);
            //}
            //foreach (var item in pros)
            //{
            //    //item.Kill();//停止本机关联的进程
            //    Console.WriteLine(item.ProcessName);//获取进程名称
            //}
            //Process.Start("calc");//打开计算器
            //Process.Start("mspaint");//打开画图
            //Process.Start("iexplore", "http://www.baidu.com");
            //创建一个进程信息
            ProcessStartInfo info = new ProcessStartInfo(@"E:\360Downloads\270266.jpg");
            Process process = new Process();
            process.StartInfo = info;
            process.Start();
            Console.ReadLine();
        }
    }
}

线程

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 _03线程
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        Thread th;
        private void button1_Click(object sender, EventArgs e)
        {
            //目前一共两个线程:一个主线程控制winform窗口另外一个子线程控制输出
            //Test();
            //假死:会记录当时的操作
            th = new Thread(Test);//test是委托
            //设置这个线程是后台线程
            th.IsBackground = true;
            //前台线程:只有所有的前台线程都关闭了才能完成程序关闭
            //后台线程:只要所有的前台线程都关闭了,后台线程会自动关闭
            th.Start();
        }
        void Test()
        {
            for (int i=0;i<100000;i++)
            {
                //Console.WriteLine(i);
                label1.Text = i.ToString();
            }
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            //.net中不允许跨线程访问对象
            //取消跨线程的访问造成的监视
            Control.CheckForIllegalCrossThreadCalls = false;
        }

        private void Form1_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (th!=null)
            {
                th.Abort();
            }
        }
    }
}
posted on 2019-02-06 12:07  豆皮没有豆  阅读(95)  评论(0)    收藏  举报