using System; using System.Collections.Generic; using System.Linq; using System.Windows.Forms; using System.Data; using System.IO; using System.Collections; using System.Threading; using Att_Auto_Transfer_Basic_Csharp.config; using Att_Auto_Transfer_Basic_Csharp.biz; using Att_Auto_Transfer_Basic_Csharp.common; using System.Diagnostics; using Att_Auto_Transfer_Basic_Csharp.log; using System.Runtime.InteropServices; using System.Text; using System.Text.RegularExpressions; namespace Att_Auto_Transfer_Basic_Csharp { static class Program { public static string errorLogPath = ""; public static string configurePath = ""; static void Main() {ProcessJudge(); } #region 判断线程 -static void ProcessJudge() /// <summary> /// 判断线程 /// </summary> /// <returns></returns> [DllImport("user32", SetLastError = true)] public static extern int GetWindowText( IntPtr hWnd, StringBuilder lpString, int nMaxCount ); private static void ProcessJudge() { int MAX_MINUTES = 1; //30分钟程序没有跑完则Kill掉 Process currentProcess = Process.GetCurrentProcess(); string currentProcessName = currentProcess.ProcessName; int currentProcessId = currentProcess.Id; List<Process> process = new List<Process>(); process.AddRange(Process.GetProcesses()); string strLogPath = Directory.GetCurrentDirectory() + " \\errorLog.txt"; List<string> list = new List<string>(); for (int i = 0; i < process.Count; i++) { list.Add(process[i].ProcessName); string sProcessName = process[i].ProcessName; int th32ProcessID = process[i].Id; if (currentProcessName.Equals(sProcessName)) //判断进程名 { Log.WriteLog("sProcessName was:", sProcessName); Log.WriteLog("currentProcessName was:", currentProcessName); if (currentProcessId != th32ProcessID) //判断进程ID { Log.WriteLog("th32ProcessID was:", th32ProcessID.ToString()); IntPtr ptr = process[i].MainWindowHandle; Log.WriteLog("currentProcessId was:", currentProcessId.ToString()); StringBuilder sb = new StringBuilder(256); GetWindowText(ptr, sb, sb.Capacity); string strTh32ProcessTitle = sb.ToString(); string sDateTime = strTh32ProcessTitle.Substring(strTh32ProcessTitle.IndexOf(":") + 1, strTh32ProcessTitle.Length - strTh32ProcessTitle.IndexOf(":") - 1); DateTime DateTime2 = DateTime.Parse(sDateTime); TimeSpan timeSpan = DateTime.Now - DateTime2; int iMinutes = timeSpan.Minutes; if (iMinutes >= MAX_MINUTES) //上次程序运行时间>MAX_MINUTES设置的值则kill上次运行的程序 { Log.WriteLog("Different Process Run:", th32ProcessID.ToString()); process[i].Kill(); Log.WriteLog("Kill Process Run:", th32ProcessID.ToString() + " Successful!!!"); } else { CommonDelegate.blStart = false; //上次程序运行时间<MAX_MINUTES设置的值则不运行 } } } } } #endregion } }