Wince C # The program can only run once

Source: Internet
Author: User

The code for the first method (this is recommended and I have tested it) is as follows:

[1 .]

Using system; using system. collections. generic; using system. windows. forms; // using rfidwarehouse. view; using system. runtime. interopservices; namespace phone {static class program {[dllimport ("coredll. DLL ")] Private Static extern int getlasterror (); [dllimport (" coredll. DLL ")] Private Static extern int releasemutex (intptr hmutex); [dllimport (" coredll. DLL ")] Private Static extern intptr createmutex (secur Ity_attributes lpmutexattributes, bool binitialowner, string lpname); Private const int error_already_exists = 0183; [structlayout (layoutkind. sequential)] public class security_attributes {public int nlength; Public int lpsecuritydescriptor; Public int binherithandle; }/// <summary> // main entry point of the application. /// </Summary> [mtathread] Static void main () {// application. run (New frmain (); intptr hmutex = createmutex (null, false, "futureproduct"); If (getlasterror ()! = Error_already_exists) {system. windows. forms. application. run (New frmain (); return;} else {MessageBox. show ("the program has been started. "); releasemutex (hmutex); Return ;}}}}

[2]
Method 2:

Public class mutex {[dllimport ("coredll. DLL ", entrypoint =" createmutex ", setlasterror = true)] public static extern intptr createmutex (intptr lpmutexattributes, bool initialowner, string mutexname); [dllimport (" coredll. DLL ", entrypoint =" releasemutex ", setlasterror = true)] public static extern bool releasemutex (intptr hmutex); Private const int error_already_exists = 0183; /// <summary> /// determine whether the program is running /// </Summary> /// <returns> /// true: the program is running, then do nothing // false: The program is not running, start the program // </returns> Public static bool isexist () {string strappname = system. reflection. assembly. getexecutingassembly (). getname (). name; intptr hmutex = createmutex (intptr. zero, true, strappname); If (hmutex = intptr. zero) throw new applicationexception ("Failure creating mutex:" + marshal. getlastwin32error (). tostring ("X"); If (marshal. getlastwin32error () = error_already_exists) {releasemutex (hmutex); Return true ;}return false ;}}

 

[3 .]

Let the program start only once
-- Mutex sometimes needs to run only one instance at a time during program development.

The mutex class is a synchronization primitive that grants exclusive access to shared resources to only one thread.

When two or more threads need to access a shared resource at the same time, the system needs to use a synchronization mechanism to ensure that only one thread uses the resource at a time.

If a thread obtains the mutex, the second thread to obtain the mutex will be suspended until the first thread releases the mutex.


The following demonstrates the mutex class to ensure that the application only has a unique instance.
Using system; using system. collections. generic; using system. LINQ; using system. windows. forms; namespace allows the program to start only once {static class program {// <summary> // main entry point of the application. /// </Summary> [stathread] Static void main () {application. enablevisualstyles (); application. setcompatibletextrenderingdefault (false); bool bcreate; system. threading. mutex = new system. threading. mutex (false, "singile_instance_mutex", out bcreate); If (bcreate) {application. run (New form1 ();} else {MessageBox. show ("program started"); application. exit ();}}}}

I was also worried that some programs could only start the program once.

But why can't some programs work? The code is the same. Have you found out what the problem is ??

If none of the above methods work, try the following method:

[4 .]

Using system; using system. LINQ; using system. collections. generic; using system. windows. forms; using system. runtime. interopservices; namespace pdaapplication {static class program {/// <summary> /// main entry point of the application. /// </Summary> [mtathread] Static void main () {// application. run (New forapplicatinesman (); intptr hdlg = intptr. zero; hdlg = findwindow (null, "thiswindows"); // thiswindows: Main Application Form if (hdlg! = Intptr. zero) {setforegroundwindow (hdlg);} else {application. run (New forapplicatinesman () ;}} [dllimport ("coredll. DLL ")] public static extern intptr findwindow (string classname, String title); [dllimport (" coredll. DLL ")] public static extern void setforegroundwindow (intptr hwnd );}}

 

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.