• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

C# Threading.Mutex类代码示例

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

本文整理汇总了C#中System.Threading.Mutex的典型用法代码示例。如果您正苦于以下问题:C# System.Threading.Mutex类的具体用法?C# System.Threading.Mutex怎么用?C# System.Threading.Mutex使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。



System.Threading.Mutex类属于命名空间,在下文中一共展示了System.Threading.Mutex类的20个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的C#代码示例。

示例1: Main

        /* mutex tries to create a mutex lock, titled "MathDrills". result will be whether or not that succeeded. It won't succeed if it is already locked
         * GC.KeepAlive means that the garbage collector will never release that mutex.
         * 
         * If it could not get the mutex lock, then don't bother running the rest of the program.
         */
        static void Main()
        {
            bool result;
            rng = new Random();
            var mutex = new System.Threading.Mutex(true, "MathDrills", out result);
            if (!result)
            {
                MessageBox.Show("The game is already open in another window.");
                return;

            }
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);

            Directory.CreateDirectory(@"c:\users\public\MathDrills\ProblemSets"); //Make sure there is a directory into which to save the problems
            
            initializeConfigFile();
            //initializeUserList();
            //Aurelio Arango
            //3-31-14 

            xml = new XML_Handler();
            xml.create_xml(xml.check_xml_exists(USERSFILE));
            loadData();
            Application.Run(new StartForm());

            //Application.Run(new LoginForm());
            GC.KeepAlive(mutex);
        }
开发者ID:ksnovak,项目名称:CS-441-Group2,代码行数:34,代码来源:Program.cs


示例2: CheckPrevious

        System.Threading.Mutex _Mutex; //Used to determine if the application is already open

        #endregion Fields

        #region Methods

        public bool CheckPrevious(int wParam)
        {
            //Check for previous instance of this app
            lock (mutexLock)
            {
                m_uniqueIdentifier = Application.ProductName;
                _Mutex = new System.Threading.Mutex(false, m_uniqueIdentifier);
            }
            // First register the windows message
            MessageId = NativeMethods.RegisterWindowMessage(m_uniqueIdentifier);
            if (_Mutex.WaitOne(1, true))
            {
                //we are the first instance don't need to do anything
                return true;
            }
            else
            {
                // Cause the current form to show
                // Now broadcast a message to cause the first instance to show up
                Int32 BSMRecipients = BSM_APPLICATIONS; //Only go to applications
                Int32 tmpuint32 = 0;
                tmpuint32 = tmpuint32 | BSF_IGNORECURRENTTASK; //Ignore current app
                tmpuint32 = tmpuint32 | BSF_POSTMESSAGE; //Post the windows message
                int ret = NativeMethods.BroadcastSystemMessage(tmpuint32, ref BSMRecipients, MessageId, wParam, 0);
                //A different instance already exists exit now.
                return false;
            }
        }
开发者ID:JocysCom,项目名称:PassGen,代码行数:34,代码来源:MainForm.OneInstance.cs


示例3: Main

        static void Main()
        {
            //string[] args = Environment.GetCommandLineArgs();
            //if (args.Length > 1)
            //{
            //    string url = args[1];
            //    url=url.Replace("{", "").Replace("}","");
            //    try
            //    {
            //        CodeAnalyser.Download(url);
            //    }
            //    catch (Exception ex)
            //    {
            //        MessageBox.Show(ex.Message);
            //    }
            //}

            bool createdNew;
            System.Threading.Mutex m = new System.Threading.Mutex(true, "Your App here", out createdNew);

            if (!createdNew)
            {
                return;
            }

            {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            Application.Run(new ToolForm());

            } GC.KeepAlive(m); // important!
        }
开发者ID:brightideainc,项目名称:TSTool,代码行数:32,代码来源:Program.cs


