110,534
社区成员
发帖
与我相关
我的任务
分享
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;
using System.Collections.Generic;
namespace TestData
{
public partial class Form1 : Form
{
//线程参数
private Thread read_thread; //数据读线程
private Thread datahandler_thread; //数据处理
//数据存储列表
List<string> aAutoRead = new List<string>();
List<string> bAutoRead = new List<string>();
//变量
int aCount = 0; //A类型数据个数
int bCount = 0; //B类型数据个数
double aValue = 0;
double bValue = 0;
bool a_Start = false;
bool b_Start = false;
public Form1()
{
InitializeComponent();
Init();
}
public void Init()
{
read_thread = new Thread(ReadCycle);
read_thread.IsBackground = true;
read_thread.Priority = ThreadPriority.BelowNormal;
read_thread.Start();
datahandler_thread = new Thread(DataProcess);
datahandler_thread.IsBackground = true;
datahandler_thread.Priority = ThreadPriority.BelowNormal;
datahandler_thread.Start();
}
public delegate void UpdateText(string data, string labtext);
public void ReadCycle()
{
while (true)
{
Thread.Sleep(10);
if (a_Start)
{
aAutoRead.Add(aValue.ToString("000.0"));
aValue += 0.2;
}
if (b_Start)
{
bAutoRead.Add(bValue.ToString("000.0"));
bValue += 0.2;
}
}
}
public void DataProcess()
{
while (true)
{
Thread.Sleep(50);
if (aAutoRead.Count > 0)
{
if (aAutoRead == null)
continue;
CycleUpdateGuiDisplay(aAutoRead[aCount++], labelA.Text);
}
if (bAutoRead.Count > 0)
{
if (bAutoRead == null)
continue;
CycleUpdateGuiDisplay(bAutoRead[bCount++], labelB.Text);
}
}
}
public void CycleUpdateGuiDisplay(string data, string labtext)
{
UpdateText labelText = new UpdateText(UpdateTextMethod);
BeginInvoke(labelText, new object[] { data, labtext });
}
public void UpdateTextMethod(string data, string labtext)
{
if (tabControl1.SelectedTab == tabPage1 && labtext == labelA.Text)
labelA.Text = data;
if (tabControl1.SelectedTab == tabPage2 && labtext == labelB.Text)
labelB.Text = data;
}
private void button1_Click(object sender, EventArgs e)
{
if (button1.Text == "开始")
a_Start = true;
}
private void button2_Click(object sender, EventArgs e)
{
if (button2.Text == "开始")
b_Start = true;
}
}
}
using System;
using System.Collections.Generic;
using System.Threading;
using System.Windows.Forms;
namespace WindowsFormsApplication3
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
Init();
}
private List<string> AList = new List<string>();
private List<string> BList = new List<string>();
private void Init()
{
AList.Add(label1.Text);
AList.Add(label2.Text);
AList.Add(label3.Text);
AList.Add(label4.Text);
AList.Add(label5.Text);
AList.Add(label6.Text);
BList.Add(label7.Text);
BList.Add(label8.Text);
BList.Add(label9.Text);
BList.Add(label10.Text);
BList.Add(label11.Text);
BList.Add(label12.Text);
}
private void btnAstart_Click(object sender, EventArgs e)
{
UpdateControl(label15, AList, AList.Count);
}
private void btnBstart_Click(object sender, EventArgs e)
{
UpdateControl(label16, AList, AList.Count);
}
private void UpdateControl(Control c, List<string> typeList, int listCount)
{
int labTime = 0;
double value = 0;
double time = Convert.ToDouble(typeList[labTime]);
ThreadPool.QueueUserWorkItem(delegate
{
while (listCount > 0)
{
Thread.Sleep(50);
value += 0.2;
string content = value.ToString("000.0");
this.BeginInvoke((EventHandler) delegate { c.Text = content; });
if (time == Convert.ToDouble(content))
{
listCount--;
if (listCount >= 1)
{
labTime++;
time += Convert.ToDouble(typeList[labTime]);
Thread.Sleep(3000);
}
else
{
MessageBox.Show("完成!退出!显示提示框!");
}
}
}
});
}
}
}