Simulate a computer lottery program using multiple threads ,. Click scroll number to start the thread and display the background phone number cyclically. Click the lottery button to close the thread. The phone number displayed in the text box is the winning number.
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 Ex02_Lottery
{
Public partial class Form1: Form
{
Public Form1 ()
{
InitializeComponent ();
}
// Define a generic
List <string> liNum = new List <string> ();
// Define a global variable
Thread thread;
Private void btnRoll_Click (object sender, EventArgs e)
{
// Define a thread
Thread = new Thread (new ThreadStart (Num ));
// Enable the thread
Thread. Start ();
BtnRoll. Enabled = false;
}
Public void Num ()
{
Int I = 0;
LiNum. Add ("13965113141 ");
LiNum. Add ("18676768761 ");
LiNum. Add ("13456468141 ");
LiNum. Add ("15456564541 ");
LiNum. Add ("13965113141 ");
LiNum. Add ("13968766141 ");
LiNum. Add ("13965113141 ");
LiNum. Add ("13123113311 ");
// Loop
While (I <liNum. Count + 1)
{
If (I> = liNum. Count) I = 0;
TxtNum. Text = liNum [I]. ToString ();
I ++;
}
}
Private void Form1_Load (object sender, EventArgs e)
{
TxtNum. Enabled = false;
// The Inter-thread operation is invalid: It is accessed by a thread that does not create the control "btnRoll. Solution
Form1.checkforillegalcrossthreadcils = false;
}
Private void btnLottery_Click (object sender, EventArgs e)
{
// Hanging thread
Thread. Suspend ();
// Restore the thread
Thread. Resume ();
// Close the thread
Thread. Abort ();
BtnLottery. Enabled = false;
MessageBox. Show ("number:" + txtNum. Text + "Congratulations, you have won the prize", "message prompt ");
}
}
}