示例4: Main

 static void Main( )
 {
     bool ret;
     System.Threading.Mutex mutex = new System.Threading.Mutex(true, Application.ProductName, out ret);
     if (ret)
     {
         Application.EnableVisualStyles( );
         Application.SetCompatibleTextRenderingDefault(false);
         //Application.Run(new FingerTest( ));
         //Application.Run(MainMetroForm.Instance);
         Utilities.CreateDesktopShortCut( );
         AppContext.IsRuning = true;
         ServiceProvider.Init( );
         if (new LoginForm( ).ShowDialog( ) == DialogResult.OK)
             Application.Run(MainMetroForm.Instance);
         else
             Application.Exit( );
     }
     else
     {
         MessageBox.Show("已经有相同的应用程序在运行,请不要同时运行多个本程序。\n\n这个程序即将退出。", Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Warning);
         //   提示信息,可以删除。
         Application.Exit( );//退出程序
     }
 }
开发者ID:eleooo,项目名称:App,代码行数:25,代码来源:Program.cs


示例5: Main

        private static void Main()
        {
            if( Properties.Settings.Default.newFileFlag ) {
                Properties.Settings.Default.Upgrade();
                Properties.Settings.Default.newFileFlag = true;
                Properties.Settings.Default.Save();
            }

            // 多重起動対策
            using( var mutex = new System.Threading.Mutex( false, Application.ProductName ) ) {
                if( mutex.WaitOne( 0, false ) ) {
                    Application.EnableVisualStyles();
                    Application.SetCompatibleTextRenderingDefault( false );
                    var mainForm = new MainForm();
                    if( (Options.StartupState)Properties.Settings.Default.startupState == Options.StartupState.NORMAL ) {
                        Application.Run( mainForm );
                    } else {
                        Application.Run();
                    }
                } else {
                    var hThisProcess = Process.GetCurrentProcess();
                    var hProcesses = Process.GetProcessesByName( hThisProcess.ProcessName );
                    var iThisProcessId = hThisProcess.Id;

                    foreach( var hProcess in hProcesses.Where( hProcess => hProcess.Id != iThisProcessId ) ) {
                        Win32.Window.ShowWindow( hProcess.MainWindowHandle, Win32.Window.SW_NORMAL );
                        Win32.Window.SetForegroundWindow( hProcess.MainWindowHandle );
                        break;
                    }
                    MessageBox.Show( "既にLordOfRangerが起動されています。" );
                }
            }
        }
开发者ID:southernwind,项目名称:Lord-Of-Ranger,代码行数:33,代码来源:Program.cs


示例6: GetMutex

		public bool GetMutex()
		{
			bool acquired;
			_appMutex = new System.Threading.Mutex(true, "KeyMapperAppMutex", out acquired);
			
			return acquired;
		}
开发者ID:tmzu,项目名称:keymapper,代码行数:7,代码来源:AppMutex.cs


示例7: Main

        static void Main(string[] arg)
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);

            try
            {
                mutex = new System.Threading.Mutex(false, @"{747690E4-BCE9-4949-9B79-FBAC7E700D63}");
                if (!mutex.WaitOne(0, false))
                {
                    Process prevProcess = GetPreviousProcess();
                    if (prevProcess != null && prevProcess.MainWindowHandle != IntPtr.Zero)
                    {
                        // 起動中のアプリケーションを最前面に表示
                        WakeupWindow(prevProcess.MainWindowHandle);
                    }
                    return;
                }

                switch (arg.Length)
                {
                    case 0:
                        Application.Run(new Form1());
                        break;
                    case 3:
                        loginState form = new loginState(arg[0], arg[1], bool.Parse(arg[2]));
                        Application.Run(form);
                        break;
                }
            }
            finally
            {
                mutex.Close();
            }
        }
开发者ID:avaQuickBoot,项目名称:avaQuickBoot,代码行数:35,代码来源:Program.cs


示例8: EnsureSingleLoad

        private static bool EnsureSingleLoad()
        {
            bool result;
            var mutex = new System.Threading.Mutex(true, "Postworthy.Tasks.StreamMonitor", out result);

            return result;
        }
开发者ID:postworthy,项目名称:postworthy,代码行数:7,代码来源:Program.cs


