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 Thread_Test
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
Thread Thread_1;
Thread Thread_2;
Thread Thread_3;
bool App_Open = false;
private void Form1_Load(object sender, EventArgs e)
{
App_Open = true;
//
Thread_1 = new Thread(Running1);
Thread_1.Start();
Thread_2 = new Thread(Running2);
Thread_2.Start();
Thread_3 = new Thread(Running3);
Thread_3.Start();
}
private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
App_Open = false;
Thread.Sleep(100);
}
#region 方法1**标志位+执行位
bool Point_Do = false;
bool Point_Set = true;
private void Btn_Hand_Click(object sender, EventArgs e)
{
if (Point_Set == true)
{
Point_Set = false;
}
else
{
Point_Set = true;
}
Point_Do = true;
}
private void Running1()
{
while (App_Open == true)
{
Thread.Sleep(10);
try
{
if (Point_Do == true)
{
Point_Do = false;
//线程中要求改主窗体UI中的text属性
this.SetText(Point_Set);
}
}
catch (Exception ex)
{
string Info_Err = ex.ToString();
//---日志记录
}
}
}
//创建代理
delegate void SetTextCallback(bool Bool_1);
//调用窗体中的函数用invoke传递参数
private void SetText(bool Bool_1)
{
if (this.Btn_Result1.InvokeRequired)
{
SetTextCallback d = new SetTextCallback(SetText);
this.Invoke(d, new object[] { Bool_1 });
}
else
{
if (Bool_1 == true)
{
Btn_Result1.BackColor = Color.Green;
Btn_Result1.Text = "OK";
}
else
{
Btn_Result1.BackColor = Color.Red;
Btn_Result1.Text = "NG";
}
}
}
#endregion
#region 方法2**上升沿
bool Point_Do2 = true;
private void Btn_Hand2_Click(object sender, EventArgs e)
{
if (Point_Do2 == true)
{
Point_Do2 = false;
}
else
{
Point_Do2 = true;
}
}
private void Running2()
{
bool Point_X = false;
while (App_Open == true)
{
try
{
Thread.Sleep(10);
if (Point_X != Point_Do2)
{
Point_X = Point_Do2;
//线程中要求改主窗体UI中的text属性
this.SetText2(Point_X);
}
}
catch (Exception ex)
{
string Info_Err = ex.ToString();
//---日志记录
}
}
}
//创建代理
delegate void SetTextCallback2(bool Bool_1);
//调用窗体中的函数用invoke传递参数
private void SetText2(bool Bool_1)
{
if (this.Btn_Result1.InvokeRequired)
{
SetTextCallback2 d = new SetTextCallback2(SetText2);
this.Invoke(d, new object[] { Bool_1 });
}
else
{
if (Bool_1 == true)
{
Btn_Result2.BackColor = Color.Green;
Btn_Result2.Text = "OK";
}
else
{
Btn_Result2.BackColor = Color.Red;
Btn_Result2.Text = "NG";
}
}
}
#endregion
#region 方法3**简单
bool Point_Do3 = true;
private void Btn_Hand3_Click(object sender, EventArgs e)
{
if (Point_Do3 == true)
{
Point_Do3 = false;
}
else
{
Point_Do3 = true;
}
}
private void Running3()
{
while (App_Open == true)
{
Thread.Sleep(100);
try
{
Invoke(new Action(() =>
{
if (Point_Do3 == true)
{
Btn_Result3.Text = "OK";
Btn_Result3.BackColor = Color.Green;
}
else
{
Btn_Result3.Text = "NG";
Btn_Result3.BackColor = Color.Red;
}
}));
}
catch (Exception ex)
{
string Info_Err = ex.ToString();
//---日志记录
}
}
}
#endregion
}
}