本文整理汇总了C#中System.Threading.Mutex类的典型用法代码示例。如果您正苦于以下问题:C# System.Threading.Mutex类的具体用法?C# System.Threading.Mutex怎么用?C# System.Threading.Mutex使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
System.Threading.Mutex类属于命名空间,在下文中一共展示了System.Threading.Mutex类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的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);
}
示例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;
}
}
示例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!
}
示例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( );//退出程序
}
}
示例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が起動されています。" );
}
}
}
示例6: GetMutex
public bool GetMutex()
{
bool acquired;
_appMutex = new System.Threading.Mutex(true, "KeyMapperAppMutex", out acquired);
return acquired;
}
示例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();
}
}
示例8: EnsureSingleLoad
private static bool EnsureSingleLoad()
{
bool result;
var mutex = new System.Threading.Mutex(true, "Postworthy.Tasks.StreamMonitor", out result);
return result;
}
示例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();
}
示例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;
}
}
}
}
}
示例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!");
//}
}
示例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());
}
示例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();
}
示例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!
}
}
示例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();
}