zoukankan      html  css  js  c++  java
  • 进程和线程的简单实例

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

    namespace TestOfProcess
    {
    public partial class Form1 : Form
    {
    Thread thread1;
    int num = 0;
    bool isRun=true;
    public Form1()
    {
    InitializeComponent();
    thread1 = new Thread(new ThreadStart(thread1CallBack));

    }
    public void thread1CallBack()
    {
    while(true)
    while (isRun)
    {
    num++;
    Thread.Sleep(1000);
    }
    }
    private void button1_Click(object sender, EventArgs e)
    {
    Process process1 = new Process();
    process1.StartInfo.FileName = "notepad.exe";
    //启动Notepad.exe进程.
    process1.Start();

    }

    private void button2_Click(object sender, EventArgs e)
    {
    Process[] process;
    process = Process.GetProcessesByName("notepad");
    foreach(Process instance in process)
    {
    instance.WaitForExit(1000);
    instance.CloseMainWindow();

    }
    }

    private void button3_Click(object sender, EventArgs e)
    {
    listBox1.Items.Clear();
    //创建Process类型的数组,并将它们与系统内所有进程相关联
    Process[] processes;
    processes = Process.GetProcesses();
    foreach (Process p in processes)
    {
    //Idle指显示CPU空闲率的进程名称
    //由于访问Idle的StartTime会出现异常,所以将其排除在外
    if (p.ProcessName != "Idle")
    {
    //将每个进程名和进程开始时间加入listBox1中
    this.listBox1.Items.Add(
    string.Format("{0,-30}{1:h:m:s}", p.ProcessName, p.StartTime));
    }
    }

    }

    private void button4_Click(object sender, EventArgs e)
    {
    thread1.Start();
    }

    private void timer1_Tick(object sender, EventArgs e)
    {
    richTextBox1.AppendText(num.ToString());
    }

    private void checkBox1_CheckedChanged(object sender, EventArgs e)
    {
    isRun = (sender as CheckBox).Checked;
    }
    }
    }


    工程文件

        
    作者:wanglei_wan
        
    本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。
  • 相关阅读:
    Java的HttpRequest请求工具类
    js改变this指向的三种方式call() apply() bind()
    CSS3——弹性盒模型-flex——子级属性
    CSS3——弹性盒模型-flex——父级属性
    CSS3——文本延申扩展系列、多列
    CSS3——IE6混杂盒模型(box-sizing)
    CSS3——@font-face(文字字体包)
    CSS3——文本延申扩展系列—text-shadow
    CSS3—— border-radius
    CSS3——border-image
  • 原文地址:https://www.cnblogs.com/because/p/2416012.html
Copyright © 2011-2022 走看看