Multithreading
Connect to the Section multithreading learning: http://blog.csdn.net/iuhxq/archive/2005/10/12/500295.aspx
This section separates the form main thread from other threads, which enables data to be passed in from the thread
The code is as follows:
The From1.cs code is as follows:
Using System;
Using System.Drawing;
Using System.Collections;
Using System.ComponentModel;
Using System.Windows.Forms;
Using System.Data;
Using System.Threading;
namespace student
{
<summary>
Http://blog.csdn.net/iuhxq
</summary>
public class Form1:System.Windows.Forms.Form
{
Private System.Windows.Forms.Button button1;
Private System.Windows.Forms.Button button2;
Private System.Windows.Forms.Button Button3;
Private Class1 C;
Private System.Windows.Forms.ListView ListView1;
Private System.Windows.Forms.ColumnHeader ColumnHeader1;
Private System.Windows.Forms.ColumnHeader ColumnHeader2;
<summary>
The required designer variable.
</summary>
Private System.ComponentModel.Container components = null;
Public Form1 ()
{
//
Required for Windows Forms Designer support
//
InitializeComponent ();
//
TODO: Add any constructor code after the InitializeComponent call
//
}
<summary>
Clean up all resources that are in use.
</summary>
protected override void Dispose (bool disposing)
{
if (disposing)
{
if (Components!= null)
{
Components. Dispose ();
}
}
Base. Dispose (disposing);
}
Code generated #region the Windows forms Designer
<summary>
Designer supports required methods-do not use the Code editor to modify
The contents of this method.
</summary>
private void InitializeComponent ()
{
//
CLASS1 initialization
//
THIS.C = new Class1 ();
This.c.update + = new student. Class1.eventhandler (c_update);
This.button1 = new System.Windows.Forms.Button ();
This.button2 = new System.Windows.Forms.Button ();
This.button3 = new System.Windows.Forms.Button ();
This.listview1 = new System.Windows.Forms.ListView ();
This.columnheader1 = new System.Windows.Forms.ColumnHeader ();
This.columnheader2 = new System.Windows.Forms.ColumnHeader ();
This. SuspendLayout ();
//
Button1
//
This.button1.Location = new System.Drawing.Point (24, 216);
This.button1.Name = "Button1";
This.button1.TabIndex = 1;
This.button1.Text = "ADD";
This.button1.Click + = new System.EventHandler (This.button1_click);
//
Button2
//
This.button2.Location = new System.Drawing.Point (104, 216);
This.button2.Name = "Button2";
This.button2.TabIndex = 2;
This.button2.Text = "Del";
This.button2.Click + = new System.EventHandler (This.button2_click);
//
Button3
//
This.button3.Location = new System.Drawing.Point (184, 216);
This.button3.Name = "Button3";
This.button3.TabIndex = 3;
This.button3.Text = "Delall";
//
ListView1
//
This.listView1.Columns.AddRange (new system.windows.forms.columnheader[] {
This.columnheader1,
This.columnheader2});
This.listView1.FullRowSelect = true;
This.listView1.GridLines = true;
This.listView1.Location = new System.Drawing.Point (0, 0);
This.listView1.Name = "ListView1";
This.listView1.Size = new System.Drawing.Size (288, 208);
This.listView1.TabIndex = 5;
This.listView1.View = System.Windows.Forms.View.Details;
//
ColumnHeader1
//
This.columnHeader1.Text = "thread number";
This.columnHeader1.Width = 81;
//
ColumnHeader2
//
This.columnHeader2.Text = "value";
This.columnHeader2.Width = 180;
//
Form1
//
This. AutoScaleBaseSize = new System.Drawing.Size (6, 14);
This. ClientSize = new System.Drawing.Size (292, 266);
This. Controls.Add (THIS.LISTVIEW1);
This. Controls.Add (This.button3);
This. Controls.Add (This.button2);
This. Controls.Add (This.button1);
This. Name = "Form1";
This. Text = "Form1";
This. Load + = new System.EventHandler (this. Form1_Load);
This. ResumeLayout (FALSE);
}
#endregion
<summary>
The main entry point for the application.
</summary>
[STAThread]
static void Main ()
{
Application.Run (New Form1 ());
}
private void Form1_Load (object sender, System.EventArgs e)
{
}
private void c_update (int index)
{
Listview1.items[index]. SUBITEMS[1]. Text = C.result[index]. ToString ();
}
private void Button1_Click (object sender, System.EventArgs e)
{
LISTVIEW1.ITEMS.ADD (New ListViewItem (New String[]{c.result.count.tostring (), "0"}));
C.add ();
}
private void Button2_Click (object sender, System.EventArgs e)
{
int index = C.del ();
if (index>=0)
{
ListView1.Items.RemoveAt (index);
}
}
}
}
Add Class Class1:
The CLASS1 code is as follows:
Using System;
Using System.Collections;
Using System.Threading;
namespace student
{
<summary>
Summary description of the CLASS1.
</summary>
public class Class1
{
Public ArrayList Threads = new ArrayList ();
Public ArrayList result = new ArrayList ();
public event EventHandler Update;
public delegate void EventHandler (int index);
Public Class1 ()
{
//
Todo:http://blog.csdn.net/iuhxq
//
Update + = new EventHandler (class1_update);
}
private void Process ()
{
int i = 1;
while (true)
{
i++;
int index = Threads.indexof (Thread.CurrentThread);
Result[index] = i;
if (update!=null) Update (index);
Thread.Sleep (0);
}
}
public int Add ()
{
Thread t = new Thread (new ThreadStart (Process));
T.start ();
Threads.add (t);
Lock (Result)
{
Result.add (0);
}
return threads.count-1;
}
public int Del ()
{
int count = Threads.count;
if (count>0)
{
Thread T1 = (thread) threads[count-1];
if (t1. IsAlive)
{
T1. Abort ();
}
Threads.removeat (count-1);
Lock (Result)
{
Result.removeat (count-1);
}
}
return count-1;
}
private void class1_update (int index)
{
}
}
}