WinForm 进程 ,线程

进程:一个进程,代表一个程序

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

一.进程  关键词Process

Process既拥有普通方法,也拥有静态方法

静态方法关键字:static

非静态方法需要实例化对象,通过对象来点出方法

静态方法,不需要实例化对象,直接通过类名点出方法

 (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 WindowsFormsApplication5
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        //选择按钮点击事件
        private void button1_Click(object sender, EventArgs e)
        {
            //设置智能打开.exe型应用程序
            openFileDialog1.Filter = "应用程序|*.exe";
            //设置先打开要选择路径的控件
            DialogResult dr = openFileDialog1.ShowDialog();
            if (dr == DialogResult.OK)
            {
                //将程序的绝对路径展示到textBox1中
                textBox1.Text = openFileDialog1.FileName;
            }
        }
        //打开按钮点击事件
        private void button2_Click(object sender, EventArgs e)
        {
            //实例化Process的普通方法
            Process p = new Process();
            //获取p需要到的据对路径
            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();

}

二.线程  关键词Thread

为什么要使用多线程操作:一个窗体,在创建的时候,默认会生成一个主线程,这个主线程会操作窗体的移动等操作,在执行需要等待时间的方法时,如果没有多线程操作,窗体是无法进行移动的,并且关闭之后,方法还会继续运行,会引起程序故障.因此使用多线程操作.[不能让主线程进行需要等待时间的操作]

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 WindowsFormsApplication5
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            //关闭线程监控,防止报错
            Control.CheckForIllegalCrossThreadCalls = false;
        }

//需要对窗体设置关闭事件,当关闭窗体时,关闭所有进程,避免后台线程正在运行时,关闭窗体,造成报错

            private void Form1_FormClosing(object sender, FormClosingEventArgs e)
           {
           //关闭线程
            td1.Abort();
            td2.Abort();
            td3.Abort();
            }

        Thread td1 = null;
        Thread td2 = null;
        Thread td3 = null;
        private void button1_Click(object sender, EventArgs e)
        {
            //实例化线程并委托
            td1 = new Thread(tt1);
            td2 = new Thread(tt2);
            td3 = new Thread(tt3);
            //设置线程为后台线程
            td1.IsBackground = true;
            td2.IsBackground = true;
            td3.IsBackground = true;
           //数组a2,操作有参无返
            string[] a2 = new string[] { "哈哈", "嘿嘿", "嘻嘻", "哼哼", "恩恩", "咦咦" };
            //开启线程
            td1.Start();
            td2.Start(a2);
            td3.Start();
        }
        public void tt1()
        {
            //死循环,循环展示数组,间隔200毫秒
            string[] a1 = new string[] { "", "", "", "", "", "" };
            while (true)
            {
                foreach (string ax1 in a1)
                {
                    label1.Text = ax1.ToString();
                    Thread.Sleep(200);
                }
            }
        }
        //默认方法是无参数,无返回值的,这里也可以传入参数,只能传入基类
        //
        public void tt2(object s)
        {
            //死循环,循环展示数组,间隔200毫秒    
            string[] a22 = s as string[];
            while (true)
            {
                foreach (string ax2 in a22)
                {
                    label2.Text = ax2.ToString();
                    Thread.Sleep(200);
                }
            }
        }
        public void tt3()
        {
            //死循环,循环展示数组,间隔200毫秒
            string[] a3 = new string[] { "可口可乐", "怡宝", "佳得乐", "脉动", "雪碧", "芬达" };
            while (true)
            {
                foreach (string ax3 in a3)
                {
                    label3.Text = ax3.ToString();
                    Thread.Sleep(200);
                }
            }
        }
        private void button2_Click(object sender, EventArgs e)
        {
            //只有在按下Button1才能按Button2
            if (td1 != null && td2 != null && td3 != null)
            {
                //关闭线程
                td1.Abort();
                td2.Abort();
                td3.Abort();
            }
        }
    }
}
原文地址:https://www.cnblogs.com/zhangxin4477/p/6806153.html