2017-5-4 进程 线程 用户控件

进程:一个应用程序就一个进程

线程:一个程序,有多个线程

(一)进程:关键词process

1.进程:一个应用程序就一个进程

  1.一个程序就是一个进程,在WinForm中可以做到打开其他应用程序[静态方法],例:

 Process.Start("calc");就是打开电脑上的计算器

  2.通过绝对路径打开电脑上的其它程序[普通方法]

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
     //预览
        private void button1_Click(object sender, EventArgs e)
        {
            openFileDialog1.Filter = "应用程序|*.exe";
            DialogResult dr = openFileDialog1.ShowDialog();
            textBox1.Text = openFileDialog1.FileName;
            if (dr == DialogResult.OK) 
            {
            
            }
            
        }
     //打开
        private void button2_Click(object sender, EventArgs e)
        {
            Process p = new Process();
            //开启的文件对象
            p.StartInfo = new ProcessStartInfo(textBox1.Text);
            p.Start();
        }
    }
}

  3.通过和LinkLabel配合,实现打开网页

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

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

        private void linkLabel1_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            Process.Start("http://www.baidu.com");
        }
       
    }
}

补充:

Process[] p=Process.GetProcesses();//获取全部进程

foreach( process  pp in p)//遍历关闭全部进程

{

   pp.kill();//关闭进程

}

(二)线程:

1.线程的含义:

  如果一段代码的执行需要时间,那么必须开启一个新线程来执行,
  如果不开线程,窗口会出现假死

2.多线程的应用:3个lable不停的循环,点击开始,循环开始,点击暂停循环关闭。

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.Windows.Forms;

namespace WindowsFormsApplication1
{
    public partial class Form2 : Form
    {
        public Form2()
        {
            InitializeComponent();
            //关闭监控
            Control.CheckForIllegalCrossThreadCalls = false;
        }

        List<string> slist = new List<string>();
        List<string> alist = new List<string>();
        List<string> blist = new List<string>();
        Thread th = null;
        Thread th1 = null;
        Thread th2 = null;
        private void button1_Click(object sender, EventArgs e)
        {
            slist.Add("小王");
            slist.Add("史蒂夫");
            slist.Add("乔布斯");
            slist.Add("雷军");
            slist.Add("刘能");
            slist.Add("小名");
            slist.Add("老王");

            alist.Add("马路上");
            alist.Add("厕所里");
            alist.Add("教室里");

            blist.Add("对异性说我爱你");
            blist.Add("亲吻同性");
            blist.Add("大笑3声");
            blist.Add("我大声说我错了");

            //实例化调用aa方法
            th = new Thread(aa);
            //设置后台线程
            th.IsBackground = true;
            //线程开始并传参数
            th.Start(slist);

            th1 = new Thread(bb);
            th1.IsBackground = true;
            th1.Start(alist);

            th2 = new Thread(cc);
            th2.IsBackground = true;
            th2.Start(blist);
        }
        Random r = new Random();
        public void aa(object s) 
        {
           
            List<string> aaa = s as List<string>;
            while (true)
            {
                label2.Text = aaa[r.Next(0,aaa.Count)].ToString();
                //间隔时间
                Thread.Sleep(10);
            }
        }
        public void bb(object s)
        {
            List<string> bb = s as List<string>;
            while (true) 
            {
                label3.Text = bb[r.Next(0, bb.Count)].ToString();
                //间隔时间
                Thread.Sleep(10);
            }
        }
        public void cc(object s) 
        {
            List<string> cc = s as List<string>;
            while (true) 
            {
                label7.Text = cc[r.Next(0, cc.Count)].ToString();
                Thread.Sleep(10);
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            //关闭线程
            th.Abort();
            th1.Abort();
            th2.Abort();
        }

        private void label2_Click(object sender, EventArgs e)
        {

        }
        /// <summary>
        /// 在程序关闭的时候关闭线程
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Form2_FormClosing(object sender, FormClosingEventArgs e)
        {
            //关闭线程
            th.Abort();
            th1.Abort();
            th2.Abort();
        }
    }
}
View Code

(三)用户控件

1.用户控件:

让用户用现有的控件自由组合成一个新的控件
方便某些功能统一管理

项目 - 添加新项 - 用户控件

2.用法:

  首先需要将要用的控件调成public公共的,才能调用,完成之后,点击生成解决方案,用户控件会出现,外形类似于无边框窗体

  

原文地址:https://www.cnblogs.com/qingnianxu/p/6810164.html