-How to ensure that the application only runs on one instance

Source: Internet
Author: User
Tags custom name

Address: http://www.cnblogs.com/zhangronghua/archive/2006/11/17/564085.html

Idea: You can have a variable to indicate whether the program has been started!

1. Use the Registry... When the program is started for the first time, a node (custom name) is written to the registry, and the written node is deleted when it is closed... check whether the program exists at startup. If yes, the system prompts "started". Otherwise, write and start the program ..

Static class Program
{
/// <Summary>
/// Main entry point of the application.
/// </Summary>
[Stathread]
Static void main ()
{
Application. enablevisualstyles ();
Application. setcompatibletextrenderingdefault (false );
Try
{
// Microsoft. win32.registry key = Microsoft. win32.registry. localmachine. opensubkey (@ "SOFTWARE \ wo9421 ");
Microsoft. win32.registrykey = Microsoft. win32.registry. localmachine. opensubkey (@ "SOFTWARE \ wo9421 ");
If (Key = NULL)
{
Microsoft. win32.registrykey writekey = Microsoft. win32.registry. localmachine. createsubkey (@ "SOFTWARE \ wo9421 ");
Writekey. setvalue ("instanceflag", "1 ");
Application. Run (New form1 ());
}
Else
{
MessageBox. Show ("program started ");
}
}
Catch (exception ex)
{
MessageBox. Show (ex. Message );
}
// Application. Run (New form1 ());
}
}

PS: It is registrykey, not registry. Otherwise, an error is prompted: the variable of the static type "Microsoft. win32.registry" cannot be declared.

PS: (@ "SOFTWARE \ wo9421") is a custom name, which must be consistent before and after.

PS: You must delete this information when the program is closed. Otherwise, the program cannot be started later! For example, add the following code to the form1_formclosing event:

Private void form=formclosing (Object sender, formclosingeventargs E)
{
Try
{
Microsoft. win32.registrykey = Microsoft. win32.registry. localmachine. opensubkey (@ "software", true );
If (key! = NULL)
{
Key. deletesubkey (@ "wo9421 ");
}
}
Catch (exception ex)
{
MessageBox. Show (ex. Message );
}
}

PS: software rather than SOFTWARE \ wo9421.. is enabled, and the parameter "true" indicates that the file can be written. Otherwise, the file cannot be deleted ..

PS: The wo9421 node under software cannot be deleted incorrectly.

A disadvantage of using the registry: When an exception is disabled, the closing event cannot be executed, registry information cannot be deleted, and thus cannot be started .. (if the program is closed through the task manager when the program is running, it cannot be opened again and a prompt is displayed that the program has been started)

2. Use mutex (similar to using threads)

The mutex object can be used to protect shared resources from being accessed by multiple threads or processes at the same time. The status of the mutex object can be set to terminate (when it does not belong to any thread), or set to non-terminate (when it belongs to a thread ). At the same time, only one thread can own one mutex object.

Static class Program
{
/// <Summary>
/// Main entry point of the application.
/// </Summary>
[Stathread]
Static void main ()
{
Application. enablevisualstyles ();
Application. setcompatibletextrenderingdefault (false );
Bool bcreatednew = true;
System. Threading. mutex Mt = new system. Threading. mutex (false, "9421 frame", out bcreatednew );
If (bcreatednew)
{

Application. Run (New form1 ());
}
Else
{
MessageBox. Show ("program started ");
}
// Application. Run (New form1 ());
}
}

PS: when giving mutex a name, it should not be too simple to prevent repeated mutex with other programs, thus failing to achieve the expected effect.

3. Use Process ?)

 

Static class Program
{
/// <Summary>
/// Main entry point of the application.
/// </Summary>
[Stathread]
Static void main ()
{
Application. enablevisualstyles ();
Application. setcompatibletextrenderingdefault (false );
If (runinginstance () = NULL)
{
Application. Run (New form1 ());
}
Else
{
MessageBox. Show ("program started ");
}
}

Public static system. Diagnostics. Process runinginstance ()
{
System. Diagnostics. Process current = system. Diagnostics. process. getcurrentprocess ();
System. Diagnostics. Process [] processes = system. Diagnostics. process. getprocessesbyname (current. processname );

Foreach (system. Diagnostics. Process pro in processes)
{
// Ignore the current process
If (PRO. ID! = Current. ID)
{
// Make sure that the process is running from the EXE file.
If (system. reflection. Assembly. getexecutingassembly (). Location. Replace ("/", "\") = current. mainmodule. filename)
{
// Return the other process instance
Return pro;
}
}
}

// No other instance was found, return null.
Return NULL;
}
}

 

 

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.