private static void StartMainWindow() { Stopwatch watch2 = new Stopwatch(); //检查同名进程数 ELogger.Info(string.Format("程序{0}启动...", Application.ProductName)); Process[] processes = System.Diagnostics.Process.GetProcessesByName(Application.ProductName); ELogger.Info(string.Format("发现进程数{0}", processes.Length)); //只允许运行1个实例 if (processes.Length <= 1) { watch2.Start(); Application.EnableVisualStyles(); Application.SetCompatibleTextRenderingDefault(false); ELogger.Info(string.Format("启动主线程...")); watch2.Stop(); ELogger.Info("记录2耗时:" + watch2.ElapsedMilliseconds); //记录5耗时:11889 Application.Run(new Form1()); ELogger.Info(string.Format("主线程结束...")); } else { ELogger.Info("不允许多实例运行,退出"); //单实例,不允许多实例运行 System.Environment.Exit(1); } }
/// <summary> /// 构造函数 /// </summary> public Form1() { InitializeComponent(); table.Columns.Add("方向"); table.Columns.Add("协议层数据"); table.Columns.Add("数据层数据"); table.Columns.Add("时间"); table.Columns.Add("SR"); table.Columns.Add("NR"); dataGridView1.DataSource = table; dataGridView1.Columns[0].AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill; dataGridView1.Columns[0].FillWeight = 10; dataGridView1.Columns[1].AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill; dataGridView1.Columns[1].FillWeight = 20; dataGridView1.Columns[2].AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill; dataGridView1.Columns[2].FillWeight = 60; dataGridView1.Columns[3].AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill; dataGridView1.Columns[3].FillWeight = 10; dataGridView1.Columns[4].AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill; dataGridView1.Columns[4].FillWeight = 5; dataGridView1.Columns[5].AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill; dataGridView1.Columns[5].FillWeight = 5; table2.Columns.Add("地址"); table2.Columns.Add("值"); table2.Columns.Add("时间"); table2.PrimaryKey = new DataColumn[] { table2.Columns[0] }; dataGridView2.DataSource = table2; dataGridView2.Columns[0].AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill; dataGridView2.Columns[0].FillWeight = 10; dataGridView2.Columns[1].AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill; dataGridView2.Columns[1].FillWeight = 20; dataGridView2.Columns[2].AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill; dataGridView2.Columns[2].FillWeight = 60; ReadCSV(); if (!ReadEnable) { backgroundWorker1.RunWorkerAsync(); } else { waitHandel.Set(); } ELogger.Info("启动成功"); }
private void ReadCSV() { StreamReader sr = new StreamReader(Application.StartupPath + @"\TAGLIST.CSV"); String line; int i = 0; //越过第一行标题 sr.ReadLine(); while ((line = sr.ReadLine()) != null) { TagInfo aTagInfo = new TagInfo(); string[] strarr = line.Split(','); aTagInfo.ADDRNO = Convert.ToInt32(strarr[0]); aTagInfo.TAGNAME = strarr[1]; aTagInfo.TAGMC = strarr[2]; aTagInfo.TAGLX = strarr[3]; lstTagInfo.Add(aTagInfo.ADDRNO, aTagInfo); i++; } sr.Close(); ELogger.Info("1214:初始化TAGLIST个数:" + i.ToString()); }
private void checkState() { while (true) { Thread.Sleep(10000); if (client.Connected == false) { try { client.Close(); client = new TcpClient(); client.Connect(hostip, port); IsConnection = true; } catch { IsConnection = false; ELogger.Info("网闸外侧连接失败"); } } } }
Thread checkStateThread; //检查网络状态线程 public TcpClientEx(string hostip, int port) { this.hostip = hostip; this.port = port; client = new TcpClient(); try { client.Connect(hostip, port); if (client.Connected) { IsConnection = true; Console.WriteLine(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "网闸外侧连接成功"); ELogger.Info("网闸外侧连接成功"); } } catch (Exception ex) { IsConnection = false; } checkStateThread = new Thread(new ThreadStart(checkState)); checkStateThread.IsBackground = true; checkStateThread.Start(); }
/// <summary> /// 后台线程启动 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e) { try { UDPPushClient = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp); IPEndPoint send = new IPEndPoint(IPAddress.Parse("127.0.0.1"), 9020); Remote = (EndPoint)(send); APDUClass myBuffer = new APDUClass(new APCIClassUFormat(APCIClassUFormat.UFormatType.StartSet), null); tm = new System.Timers.Timer(); socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); socket.Connect(IPAddress.Parse(textBox1.Text), 2404); socket.SendTimeout = 500; socket.ReceiveTimeout = 500; sendTCPClient = new TcpClientEx(SENDTCPIP, SENDTCPPORT); SendList.Enqueue(new APDUClass(new APCIClassUFormat(APCIClassUFormat.UFormatType.StartSet), null)); sr = 0; nr = 0; ASDUClass clockBuffer = new ASDUClass(); clockBuffer.Pack(ASDUClass.TransRes.Active, ASDUClass.FunType.ClockConfirm); ASDUClass calBuffer = new ASDUClass(); calBuffer.Pack(ASDUClass.TransRes.Active, ASDUClass.FunType.CalAll); ASDUClass ymBuffer = new ASDUClass(); ymBuffer.Pack(ASDUClass.TransRes.Active, ASDUClass.FunType.CalEnergyPulse); // SendList.Enqueue(new APDUClass(new APCIClassIFormat(++sr, nr), clockBuffer)); SendList.Enqueue(new APDUClass(new APCIClassIFormat(++sr, nr), calBuffer)); SendList.Enqueue(new APDUClass(new APCIClassIFormat(++sr, nr), ymBuffer)); //SendList.Enqueue(new APDUClass(new APCIClassUFormat(APCIClassUFormat.UFormatType.StopSet), null)); ReadEnable = true; ReceiveOnce(); tm.AutoReset = true; tm.Interval = scaneRate; tm.Elapsed += new System.Timers.ElapsedEventHandler(tm_Elapsed); tm.Start(); new Thread(t => { while (true) { foreach (var item in dicTag) { var sSendMessage = string.Format(sJsonRT, DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"), item.Key, item.Value); if (sendTCPClient.IsConnection) { sendTCPClient.SendMessage(sSendMessage); UDPPushClient.SendTo(EncodeUDP(sSendMessage), Remote); Console.WriteLine(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + ":" + sSendMessage); ELogger.Info("发送:" + sSendMessage); } } Thread.Sleep(PauseTime); } }).Start(); waitHandel.WaitOne(); } catch (Exception ex) { ELogger.Error("backgroundWorker1_DoWork error:" + ex.Message + ex.StackTrace); } }