The code for a simple small program is as follows:
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. Threading;
Namespace exe_thread1
{
Public partial class Form2: Form
{
Public Form2 ()
{
InitializeComponent ();
}
// Number string
String [] str = {"15131254154", "15251247858", "15651244567 ",
"15344547254", "1551247732", "15661242345 ",
"15461237356", "15761247611", "15873457954 ",
"15571247357", "15071247430", "15571678004 ",
"15611247553 "};
// Generate random numbers
Random r = new Random ();
// Record string subscript
Int I;
// Define the thread
Thread myThread;
// Thread method
Private void Thread ()
{
While (true)
{
This. SetText ();
}
}
Private void button#click (object sender, EventArgs e)
{
// Instantiate the thread
MyThread = new Thread (new ThreadStart (this. Thread ));
// Start thread
MyThread. Start ();
}
// Define the delegate
Delegate void SetTextCallback ();
// Delegate Method
Private void SetText ()
{
If (this. textBox1.InvokeRequired)
{
SetTextCallback d = new SetTextCallback (SetText );
This. Invoke (d, new object [] {});
}
Else
{
I = r. Next (str. Length );
This. textBox1.Text = str [I];
}
}
Private void button2_Click (object sender, EventArgs e)
{
// End the thread
MyThread. Abort ();
MessageBox. Show ("Congratulations! ");
}
}
}