程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> .NET網頁編程 >> C# >> C#入門知識 >> winform基於異步拜托完成多線程搖獎器

winform基於異步拜托完成多線程搖獎器

編輯:C#入門知識

winform基於異步拜托完成多線程搖獎器。本站提示廣大學習愛好者:(winform基於異步拜托完成多線程搖獎器)文章只能為提供參考,不一定能成為您想要的結果。以下是winform基於異步拜托完成多線程搖獎器正文


本文實例講述了winform基於異步拜托完成多線程搖獎器。分享給年夜家供年夜家參考。詳細完成辦法以下:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace _08_搖獎機
{
//創立六個invoke辦法(控件挪用的時刻的拜托,用來給lable控件賦值)
public delegate void MyDelegate1(int num);
public delegate void MyDelegate2(int num);
public delegate void MyDelegate3(int num);
public delegate void MyDelegate4(int num);
public delegate void MyDelegate5(int num);
public delegate void MyDelegate6(int num);
public partial class Form1 : Form
{
public MyDelegate1 md1;
public MyDelegate1 md2;
public MyDelegate1 md3;
public MyDelegate1 md4;
public MyDelegate1 md5;
public MyDelegate1 md6;
//此拜托用來作異步拜托,旨在讓clr主動創立另外一個線程來完成主線程要做的操作,以減緩主線程的壓力
public delegate void MyDelegate(bool b);
public Form1()
{
InitializeComponent();
md1 = SetLable1;
md2 = SetLable2;
md3 = SetLable3;
md4 = SetLable4;
md5 = SetLable5;
md6 = SetLable6;
}
//用來寄存子線程對象
private Thread nameThread;
private int id;
private void button1_Click(object sender, EventArgs e)
{
MyDelegate md = new MyDelegate(this.SetNumberData);;
if (button1.Text.Trim()=="開端")
{
button1.Text = "停滯";
//挪用異步拜托,就是在另外一個線程中履行此拜托綁定的辦法
IAsyncResult result = md.BeginInvoke(true,null, null);
}
else
{
button1.Text = "開端";
//停滯的話就相當於終止子線程
nameThread.Abort();
}

//起首要想清晰 要給主線程的空間lable賦值,那末就必需是主線程干的事

//md.EndInvoke(result);
}

public void SetNumberData(bool b)
{
while (b==true)
{
List<int> listNum = new List<int>();
Random random = new Random();
//隨機生成6個數
while (listNum.Count <= 6)
{
int n = random.Next(0, 10);
listNum.Add(n);
}
//不是創立此控件的線程挪用此控件的時刻就必需挪用invoke辦法
if (this.label1.InvokeRequired)
{
this.Invoke(md1, listNum[0]);
}
else
{
label1.Text = listNum[0].ToString();
}
if (this.label2.InvokeRequired)
{
this.Invoke(md2, listNum[1]);
}
else
{
label2.Text = listNum[1].ToString();
}
if (this.label3.InvokeRequired)
{
this.Invoke(md3, listNum[2]);
}
else
{
label3.Text = listNum[2].ToString();
}
if (this.label4.InvokeRequired)
{
this.Invoke(md4, listNum[3]);
}
else
{
label4.Text = listNum[3].ToString();
}
if (this.label5.InvokeRequired)
{
this.Invoke(md5, listNum[4]);
}
else
{
label5.Text = listNum[4].ToString();
}
if (this.label6.InvokeRequired)
{
this.Invoke(md6, listNum[5]);
}
else
{
label6.Text = listNum[5].ToString();
}
//記載下以後的線程對象,以便於在點擊停滯按鈕的時刻終止此線程
nameThread = Thread.CurrentThread;
Thread.Sleep(300);
}
//id=Thread.CurrentThread.ManagedThreadId;

}

public void SetLable1(int n)
{
label1.Text = n.ToString();
}
public void SetLable2(int n)
{
label2.Text = n.ToString();
}
public void SetLable3(int n)
{
label3.Text = n.ToString();
}
public void SetLable4(int n)
{
label4.Text = n.ToString();
}
public void SetLable5(int n)
{
label5.Text = n.ToString();
}
public void SetLable6(int n)
{
label6.Text = n.ToString();
}
}

法式運轉成果以下所示:

願望本文所述對年夜家的C#法式設計有所贊助。

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved