这有一个简单的例程,希望对你有一点感性的帮助。
其实还有一个更好一点的。功能是取某url的html码,
虽然用别的实现很简单,几句话,但是为了处理多
进程还是多费了一点代码。该代码没光盘,我一边学
一边敲到电脑上了,可惜现在不知哪里去了。
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Threading;namespace MutexSample
{
/// <summary>
/// Summary description for Form1.
/// </summary>
public class MainFoem : System.Windows.Forms.Form
{
static bool AllowRunning;
private System.Threading.Thread t1;
private System.Threading.Thread t2;
private System.Windows.Forms.Button Start;
private System.Windows.Forms.Label label5; 
/// <summary>
/// Required designer variable.
/// 
       // public System.Threading.Mutex myMutex;
private System.Windows.Forms.TextBox InfoBox;
private System.Windows.Forms.Button Stop;
/// </summary>
private System.ComponentModel.Container components = null; public MainFoem()
{
//
// Required for Windows Form Designer support
//
InitializeComponent(); //
AllowRunning=true;
Stop.Enabled=false;
Start.Enabled=true;

} /// <summary>
/// Clean up any resources being used.
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null) 
{
components.Dispose();
}
}
base.Dispose( disposing );
} #region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.InfoBox = new System.Windows.Forms.TextBox();
this.Start = new System.Windows.Forms.Button();
this.label5 = new System.Windows.Forms.Label();
this.Stop = new System.Windows.Forms.Button();
this.SuspendLayout();
// 
// InfoBox
// 
this.InfoBox.BackColor = System.Drawing.SystemColors.HighlightText;
this.InfoBox.ForeColor = System.Drawing.Color.FromArgb(((System.Byte)(0)), ((System.Byte)(0)), ((System.Byte)(64)));
this.InfoBox.Location = new System.Drawing.Point(72, 93);
this.InfoBox.Multiline = true;
this.InfoBox.Name = "InfoBox";
this.InfoBox.ReadOnly = true;
this.InfoBox.Size = new System.Drawing.Size(358, 185);
this.InfoBox.TabIndex = 9;
this.InfoBox.Text = "";
// 
// Start
// 
this.Start.Location = new System.Drawing.Point(440, 93);
this.Start.Name = "Start";
this.Start.Size = new System.Drawing.Size(96, 26);
this.Start.TabIndex = 0;
this.Start.Text = "线程开始";
this.Start.Click += new System.EventHandler(this.OnStart);
// 
// label5
// 
this.label5.Font = new System.Drawing.Font("Microsoft Sans Serif", 18F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
this.label5.ForeColor = System.Drawing.Color.Red;
this.label5.Location = new System.Drawing.Point(113, 28);
this.label5.Name = "label5";
this.label5.Size = new System.Drawing.Size(297, 37);
this.label5.TabIndex = 1;
this.label5.Text = "线程运行演示程序";
// 
// Stop
// 
this.Stop.Enabled = false;
this.Stop.Location = new System.Drawing.Point(440, 139);
this.Stop.Name = "Stop";
this.Stop.Size = new System.Drawing.Size(96, 26);
this.Stop.TabIndex = 10;
this.Stop.Text = "线程停止";
this.Stop.Click += new System.EventHandler(this.Stop_Click);
// 
// MainFoem
// 
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.BackColor = System.Drawing.Color.FromArgb(((System.Byte)(192)), ((System.Byte)(192)), ((System.Byte)(255)));
this.ClientSize = new System.Drawing.Size(552, 292);
this.Controls.AddRange(new System.Windows.Forms.Control[] {
  this.Stop,
  this.InfoBox,
  this.label5,
  this.Start});
this.ForeColor = System.Drawing.Color.FromArgb(((System.Byte)(0)), ((System.Byte)(0)), ((System.Byte)(192)));
this.MaximizeBox = false;
this.Name = "MainFoem";
this.Text = "ThreadDemo";
this.Load += new System.EventHandler(this.MainFoem_Load);
this.Closed += new System.EventHandler(this.OnClose);
this.ResumeLayout(false); }
#endregion /// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main() 
{
Application.Run(new MainFoem());
}
public void thread1Start( )

Mutex mutex1=new Mutex(false,"Name");
while(AllowRunning)
{
mutex1.WaitOne(); 
MessageBox.Show("线程1正在运行!");
InfoBox.Text+="线程1正在运行!\r\n";
Thread.Sleep(2000);
// Monitor.Exit(myMutex);
mutex1.ReleaseMutex(); 
Thread.Sleep(7500);
}
return; } public void thread2Start( )
{
Mutex mutex1=new Mutex(false,"Name");
while(AllowRunning)
{
mutex1.WaitOne();
MessageBox.Show("线程2正在运行!");
InfoBox.Text+="线程2正在运行!\r\n";
Thread.Sleep(2000);
mutex1.ReleaseMutex();
Thread.Sleep(3000);
}
return; }
private void OnStart(object sender, System.EventArgs e)
{
 
AllowRunning=true; MainFoem tm = new MainFoem( );

t1 = new Thread(new ThreadStart(tm.thread1Start));
InfoBox.Text+="线程开始运行!\r\n.";
t1.Start();

t2 = new Thread(new ThreadStart(tm.thread2Start));
t2.Start();
Start.Enabled=false;
Stop.Enabled=true;



}   private void OnClose(object sender, System.EventArgs e)
{
if(t1!=null) 
   if(t1.IsAlive) 
      t1.Abort();
if(t2!=null)
    if(t2.IsAlive)
       t2.Abort();
} private void Stop_Click(object sender, System.EventArgs e)
{
AllowRunning=false;
InfoBox.Text+="线程正在停止!\r\n.";
Start.Enabled=true;
Stop.Enabled=false;

} private void MainFoem_Load(object sender, System.EventArgs e)
{

}
}
}