多线程窗体切换
while (true)//表示不停重复执行这个代码片段
{
if (form != null)
{
form.ShowDialog();//当前窗口的起始点,只要reForm窗体类发生改变,就从重复执行代码段
form = form.reForm;
}
}
关键代码见红色处
public partial class Main : newForm { System.Timers.Timer _timer10 = new System.Timers.Timer(); System.Timers.Timer _timer30 = new System.Timers.Timer(); Thread threadInit; string sError = ""; static int index = 0;//显示页数 byte[] pc = new byte[512]; byte[] byteArray;//图片名字存储为数组 int numCheck;//判断是否收到汽车图片 public Main() { InitializeComponent(); } private void btn_chaxun_Click(object sender, EventArgs e) { if (btn_chaxun.Text == "停止查询") { btn_shng.Enabled = false; btn_shng.BackColor = System.Drawing.Color.SlateGray; btn_xia.Enabled = false; btn_xia.BackColor = System.Drawing.Color.SlateGray; btn_chaxun.Text = "开始查询"; groupBox5.Visible = false; groupBox4.Visible = true; } else { btn_shng.Enabled = true; btn_shng.BackColor = System.Drawing.Color.Blue; btn_xia.Enabled = true; btn_xia.BackColor = System.Drawing.Color.Blue; btn_chaxun.Text = "停止查询"; groupBox5.Visible = true; groupBox4.Visible = false; DataTable dtSelect = HNGovernment.DAL.Main.GetMaxData(out sError); if (dtSelect.Rows.Count > 0) { lab4InTime.Text = dtSelect.Rows[0]["DiscriDate"].ToString(); lab4InPlace.Text = DalEnum.GetAppConfig(dtSelect.Rows[0]["DoorCode"].ToString()); lab4ElectronCode.Text = dtSelect.Rows[0]["ElectronCode"].ToString(); lab4CarCode.Text = dtSelect.Rows[0]["CarCode"].ToString(); lab4InFlag.Text = DalEnum.GetAppConfig(dtSelect.Rows[0]["OutFlag"].ToString()); byte[] byteArray2 = System.Text.Encoding.Default.GetBytes(dtSelect.Rows[0]["ElectronCode"].ToString() + ".jpg"); int numCheck2 = CarDeviceDll.GetPic_From_Server_IDI(byteArray2, pc, 512); panel5.BackgroundImage = numCheck2 == 0 ? Image.FromFile(Encoding.Default.GetString(pc).TrimEnd('\0')) : Image.FromFile(Application.StartupPath + "\\NoPicture.jpg"); if (dtSelect.Rows[0]["DoorAuthority"].ToString() == "Y") groupBox5.BackColor = System.Drawing.Color.Green; if (dtSelect.Rows[0]["DoorAuthority"].ToString() == "N") groupBox5.BackColor = System.Drawing.Color.Red; if (dtSelect.Rows[0]["DoorAuthority"].ToString() == "H") groupBox5.BackColor = System.Drawing.Color.Black; } } } private void btn_shng_Click(object sender, EventArgs e) { ++index; DataTable dt = HNGovernment.DAL.Main.GetUpperDownData(out sError, index); if (dt.Rows.Count > 0) { for (int i = 0; i < dt.Rows.Count; i++) { lab4InTime.Text = dt.Rows[0]["DiscriDate"].ToString(); lab4InPlace.Text = DalEnum.GetAppConfig(dt.Rows[0]["DoorCode"].ToString()); lab4ElectronCode.Text = dt.Rows[0]["ElectronCode"].ToString(); lab4CarCode.Text = dt.Rows[0]["CarCode"].ToString(); lab4InFlag.Text = DalEnum.GetAppConfig(dt.Rows[0]["OutFlag"].ToString()); byte[] byteArray3 = System.Text.Encoding.Default.GetBytes(dt.Rows[0]["ElectronCode"].ToString() + ".jpg"); int numCheck3 = CarDeviceDll.GetPic_From_Server_IDI(byteArray3, pc, 512); panel5.BackgroundImage = numCheck3 == 0 ? Image.FromFile(Encoding.Default.GetString(pc).TrimEnd('\0')) : Image.FromFile(Application.StartupPath + "\\NoPicture.jpg"); if (dt.Rows[0]["DoorAuthority"].ToString() == "Y") groupBox5.BackColor = System.Drawing.Color.Green; if (dt.Rows[0]["DoorAuthority"].ToString() == "N") groupBox5.BackColor = System.Drawing.Color.Red; if (dt.Rows[0]["DoorAuthority"].ToString() == "H") groupBox5.BackColor = System.Drawing.Color.Black; } } } private void btn_xia_Click(object sender, EventArgs e) { --index; DataTable dt = HNGovernment.DAL.Main.GetUpperDownData(out sError, index); if (dt.Rows.Count > 0) { for (int i = 0; i < dt.Rows.Count; i++) { lab4InTime.Text = dt.Rows[0]["DiscriDate"].ToString(); lab4InPlace.Text = DalEnum.GetAppConfig(dt.Rows[0]["DoorCode"].ToString()); lab4ElectronCode.Text = dt.Rows[0]["ElectronCode"].ToString(); lab4CarCode.Text = dt.Rows[0]["CarCode"].ToString(); lab4InFlag.Text = DalEnum.GetAppConfig(dt.Rows[0]["OutFlag"].ToString()); byte[] byteArray4 = System.Text.Encoding.Default.GetBytes(dt.Rows[0]["ElectronCode"].ToString() + ".jpg"); int numCheck4 = CarDeviceDll.GetPic_From_Server_IDI(byteArray4, pc, 512); panel5.BackgroundImage = numCheck4 == 0 ? Image.FromFile(Encoding.Default.GetString(pc).TrimEnd('\0')) : Image.FromFile(Application.StartupPath + "\\NoPicture.jpg"); if (dt.Rows[0]["DoorAuthority"].ToString() == "Y") groupBox5.BackColor = System.Drawing.Color.Green; if (dt.Rows[0]["DoorAuthority"].ToString() == "N") groupBox5.BackColor = System.Drawing.Color.Red; if (dt.Rows[0]["DoorAuthority"].ToString() == "H") groupBox5.BackColor = System.Drawing.Color.Black; } } if (!String.IsNullOrEmpty(sError)) { MessageBox.Show(sError); } } /// <summary> /// 窗体加载时心跳检测方法判断连接状态,获得数据,将其取出赋值并插入到CarsInfo表里; /// </summary>识别时间|大门编号|车载电子卡号|车牌号|车主姓名|当前门禁权限|出入标志|数据来源|车内人数| /// <param name="sender"></param> /// <param name="e"></param> private void Main_Load(object sender, EventArgs e) { txtCarCode.Text = ""; HNGovernment.Common.CarDeviceDll.Client_Data rec_Client_Data = new CarDeviceDll.Client_Data(); string strRecv_IDI = ""; //初始化外部dll库 if (HNGovernment.Common.CarDeviceDll.Init_IDI(ConfigurationManager.AppSettings["ServerIP"], int.Parse(ConfigurationManager.AppSettings["Port"])) != 0) { lab_log1.Text = "连接中请稍候"; btn_quequan.Enabled = false; } else { lab_log1.Text = "连接成功"; btn_quequan.Enabled = true; btn_9t.Enabled = true; //开启从服务端接受数据线程,只要收到数据就插入到数据库里 threadInit = new Thread(new ThreadStart(() => { while (true) { HNGovernment.Common.CarDeviceDll.HeartBeat_IDI(); if (HNGovernment.Common.CarDeviceDll.Recv_IDI(ref rec_Client_Data) == -1) continue; strRecv_IDI = rec_Client_Data.szData; if (!String.IsNullOrEmpty(strRecv_IDI)) { DataTable _dt = new DataTable(); _dt.Columns.Add("DiscriDate"); _dt.Columns.Add("DoorCode"); _dt.Columns.Add("ElectronCode"); _dt.Columns.Add("CarCode"); _dt.Columns.Add("CarName"); _dt.Columns.Add("DoorAuthority"); _dt.Columns.Add("OutFlag"); _dt.Columns.Add("DataSource"); _dt.Columns.Add("Peoples"); _dt.AcceptChanges(); DataRow _drNew = _dt.NewRow(); string strDate = strRecv_IDI.Split('|').GetValue(0).ToString(); strDate = strDate.Substring(0, 4) + "-" + strDate.Substring(4, 2) + "-" + strDate.Substring(6, 2) + " " + strDate.Substring(8, 2) + ":" + strDate.Substring(10, 2) + ":" + strDate.Substring(12, 2); _drNew["DiscriDate"] = strDate; _drNew["DoorCode"] = strRecv_IDI.Split('|').GetValue(1); _drNew["ElectronCode"] = strRecv_IDI.Split('|').GetValue(2); _drNew["CarCode"] = strRecv_IDI.Split('|').GetValue(3); _drNew["CarName"] = strRecv_IDI.Split('|').GetValue(4); _drNew["DoorAuthority"] = strRecv_IDI.Split('|').GetValue(5); _drNew["OutFlag"] = strRecv_IDI.Split('|').GetValue(6); _drNew["DataSource"] = strRecv_IDI.Split('|').GetValue(7); _drNew["Peoples"] = strRecv_IDI.Split('|').GetValue(8); _dt.Rows.Add(_drNew); _dt.AcceptChanges(); if (HNGovernment.DAL.Main.InsertCarsInfo(out sError, _dt) == false) MessageBox.Show(sError); } } })); threadInit.IsBackground = true; threadInit.Start(); } btnNormal_Click(null, null); //每隔10s界面显示车辆信息清空 _timer10.Elapsed += new System.Timers.ElapsedEventHandler(Time10); _timer10.Interval = 10000; _timer10.Start(); ////每隔30min门卫处登记的车牌信息清空 _timer30.Elapsed += new System.Timers.ElapsedEventHandler(Time30); _timer30.Interval = 1800000; _timer30.Start(); } /// <summary> /// 从CarsInfo里取表信息10s辆车信息显示清空。 /// </summary> private void Time10(object sender, EventArgs e) { string DiscriDate = ""; string carCode = ""; this.BeginInvoke(new Action(delegate() { if (!String.IsNullOrEmpty(lab1InTime.Text)) DiscriDate = "'" + Convert.ToDateTime(lab1InTime.Text.ToString()).ToString("yyyy-MM-dd HH:mm:ss") + "'"; if (!String.IsNullOrEmpty(lab2InTime.Text)) DiscriDate += "," + "'" + Convert.ToDateTime(lab2InTime.Text.ToString()).ToString("yyyy-MM-dd HH:mm:ss") + "'"; if (!String.IsNullOrEmpty(lab3InTime.Text)) DiscriDate += "," + "'" + Convert.ToDateTime(lab3InTime.Text.ToString()).ToString("yyyy-MM-dd HH:mm:ss") + "'"; if (!String.IsNullOrEmpty(lab5InTime.Text)) DiscriDate += "," + "'" + Convert.ToDateTime(lab5InTime.Text.ToString()).ToString("yyyy-MM-dd HH:mm:ss") + "'"; if (!String.IsNullOrEmpty(lab1CarCode.Text)) carCode = "'" + lab1CarCode.Text.Trim() + "'"; if (!String.IsNullOrEmpty(lab2CarCode.Text)) carCode += "," + "'" + lab2CarCode.Text + "'"; if (!String.IsNullOrEmpty(lab3CarCode.Text)) carCode += "," + "'" + lab3CarCode.Text + "'"; if (!String.IsNullOrEmpty(lab5CarCode.Text)) carCode += "," + "'" + lab5CarCode.Text.Trim() + "'"; HNGovernment.DAL.Main.InsertCarsInfoHistory(out sError, DiscriDate, carCode); HNGovernment.DAL.Main.DeleteCarsInfo(out sError, DiscriDate, carCode); if (!String.IsNullOrEmpty(sError)) MessageBox.Show(sError); })); btnNormal_Click(null, null); } /// <summary> /// /// 门卫登记车牌信息(30分钟内有效) /// </summary> private void Time30(object sender, EventArgs e) { this.txtCarCode.BeginInvoke(new Action(delegate() { string carCode30 = ""; if (!String.IsNullOrEmpty(txtCarCode.Text.Trim())) { string[] carCodes = txtCarCode.Text.Trim().Split('\t'); for (int i = 0; i < carCodes.Length; i++) { carCode30 += "'" + carCodes[i].ToString() + "'" + ","; } HNGovernment.DAL.Main.UpdateDoorData(out sError, carCode30.TrimEnd(',')); if (!String.IsNullOrEmpty(sError)) MessageBox.Show(sError); } })); btnNormal_Click(null, null); } /// <summary> /// 再次确权 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void btn_quequan_Click(object sender, EventArgs e) { if (HNGovernment.Common.CarDeviceDll.QuideInfo_Check(byte.Parse(System.Configuration.ConfigurationManager.AppSettings["QUE_QUAN"])) == -1) MessageBox.Show("您好,车辆感应设备可能有问题..."); } /// <summary> /// 普通模式 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void btnNormal_Click(object sender, EventArgs e) { DataTable dt = HNGovernment.DAL.Main.GetNormalData(out sError); lab1InTime.BeginInvoke(new Action(delegate() { if (dt != null && dt.Rows.Count > 0) { try { lab1InTime.Text = dt.Rows[0]["DiscriDate"].ToString(); lab1InPlace.Text = DalEnum.GetAppConfig(dt.Rows[0]["DoorCode"].ToString()); lab1ElectronCode.Text = dt.Rows[0]["ElectronCode"].ToString(); lab1CarCode.Text = dt.Rows[0]["CarCode"].ToString(); lab1InFlag.Text = DalEnum.GetAppConfig(dt.Rows[0]["OutFlag"].ToString()); //根据车牌电子卡号获取汽车图片 byteArray = System.Text.Encoding.Default.GetBytes(dt.Rows[0]["ElectronCode"].ToString() + ".jpg"); numCheck = CarDeviceDll.GetPic_From_Server_IDI(byteArray, pc, 512); panel1.BackgroundImage = numCheck == 0 ? Image.FromFile(Encoding.Default.GetString(pc).TrimEnd('\0')) : Image.FromFile(Application.StartupPath + "\\NoPicture.jpg"); if (dt.Rows[0]["DoorAuthority"].ToString() == "Y") groupBox1.BackColor = System.Drawing.Color.Green; if (dt.Rows[0]["DoorAuthority"].ToString() == "N") groupBox1.BackColor = System.Drawing.Color.Red; if (dt.Rows[0]["DoorAuthority"].ToString() == "H") groupBox1.BackColor = System.Drawing.Color.Black; if (dt.Rows.Count > 1) { lab2InTime.Text = dt.Rows[1]["DiscriDate"].ToString(); lab2InPlace.Text = DalEnum.GetAppConfig(dt.Rows[1]["DoorCode"].ToString()); lab2ElectronCode.Text = dt.Rows[1]["ElectronCode"].ToString(); byteArray = System.Text.Encoding.Default.GetBytes(dt.Rows[1]["ElectronCode"].ToString() + ".jpg"); numCheck = CarDeviceDll.GetPic_From_Server_IDI(byteArray, pc, 512); panel2.BackgroundImage = numCheck == 0 ? Image.FromFile(Encoding.Default.GetString(pc).TrimEnd('\0')) : Image.FromFile(Application.StartupPath + "\\NoPicture.jpg"); lab2CarCode.Text = dt.Rows[1]["CarCode"].ToString(); lab2InFlag.Text = DalEnum.GetAppConfig(dt.Rows[1]["OutFlag"].ToString()); if (dt.Rows[1]["DoorAuthority"].ToString() == "Y") groupBox2.BackColor = System.Drawing.Color.Green; if (dt.Rows[1]["DoorAuthority"].ToString() == "N") groupBox2.BackColor = System.Drawing.Color.Red; if (dt.Rows[1]["DoorAuthority"].ToString() == "H") groupBox2.BackColor = System.Drawing.Color.Black; } else { lab2InTime.Text = ""; lab2InPlace.Text = ""; lab2ElectronCode.Text = ""; lab2CarCode.Text = ""; lab2InFlag.Text = ""; panel2.BackgroundImage = Image.FromFile(Application.StartupPath + "\\NoPicture.jpg"); groupBox2.BackColor = System.Drawing.Color.Blue; } if (dt.Rows.Count > 2) { lab3InTime.Text = dt.Rows[2]["DiscriDate"].ToString(); lab3InPlace.Text = DalEnum.GetAppConfig(dt.Rows[2]["DoorCode"].ToString()); lab3ElectronCode.Text = dt.Rows[2]["ElectronCode"].ToString(); byteArray = System.Text.Encoding.Default.GetBytes(dt.Rows[2]["ElectronCode"].ToString() + ".jpg"); numCheck = CarDeviceDll.GetPic_From_Server_IDI(byteArray, pc, 512); panel3.BackgroundImage = numCheck == 0 ? Image.FromFile(Encoding.Default.GetString(pc).TrimEnd('\0')) : Image.FromFile(Application.StartupPath + "\\NoPicture.jpg"); lab3CarCode.Text = dt.Rows[2]["CarCode"].ToString(); lab3InFlag.Text = DalEnum.GetAppConfig(dt.Rows[2]["OutFlag"].ToString()); if (dt.Rows[2]["DoorAuthority"].ToString() == "Y") groupBox3.BackColor = System.Drawing.Color.Green; if (dt.Rows[2]["DoorAuthority"].ToString() == "N") groupBox3.BackColor = System.Drawing.Color.Red; if (dt.Rows[2]["DoorAuthority"].ToString() == "H") groupBox3.BackColor = System.Drawing.Color.Black; } else { lab3InTime.Text = ""; lab3InPlace.Text = ""; lab3ElectronCode.Text = ""; lab3CarCode.Text = ""; lab3InFlag.Text = ""; panel3.BackgroundImage = Image.FromFile(Application.StartupPath + "\\NoPicture.jpg"); groupBox3.BackColor = System.Drawing.Color.Blue; } if (dt.Rows.Count > 3) { lab5InTime.Text = dt.Rows[3]["DiscriDate"].ToString(); lab5InPlace.Text = DalEnum.GetAppConfig(dt.Rows[3]["DoorCode"].ToString()); labl5ElectronCode.Text = dt.Rows[3]["ElectronCode"].ToString(); byteArray = System.Text.Encoding.Default.GetBytes(dt.Rows[3]["ElectronCode"].ToString() + ".jpg"); numCheck = CarDeviceDll.GetPic_From_Server_IDI(byteArray, pc, 512); panel4.BackgroundImage = numCheck == 0 ? Image.FromFile(Encoding.Default.GetString(pc).TrimEnd('\0')) : Image.FromFile(Application.StartupPath + "\\NoPicture.jpg"); lab5CarCode.Text = dt.Rows[3]["CarCode"].ToString(); lab5InFlag.Text = DalEnum.GetAppConfig(dt.Rows[3]["OutFlag"].ToString()); if (dt.Rows[3]["DoorAuthority"].ToString() == "Y") groupBox4.BackColor = System.Drawing.Color.Green; if (dt.Rows[3]["DoorAuthority"].ToString() == "N") groupBox4.BackColor = System.Drawing.Color.Red; if (dt.Rows[3]["DoorAuthority"].ToString() == "H") groupBox4.BackColor = System.Drawing.Color.Black; } else { lab5InTime.Text = ""; lab5InPlace.Text = ""; lab5ElectronCode.Text = ""; lab5CarCode.Text = ""; lab5InFlag.Text = ""; panel4.BackgroundImage = Image.FromFile(Application.StartupPath + "\\NoPicture.jpg"); groupBox4.BackColor = System.Drawing.Color.Blue; } } catch { } } else { lab1InTime.Text = ""; lab1InPlace.Text = ""; lab1ElectronCode.Text = ""; lab1CarCode.Text = ""; lab1InFlag.Text = ""; panel1.BackgroundImage = Image.FromFile(Application.StartupPath + "\\NoPicture.jpg"); groupBox1.BackColor = System.Drawing.Color.Blue; lab2InTime.Text = ""; lab2InPlace.Text = ""; lab2ElectronCode.Text = ""; lab2CarCode.Text = ""; lab2InFlag.Text = ""; panel2.BackgroundImage = Image.FromFile(Application.StartupPath + "\\NoPicture.jpg"); groupBox2.BackColor = System.Drawing.Color.Blue; lab3InTime.Text = ""; lab3InPlace.Text = ""; lab3ElectronCode.Text = ""; lab3CarCode.Text = ""; lab3InFlag.Text = ""; panel3.BackgroundImage = Image.FromFile(Application.StartupPath + "\\NoPicture.jpg"); groupBox3.BackColor = System.Drawing.Color.Blue; lab5InTime.Text = ""; lab5InPlace.Text = ""; lab5ElectronCode.Text = ""; lab5CarCode.Text = ""; lab5InFlag.Text = ""; panel4.BackgroundImage = Image.FromFile(Application.StartupPath + "\\NoPicture.jpg"); groupBox4.BackColor = System.Drawing.Color.Blue; } DataTable dt1 = HNGovernment.DAL.Main.GetCarCode(out sError); if (dt1 != null && dt1.Rows.Count > 0) { foreach (DataRow _dr in dt1.Rows) { txtCarCode.Text += _dr["CarCode"].ToString() + "\t"; } } else { txtCarCode.Text = ""; } } )); } /// <summary> /// 高峰模式 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void btnPeak(object sender, EventArgs e) { if (HNGovernment.Common.CarDeviceDll.Init_IDI(ConfigurationManager.AppSettings["ServerIP"], int.Parse(ConfigurationManager.AppSettings["Port"])) == 0) { threadInit.Abort(); } timer1.Stop(); _timer10.Stop(); _timer30.Stop(); reForm = new Main9(); this.Close(); this.Dispose(); } private void timer1_Tick(object sender, EventArgs e) { DateTime time = DateTime.Now; labTime.Text = time.ToString(); HNGovernment.Common.CarDeviceDll.HeartBeat_IDI(); string timess = DateTime.Now.ToString("HH:mm:ss").ToString(); if (DateTime.Now.ToString("HH:mm:ss") == "03:45:00") { string sError = ""; bool isOk = DAL.Main.DeleteData(out sError); if (isOk == false) MessageBox.Show("删除一个月之前的数据有异常!"); } } private void btnPersonFrom_Click(object sender, EventArgs e) { if (HNGovernment.Common.CarDeviceDll.Init_IDI(ConfigurationManager.AppSettings["ServerIP"], int.Parse(ConfigurationManager.AppSettings["Port"])) == 0) { threadInit.Abort(); } _timer10.Stop(); _timer30.Stop(); timer1.Stop(); reForm = new MainR(); this.Close(); this.Dispose(); } private void btnExit_Click(object sender, EventArgs e) { Application.Exit(); } } }
public class newForm : System.Windows.Forms.Form { public newForm reForm; }
所有问题都会有一定程度的抽象和假设