123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231 |
- using System;
- using System.Collections.Generic;
- using System.ComponentModel;
- using System.Data;
- using System.Diagnostics;
- using System.Drawing;
- using System.Text;
- using System.Threading;
- using System.Windows.Forms;
- namespace EAPClock
- {
- public partial class Form1 : Form
- {
- //EAP小程序的名称,可能是下面两个中的某一个
- static string eapName1 = "MAutoUpdate.application";
- static string eapName2 = "EapForIdle.application";
- string eapPath1 = @"C:\Users\XiangQi\Desktop\" + eapName1; //Eap小程序的 桌面 路径
- string eapPath2 = @"C:\Users\XiangQi\Desktop\" + eapName2;
- string eapPath_D1 = @"D:\" + eapName1; //Eap小程序的 D盘 路径
- string eapPath_D2 = @"D:\" + eapName2;
- string eapPath_Auto1 = @"C:\Users\XiangQi\Desktop\123\" + eapName1; //Eap小程序的 自启动目录的 路径
- string eapPath_Auto2 = @"C:\Users\XiangQi\Desktop\123\" + eapName2;
- string eapProcessName = "EapForIdle";
- bool isStartOrNot; //判断EAP小程序是否启动
- int EAPClockRunCount = 0; //判本EAPClock此程序后台是否已运行。1表示没运行,大于1表示后台已运行此程序。
- //////下面是每个机型的机台路径:
- string WBDALHA_eapPath_desk1 = @"C:\Documents and Settings\Administrator\Desktop\" + eapName1;
- string WBDALHA_eapPath_desk2 = @"C:\Documents and Settings\Administrator\Desktop\" + eapName2;
- string WBDALHA_eapPath_D1 = @"D:\" + eapName1;
- string WBDALHA_eapPath_D2 = @"D:\" + eapName2;
- string WBDALHA_eapPath_auto1 = @"C:\Documents and Settings\Administrator\Start Menu\Programs\Startup\" + eapName1;
- string WBDALHA_eapPath_auto2 = @"C:\Documents and Settings\Administrator\Start Menu\Programs\Startup\" + eapName2;
- //WB、LHA、DA 机台的路径相同
- string AA_eapPath_desk1 = @"C:\Users\Administrator\Desktop\" + eapName1;
- string AA_eapPath_desk2 = @"C:\Users\Administrator\Desktop\" + eapName2;
- //AA的D盘路径和上面机台相同
- string AA_eapPath_auto1 = @"C:\Users\Administrator\AppData\Roaming\Microsoft\Windows\Start Menu\Programs\Startup\" + eapName1;
- string AA_eapPath_auto2 = @"C:\Users\Administrator\AppData\Roaming\Microsoft\Windows\Start Menu\Programs\Startup\" + eapName1;
- public Form1()
- {
- InitializeComponent();
- //隐藏主窗体
- this.WindowState = FormWindowState.Minimized;
- //隐藏任务栏
- this.ShowInTaskbar = false;
- //解决 Alt+Tab可以调出主窗体
- SetVisibleCore(false);
- }
- ///// 禁用窗体的关闭按钮
- ///// 同时不让窗体出现在 Alt+Tab 中。 此步骤也不会出现在 进程 中,只有进程详情中才有
- private const int CP_NOCLOSE_BUTTON = 0x200;
- protected override CreateParams CreateParams
- {
- get
- {
- const int WS_EX_TOOLWINDOW = 0x80; // 不显示在Alt+Tab
- CreateParams myCp = base.CreateParams;
- myCp.ClassStyle = myCp.ClassStyle | CP_NOCLOSE_BUTTON;
- myCp.ExStyle |= WS_EX_TOOLWINDOW; // 不显示在Alt+Tab
- return myCp;
- }
- }
- protected override void SetVisibleCore(bool value) //重写方法,目的是解决 Alt+Tab可以调出主窗体
- {
- base.SetVisibleCore(value);
- }
- private void Form1_Load(object sender, EventArgs e)
- {
- Control.CheckForIllegalCrossThreadCalls = false; //取消跨线程的检查
- MessageBox.Show("008版本");
- //AutoUpdate.CheckUpdate();
- Init();
- }
- /// <summary>
- /// 启动小程序的方法,让 AlwaysRun()方法死循环调用。根据小程序的不同路径来启动 (路径数组eapPathArry)
- /// </summary>
- public void StartMAutoUpdate()
- {
- string[] eapPathArryTest = { eapPath1, eapPath_D1, eapPath_Auto1, eapPath2, eapPath_D2, eapPath_Auto2 }; //EAP测试路径的 数组
- string[] eapPathArry = {
- WBDALHA_eapPath_desk1,
- WBDALHA_eapPath_D1,
- WBDALHA_eapPath_auto1,
- AA_eapPath_desk1,
- AA_eapPath_auto1,
- WBDALHA_eapPath_desk2,
- WBDALHA_eapPath_D2,
- WBDALHA_eapPath_auto2,
- AA_eapPath_desk2,
- AA_eapPath_auto2
- }; //EAP机台的 路径数组
- for (int i = 0; i < eapPathArryTest.Length; i++)
- {
- try
- {
- StartOnPath(eapPathArryTest[i]);
- }
- catch (Exception)
- {
- }
- if (isStartOrNot == true)
- {
- MessageBox.Show("监测到后台未开启EAP小程序,EAPClock已为您自动开启EAP");
- return;
- }
- }
- if (isStartOrNot == false)
- {
- MessageBox.Show("将小程序原文件放在下列任意一个目录中:\r\n\r\n 1-电脑桌面\r\n 2-D盘 \r\n 3-自启动目录", "小程序定时启动失败!", MessageBoxButtons.OK, MessageBoxIcon.Warning);
- }
- }
- /// <summary>
- /// 根据小程序的路径启动小程序
- /// </summary>
- /// <param name="path">传参小程序的路径</param>
- public void StartOnPath(string path)
- {
- isStartOrNot = false;
- ProcessStartInfo processStartInfo = new ProcessStartInfo(path);
- Process process = new Process();
- process.StartInfo = processStartInfo;
- isStartOrNot = process.Start();
- }
- /// <summary>
- /// 判断此程序EAPClock是否单例运行
- /// </summary>
- /// <returns>返回值runCount如果大于1,表示小程序已运行。如果等于1,表示只有当前程序在运行。</returns>
- public int IsOrNontOnlyOne()
- {
- //通过进程名获取当前进 此程序EAP 运行的实例,存放到进程数组中。
- Process[] processes = Process.GetProcessesByName("EAPClock");
- EAPClockRunCount = processes.Length;
- return EAPClockRunCount;
- }
- /// <summary>
- /// 初始化此程序。先判断后台是否已运行,如果没运行则调用 newThreadStart()
- /// </summary>
- public void Init()
- {
- IsOrNontOnlyOne();
- if (EAPClockRunCount > 1)
- {
- MessageBox.Show("后台已运行此定时系统");
- this.Close();
- Application.Exit();
- }
- else
- {
- newThreadStart();
- MessageBox.Show("已开启定时系统,欢迎使用");
- }
- }
- /// <summary>
- /// 开个新线程启动小程序.
- /// </summary>
- public void newThreadStart()
- {
- Thread thread = new Thread(AlwaysRun); //新线程调用AlwaysRun()死循环方法
- thread.IsBackground = true;
- thread.Start();
- }
- /// <summary>
- /// 循环检查是否运行小程序,如果后台没运行小程序,就启动小程序。
- /// </summary>
- public void AlwaysRun()
- {
- int runCount;
- while (true)
- {
- runCount = IsOrNotRun();
- if (runCount == 0)
- {
- StartMAutoUpdate();
- }
- // else MessageBox.Show("小程序已在后台开启,无需再次打开");
- Thread.Sleep(30000); //当前线程睡眠
- MessageBox.Show("008版本");
- }
- }
- /// <summary>
- /// 判断小程序是否在进程中运行,返回值runCount如果大于0,表示小程序已运行。如果等于0,表示未运行。
- /// </summary>
- public int IsOrNotRun()
- {
- //通过小程序进程名获取当前进小程序运行的实例,存放到进程数组中。
- Process[] processes = Process.GetProcessesByName(eapProcessName);
- int runCount = processes.Length;
- return runCount;
- }
- }
- }
|