close

C#: 預防程式重覆程動Way 1:Sometimes, we don't wish that user can open multiple program .So this article will tell us how to prevent it.First, open your project and edit Program.cs .1.Using section add "System.Threading"; .2.add a static varible "private static Mutex SMutex;".3.Tell program your applicaion text(example: paint helper4.and use this in your Main() method:SMutex = new Mutex(true, "paint helper");if (SMutex.WaitOne(0,false)) Application.Run(new Form1());else MessageBox.Show("paint helper is already running.","Startup Error",MessageBoxButtons.OK,MessageBoxIcon.Error);5.Finished.Way2:(2011/12/15 update)1.using window api to check the program is exist in system.using System.Runtime.InteropServices; //dll import class private const int SW_SHOWNORMAL = 1;// 1.Normal 2.Minimized 3.Maximized [DllImport("user32.dll", EntryPoint = "FindWindow")] public static extern IntPtr FindWindow(string lpClassName, string lpWindowName); [DllImport("user32.dll", EntryPoint = "ShowWindow")] public extern static int ShowWindow(IntPtr hDlg, short State); public static int ShowExistsApplication(string Title) //Title is the MainForm caption { IntPtr ParenthWnd = new IntPtr(0); ParenthWnd = FindWindow(null, Title);//Get the MainForm id andle id return ShowWindow(ParenthWnd, SW_SHOWNORMAL); }if ParenthWnd value is 0, the program is no exist in your system. Then function ShowWindow is show program window when it exists in system. .msgcontent .wsharing ul li { text-indent: 0; } 分享 Facebook Plurk YAHOO! .


arrow
arrow
    全站熱搜
    創作者介紹
    創作者 xx55531 的頭像
    xx55531

    中醫

    xx55531 發表在 痞客邦 留言(0) 人氣()