线程示例

启动记事本

 class Program
    {
        
        static void Main(string[] args)
        {
            Process process = new Process();
            process.StartInfo.FileName = "notepad.exe";
            try
            {
                Console.WriteLine("进程运行中");
                process.Start();
            }
            catch (Exception ex)
            {
                Console.WriteLine("失败:" + ex.ToString());
            }
            finally
            {
                process = null;
            }
        }
    }

 输出任务管理器内容

 class Program
    {

        static void Main(string[] args)
        {
            Process[] ps;
            ps = Process.GetProcesses();
            try
            {
                Console.WriteLine("进程输出信息");
                foreach (Process p in ps)
                {
                    Console.WriteLine(p.ProcessName);
                }
            }
            catch (Exception ex)
            {

                Console.WriteLine(ex.Message); ;
            }
            finally
            {
                ps = null;
            }
        }
             
    }

 任务管理器的设置

public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
           
            ProccLoad();
        }
        public void ProccLoad()
        {
            Process[] ps;
            ps = Process.GetProcesses();
            listView1.Items.Clear();
            try
            {
                foreach (Process p in ps)
                {
                    ListViewItem lvi = new ListViewItem();
                    lvi.Text = p.ProcessName;
                    lvi.SubItems.AddRange(new string[] { p.Id.ToString(), p.BasePriority.ToString(), p.WorkingSet64.ToString()+" K"});
                    listView1.Items.Add(lvi);
                }
                listView1.Items[0].Selected = true;
                this.label1.Text = "总进程数:"+listView1.Items.Count.ToString();
            }
            catch (Exception ex)
            {

                MessageBox.Show(ex.Message);
            }
            finally
            {
                ps = null;
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            KillProcess(int.Parse(this.listView1.SelectedItems[0].SubItems[1].Text));
            button1.PerformClick();
        }
        public void KillProcess(int id)
        {
            if (this.listView1.SelectedItems.Count>=1)
            {
                Process p = Process.GetProcessById(id);
                if (MessageBox.Show("确定结束进程吗?","提示",MessageBoxButtons.OKCancel,MessageBoxIcon.Question,MessageBoxDefaultButton.Button2)==DialogResult.OK)
                {
                    p.Kill();
                   
                }
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            ProccLoad();
        }


}

 多线程运行

 class Program
    {

        static void Main(string[] args)
        {
            //多线程是轮流占有CPU时间,无法控制占用的时间,只有设置优先级使线程获得的CPU资源和其他资源不相同
            Thread.CurrentThread.Name = "主线程";
            Thread th = new Thread(Demo);
            th.Name = "子线程A:";
            th.Start();
            th.Join();
            ThreadStart ts = new ThreadStart(Demo);
            Thread th1 = new Thread(ts);
            th1.Name = "子线程B:";
            th1.Start();
            Demo();
        }
        static void Demo()
        {
            for (int i = 0; i < 10; i++)
            {
                Console.WriteLine("线程名"+Thread.CurrentThread.Name+i.ToString());
            }
        }
             
    }

 线程同步

 class Program
    {

        static void Main(string[] args)
        {
            //多线程是轮流占有CPU时间,无法控制占用的时间,只有设置优先级使线程获得的CPU资源和其他资源不相同
            Thread.CurrentThread.Name = "主线程";
            Thread th = new Thread(Demo);
            th.Name = "子线程A:";
            th.Start();
            Demo();
        }
        static void Demo()
        {
            lock (typeof(Program))
            {
                for (int i = 1; i < 10; i++)
                {
                    Console.WriteLine(Thread.CurrentThread.Name+i);
                }
            }
        }
             
    }

 进程的轮流

 class Program
    {

        static void Main(string[] args)
        {
            Thread.CurrentThread.Name = "主线程";
            ParameterizedThreadStart pt = new ParameterizedThreadStart(Demo);
            Thread th = new Thread(pt);
            th.Name = "子线程:";
            th.Start(10);
            th.Join();
            Thread th1 = new Thread(new ParameterizedThreadStart(Demo));
            th1.Name = "子线程二:";
            th1.Start(10);
            th.Join();
            th1.Join();
            Demo(10);

        }
        static void Demo(object number)
        {
            for (int i = 1; i < (int)number; i++)
            {
                Console.WriteLine(Thread.CurrentThread.Name + i.ToString());
            }
        }

             
    }

 匿名方法

        static void Main(string[] args)
        {
            Thread th = new Thread(delegate()
            {
                for (int i = 0; i < 10; i++)
                {
                    Console.WriteLine("线程一");
                }
            });
            th.Start();
        }

 

posted @ 2014-03-20 20:40  bradleydan  阅读(135)  评论(0)    收藏  举报