实例介绍
using System;
using System.Collections.Generic;using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Threading;
namespace 循环
{
public partial class Form1 : Form
{
public delegate void listb(string msg1);
public listb listb1;
//public listb listb3;
//public listb listb4;
public delegate void tb(string msg2);
public tb tb1;
bool []m=new bool[100];
int l = 0;
string[] strArray1= new string[20];
Thread t1, t2; // 说明为窗体类成
public Form1()
{
InitializeComponent();
listb1 = new listb(UpdateTxtMethod1);
tb1 = new tb(UpdateTxtMethod2);
//listb listb2 = new listb(UpdateTxtMethod1);
//listb listb3 = new listb(UpdateTxtMethod2);
}
private void button1_Click(object sender, EventArgs e)
{
m[0] = false;
if (t2 != null && t2.ThreadState == ThreadState.Running)//判断线程是否处于运行状态
{
}
if (t2 != null && (t2.ThreadState & ThreadState.AbortRequested) != 0)//判断线程是否处于取消状态
{
}
if (t2 == null || (t2 != null && t2.IsAlive != true))
{
button1.Text = "线程启动中";
t2 = new Thread(new ThreadStart(BackgroundProcess));
t2.IsBackground = true;
t2.Start();
}
else
{
m[0] = true;
//t2.Abort();
button1.Text = "线程已停止";
}
//if (button1.Text != "线程启动中")
//{
// button1.Text = "线程启动中";
// t2 = new Thread(new ThreadStart(BackgroundProcess));
// t2.IsBackground = true;
// t2.Start(); //
//}
//else
//{
// t2.Abort();
// button1.Text = "线程已停止";
//}
//button1.Text = "线程启动";
//Thread objThread = new Thread(new ThreadStart(delegate
//{
// while (true)
// {
// Random random = new Random();
// int i = random.Next(10000);
// //Thread.Sleep(TimeSpan.FromSeconds(2));//暂停2S
// Thread.Sleep(100);
// this.BeginInvoke(listb1, i.ToString());
// }
//}));
//objThread.IsBackground = true;
//objThread.Start();
}
public void UpdateTxtMethod1(string msg1)
{
if (l < strArray1.Length)
{
strArray1[l] = msg1;
l ;
listBox1.Items.Clear();
for (int i = 0; i < l - 1; i )
{
listBox1.Items.Add(strArray1[l - 1 - i]);
}
}
else
{
Array.Copy(strArray1, 1, strArray1, 0, strArray1.Length - 1);
strArray1[strArray1.Length - 1] = msg1;
listBox1.Items.Clear();
for (int i = 0; i < strArray1.Length; i )
{
listBox1.Items.Add(strArray1[strArray1.Length - 1 - i]);
}
}
}
public void UpdateTxtMethod2(string msg2)
{
textBox1.AppendText(msg2 "\r\n");
textBox1.ScrollToCaret();
}
private void button2_Click(object sender, EventArgs e)
{
Thread objThread = new Thread(new ThreadStart(delegate
{
int n = 10;
this.BeginInvoke(tb1, "线程开始执行,执行" n "次,每一秒执行一次");
for (int i = 0; i < n; i )
{
this.BeginInvoke(tb1, i.ToString());
//一秒 执行一次
Thread.Sleep(1000);
}
this.BeginInvoke(tb1, "线程结束");
}));
objThread.IsBackground = true;
objThread.Start();
}
Thread th1;
//PictureBox[] picS = new PictureBox[8]; //
//ListBox lis1 = new ListBox();
TextBox tex1 = new TextBox();
//public delegate void tex(TextBox tttt );
//public tex tex1;
//public delegate void tb(string msg2);
//public tb tb1;
private void button3_Click(object sender, EventArgs e)
{
System.Windows.Forms.Control.CheckForIllegalCrossThreadCalls = false;//设置该属性 为false
//tex1 = textBox2;
//tex1.Text = "ssssss" ;
int content = 0;
int.TryParse(textBox1.Text.ToString(), out content); //表示将数字内容的字符串转为int类型,int.TryParse与 int.Parse类似,但它不会产生异常,转换成功返回 true,转换失败返回 false
textBox1.Text = content.ToString();
if (th1 == null)
{
th1 = new Thread(() =>
{
int m=0;
while (true)
{
Thread.Sleep(10);
textBox2.BeginInvoke(new Action(() =>
{
textBox2.AppendText("ssssss" m.ToString() "\r\n");
textBox2.ScrollToCaret();
m ;
}));
if (m > 1000)
{
break;
}
}
})
{ IsBackground = true };
th1.Start();
}
}
private void button4_Click(object sender, EventArgs e)
{
Thread objThread = new Thread(() =>
{
int m = 0;
Thread.Sleep(1);
textBox3.BeginInvoke(new Action(() =>
{
Thread.Sleep(100);
textBox3.AppendText("线程开始执行,执行" m "次,每一秒执行一次" "\r\n");
textBox3.ScrollToCaret();
while (true)
{
//textBox3.AppendText((10 - n).ToString() "\r\n");
//textBox3.ScrollToCaret();
//n--;
//if (n == 0)
//{
// textBox3.AppendText("线程结束" "\r\n");
// textBox3.ScrollToCaret();
// break;
//}
Thread.Sleep(100);
textBox3.BeginInvoke(new Action(() =>
{
textBox3.Text = "ssssss " m.ToString();
textBox3.ScrollToCaret();
}));
m ;
if (m > 100)
{
break;
}
}
}));
});
objThread.IsBackground = true;
objThread.Start();
//if (th1 == null)
//{
// th1 = new Thread(() =>
// {
// int n = 10;
// Thread.Sleep(1);
// while (true)
// {
// m ;
// Thread.Sleep(1);
// textBox2.BeginInvoke(new Action(() =>
// {
// if (m > 100)
// {
// m = 0;
// }
// textBox2.Text = "ssssss" m.ToString();
// }));
// }
// })
// { IsBackground = true };
// th1.Start();
// }
}
//private void Form1_Load(object sender, EventArgs e)
//{
// //t1 = new Thread(new ThreadStart(BackgroundProcess));
// //t1.Start();
//}
private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
if (t2!=null)
{
t2.Abort();
}
}
int ii = 0;
int iii = 0;
private void timer1_Tick(object sender, EventArgs e)
{
timer1.Stop();
textBox4.Text = ii.ToString();
if (ii<1000)
{
ii ;
}
else
{
ii = 0;
}
//for (int i = 0; i < 1000; i )
//{
// //timer1.Interval = 100;
// textBox4.Text = i.ToString();
//}
//timer1.Interval = 10000;
//for (int i =1000; i < 2000; i )
//{
// textBox4.Text = i.ToString();
//}
//timer1.Stop();
Thread.Sleep(1000);
timer1.Interval = 1;
timer1.Start();
//timer1.Enabled = false;
//try
//{
// //执行获取数据和绑定数据源的代码
// timer1.Interval = 6000;
//}
//catch
//{
// timer1.Interval = 30000;
//}
//finally
//{
// timer1.Enabled = true;
//}
}
private void button5_Click(object sender, EventArgs e)
{
timer1.Start();
timer2.Start();
}
private void button6_Click(object sender, EventArgs e)
{
}
private void button7_Click(object sender, EventArgs e)
{
}
private void button8_Click(object sender, EventArgs e)
{
}
private void timer2_Tick(object sender, EventArgs e)
{
timer2.Stop();
textBox5.Text = iii.ToString();
if (iii < 1000)
{
iii ;
}
else
{
iii = 0;
}
Thread.Sleep(100);
timer2.Interval = 1;
timer2.Start();
}
private void BackgroundProcess()
{
while (true)
{
Random random = new Random();
int i = random.Next(10000);
//Thread.Sleep(TimeSpan.FromSeconds(2));//暂停2S
Thread.Sleep(10);
this.BeginInvoke(listb1, i.ToString());
if (m[0]==true)
{
m[0] = false;
break;
}
}
}
}
好例子网口号:伸出你的我的手 — 分享!
小贴士
感谢您为本站写下的评论,您的评论对其它用户来说具有重要的参考价值,所以请认真填写。
- 类似“顶”、“沙发”之类没有营养的文字,对勤劳贡献的楼主来说是令人沮丧的反馈信息。
- 相信您也不想看到一排文字/表情墙,所以请不要反馈意义不大的重复字符,也请尽量不要纯表情的回复。
- 提问之前请再仔细看一遍楼主的说明,或许是您遗漏了。
- 请勿到处挖坑绊人、招贴广告。既占空间让人厌烦,又没人会搭理,于人于己都无利。
关于好例子网
本站旨在为广大IT学习爱好者提供一个非营利性互相学习交流分享平台。本站所有资源都可以被免费获取学习研究。本站资源来自网友分享,对搜索内容的合法性不具有预见性、识别性、控制性,仅供学习研究,请务必在下载后24小时内给予删除,不得用于其他任何用途,否则后果自负。基于互联网的特殊性,平台无法对用户传输的作品、信息、内容的权属或合法性、安全性、合规性、真实性、科学性、完整权、有效性等进行实质审查;无论平台是否已进行审查,用户均应自行承担因其传输的作品、信息、内容而可能或已经产生的侵权或权属纠纷等法律责任。本站所有资源不代表本站的观点或立场,基于网友分享,根据中国法律《信息网络传播权保护条例》第二十二与二十三条之规定,若资源存在侵权或相关问题请联系本站客服人员,点此联系我们。关于更多版权及免责申明参见 版权及免责申明
网友评论
我要评论