示例9: Main

        public static void Main()
        {
            bool activo;
            System.Threading.Mutex m = new System.Threading.Mutex(true, "LimpiezasPalmeralForms", out activo);

            if (!activo)
            {
                MessageBox.Show("Ya se ha iniciado la aplicación");
                Application.Exit();
            }
            else
            {
                Application.EnableVisualStyles();
                Application.SetCompatibleTextRenderingDefault(false);
                try
                {
                    //Application.Run(new Login());
                    Application.Run(new PantallaPrincipal());
                }
                catch (Exception ex)
                {
                    System.Console.WriteLine(ex.Message.ToString() + '\n' + ex.StackTrace);
                }
            }
            // Se libera la exclusión mutua
            m.ReleaseMutex();
        }
开发者ID:pablovargan,项目名称:winforms-ooh4ria,代码行数:27,代码来源:Program.cs


示例10: Main

        static void Main()
        {
                        //Create mutex at application start and try to get the ownership
            using (var m = new System.Threading.Mutex(true, "HandGestureRecognition", out isNew))
            {
                //If application owns the mutex, continue the execution
                if (isNew)
                {
                    Application.EnableVisualStyles();
                    Application.SetCompatibleTextRenderingDefault(false);
                    Application.Run(new Form1());
                }
                //else show user message that application is running and set focus to that application window
                else
                {
                    MessageBox.Show("Application already running", "Info", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    Process current = Process.GetCurrentProcess();

                    foreach (Process process in Process.GetProcessesByName(current.ProcessName))
                    {
                        
                        if (process.Id != current.Id)
                        {

                            SystemParametersInfo((uint)0x2001, 0, 0, 0x0002 | 0x0001);
                            ShowWindowAsync(process.MainWindowHandle, WS_SHOWNORMAL);
                            SetForegroundWindow(process.MainWindowHandle);
                            SystemParametersInfo((uint)0x2001, 200000, 200000, 0x0002 | 0x0001);
                            break;
                        }
                    }
                }
            }
        }
开发者ID:phylony,项目名称:handview,代码行数:34,代码来源:Program.cs


示例11: Main

        static void Main(string[] args)
        {
            #region 处理来自参数的快速启动请求,跳过对OPCSERVER的三分钟等待
            foreach (string arg in args)
            {
                if (arg.Contains("fast"))
                {
                    waitMillionSecond = 1000;
                    break;
                }

            }
            #endregion
            bool createNew;
            //try
            //{
            //Console.WriteLine(Application.ProductName);
            using (System.Threading.Mutex m = new System.Threading.Mutex(true, "Global\\" + Application.ProductName, out createNew))
            {
                if (createNew)
                {
                    IniFile ini = new IniFile(AppDomain.CurrentDomain.BaseDirectory + "MicroDAQ.ini");

                    DatabaseManager = new DatabaseManager(ini.GetValue("Database", "Address"),
                                                        ini.GetValue("Database", "Port"),
                                                        ini.GetValue("Database", "Database"),
                                                        ini.GetValue("Database", "Username"),
                                                        ini.GetValue("Database", "Password"));
                    Application.EnableVisualStyles();
                    Application.SetCompatibleTextRenderingDefault(false);

                    Form MainForm = null;
                    while (!BeQuit)
                        try
                        {
                            MainForm = new MainForm();
                            //frmMain = new TestAlarm();
                            Application.Run(MainForm);
                        }
                        catch (Exception ex)
                        {
                            Console.WriteLine("OH. NO!" + ex.ToString());
                        }
                        finally
                        {
                            if (MainForm != null) MainForm.Dispose();
                        }
                    Environment.Exit(Environment.ExitCode);
                }
                else
                {
                    MessageBox.Show("程序已经在运行,无法再次启动。", "已启动", MessageBoxButtons.OK, MessageBoxIcon.Stop);
                }
            }
            //}
            //catch
            //{
            //    MessageBox.Show("Only one instance of this application is allowed!");
            //}
        }
开发者ID:qicb,项目名称:MicroDAQ,代码行数:60,代码来源:Program.cs


示例12: Main

 static void Main()
 {
     bool createNew;
     using (System.Threading.Mutex m = new System.Threading.Mutex(true, Application.ProductName, out createNew))
     {
         if (createNew)
         {
             Application.EnableVisualStyles();
             Application.SetCompatibleTextRenderingDefault(false);
             var printClient = new PrintClient();
             if (!printClient.IsServerOK)
             {
                 return;
             }
             //Application.Run(new Login());
             Application.Run(printClient);
         }
         else
         {
             MessageBox.Show("Only one instance of this application is allowed!");
         }
     } 
     //Application.EnableVisualStyles();
     //Application.SetCompatibleTextRenderingDefault(false);
     //Application.Run(new PrintClient());
     //Application.Run(new Login());
 }
开发者ID:Novthirteen,项目名称:sih-les,代码行数:27,代码来源:Program.cs


示例13: Pos

 public Pos(int Port, int PosID, int BaudRate = 9600)
 {
     this.Port = Port;
     this.POS_id = PosID;
     this.BaudRate = BaudRate;
     this.mutex = new System.Threading.Mutex();
 }
开发者ID:dalinhuang,项目名称:shool-card,代码行数:7,代码来源:Pos.cs


示例14: Main

        static void Main(string[] args)
        {
            bool ok;
            System.Threading.Mutex m = new System.Threading.Mutex(true, "__MSDN_HV2VIEWER_LADSOFT__", out ok);

            if (! ok)
            {
                if (args.Length == 1)
                    PipeClient.Write(args[0]);
            }
            else
            {
                AppDomain currentDomain = AppDomain.CurrentDomain;
                currentDomain.AssemblyResolve += new ResolveEventHandler(ResolveAssembly);

                Application.ApplicationExit += OnApplicationExit;

                PipeServer.Start(PipeServerCallback);
                Application.EnableVisualStyles();
                Application.SetCompatibleTextRenderingDefault(false);
                mainWindow = new Form1(args);
                Application.Run(mainWindow);
                GC.KeepAlive(m);                // important!
            }
        }
开发者ID:bencz,项目名称:OrangeC,代码行数:25,代码来源:Program.cs


示例15: Main

        static void Main()
        {
            Guid assemblyGuid = Guid.Empty;
              object[] assemblyObjects = System.Reflection.Assembly.GetEntryAssembly().GetCustomAttributes(typeof(System.Runtime.InteropServices.GuidAttribute), true);
              if (assemblyObjects.Length > 0)
              {
            assemblyGuid = new Guid(((System.Runtime.InteropServices.GuidAttribute)assemblyObjects[0]).Value);
              }

              bool granted;
              System.Threading.Mutex m = new System.Threading.Mutex(true, assemblyGuid.ToString(), out granted);

              if (granted)
              {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            Application.Run(new MainForm());
              }
              else
              {
            MessageBox.Show("Application is already running");
              }

              m.Close();
        }
开发者ID:scott-t,项目名称:iMote,代码行数:25,代码来源:Program.cs


示例16: Main

        static void Main()
        {
            bool SingleApp = bool.Parse(System.Configuration.ConfigurationManager.AppSettings["SingleApp"]);

            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            if (!SingleApp)
            {
                Application.Run(new Form1());
            }
            else
            {
                bool bExist = false;
                System.Threading.Mutex mm = new System.Threading.Mutex(true, "IntServer", out bExist);
                if (bExist)
                {
                    Application.Run(new Form1());
                    mm.ReleaseMutex();
                }
                else
                {
                    //查找窗体
                    IntPtr handle = WindowAPI.FindWindow(null, "WIFI-COM网络数据调试工具");
                    if (handle != IntPtr.Zero)
                    {
                        //恢复窗口并设置为前台窗口
                        WindowAPI.SetForegroundWindow(handle);
                        WindowAPI.OpenIcon(handle);
                        //PostMessage(handle, WM_SYSCOMMAND, (IntPtr)SC_DEFAULT, IntPtr.Zero);
                    }
                }
            }
        }
开发者ID:szlon,项目名称:WifiData,代码行数:33,代码来源:Program.cs


示例17: Main

        static void Main()
        {
            System.Threading.Mutex mutex = new System.Threading.Mutex(false, "{8D719F92_EA4B_41F1_9905_0C914E45414C_Ololap");
            try
            {
                if (mutex.WaitOne(0, false))
                {
                    // Run the application
                    Application.EnableVisualStyles();
                    Application.SetCompatibleTextRenderingDefault(false);
                    Application.Run(new MainForm());
                }
                else
                {
                    MessageBox.Show("Another instance of the application is already running.\nPlease close all the other instances if you want to open the new one",
                        "Application cannot be run", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
            }
            finally
            {
                if (mutex != null)
                {
                    mutex.Close();
                    mutex = null;
                }
            }

        }
开发者ID:Andrew414,项目名称:OLOLAPReport,代码行数:28,代码来源:Program.cs


示例18: Main

        static void Main()
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            bool result;
            var mutex = new System.Threading.Mutex(true, "IMgmt", out result);
            try
            {

                if (!result)
                {
                    MessageBox.Show("Another instance is already running.");
                    return;
                }
                if (!EventLog.SourceExists(sSource))
                    EventLog.CreateEventSource(sSource, sLog);
                Application.Run(new frmSplash());
                GC.KeepAlive(mutex);
            }
            catch (Exception ex)
            {
                EventLog.WriteEntry("Inventory Mgmt", ex.Message + ":" + ex.InnerException + ":" + ex.StackTrace.ToString());
            }
            finally
            {
                mutex.ReleaseMutex();
            }
        }
开发者ID:balavigneshs,项目名称:iMgmt,代码行数:28,代码来源:Program.cs


示例19: Main

        static void Main()
        {
            bool mutexCreated = false;
            System.Threading.Mutex mutex = new System.Threading.Mutex( true, @"Local\TimeTrack.exe", out mutexCreated );

            // only want once instance open at a time.
            // if the application is already open, then scan the processes to find out which
            // one it is, and give it focus.
            if(mutexCreated) {
                Application.EnableVisualStyles();
                Application.SetCompatibleTextRenderingDefault(false);
                Application.Run(new timerMainForm());
            } else {
                // scan the processes, find the one that is ours, and focus to it
                Process current = Process.GetCurrentProcess();
                foreach (Process process in Process.GetProcessesByName(current.ProcessName))
                {
                    if (process.Id != current.Id)
                    {
                        SetForegroundWindow(process.MainWindowHandle);
                        break;
                    }
                }
            }

            mutex.Close();
        }
开发者ID:reednj,项目名称:TimeTrack,代码行数:27,代码来源:Program.cs


示例20: Main

        static void Main()
        {
            bool tryCreateNewApp;
            mInstance = new System.Threading.Mutex(true, mAppName, out tryCreateNewApp);
            if (!tryCreateNewApp)
            {
                MessageBox.Show("The program has been already started.");
                return;
            }

            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            try
            {
                Application.Run(new EditorForm());
            }
            catch (Exception ex)
            {
                //Log.LogMessage(ex);
                //string message = "Send log file to developers for fixing problem.\r\nThe program will be closed.";
                //Log.LogMessage(message);
                //mInstance.ReleaseMutex();
                mInstance.Close();
                mInstance.Dispose();
                new ErrorRestart(KeyProgram.eDoctrinaOcrEd).ReStart(ex);
            }
        }
开发者ID:ifidakov,项目名称:trunk,代码行数:27,代码来源:Program.cs



注:本文中的System.Threading.Mutex类示例整理自Github/MSDocs等源码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
C# Threading.SendOrPostCallback类代码示例发布时间:2022-05-24
下一篇:
C# Threading.ManualResetEvent类代码示例发布时间:2022-05-24
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap