WinForms multi-thread programming lottery Program

Source: Internet
Author: User

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 ");
}
}
}
 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.