Windows Mobile only runs one instance

Source: Internet
Author: User

To meet the needs of a projectProgramOnly one instance is running. I searched the internet for a long time and finally saw a reply on csdn. Get inspiration and complete this function.

It is mainly implemented using mutex objects.CodeAs follows:

Static   Class Program
{
[Dllimport ( " Coredll. dll " , Setlasterror =   True )]
Private   Static   Extern Intptr createmutex (security_attributes lpmutexattributes, Bool Binitialowner, String Lpname );

[Dllimport ("Coredll. dll", Setlasterror= True)]
Private Static Extern IntReleasemutex (intptr hmutex );

[Structlayout (layoutkind. Sequential)]
Public   Class Security_attributes
{
Public   Int Nlength;
Public   Int Lpsecuritydescriptor;
Public   Int Binherithandle;
}

Const IntError_already_exists= 0183;

///   <Summary>
/// The main entry point of the application.
///   </Summary>
[Mtathread]
Static   Void Main ()
{
Intptr hmutex = Createmutex ( Null , False , " Standardworkman " );
If (Marshal. getlastwin32error () ! = Error_already_exists)
{
Application. Run ( New Formworklist ());
}
Else
{
MessageBox. Show ( " A program has been started. Do not open it again " , " System prompt " ,
Messageboxbuttons. okcancel, messageboxicon. Asterisk, messageboxdefaultbutton. button1 );
Releasemutex (hmutex );
Application. Exit ();
}

}
}

 

The above code is completely normal. My problem is that most of them wrote it very early. The problem is as follows:

[Dllimport ( " Coredll. dll " , Setlasterror =   True )]
Private   Static   Extern   Int Getlasterror ();

.

If (Getlasterror () ! = Error_already_exists)
{
Application. Run (NewFormworklist ());
}

 

I use getlasterror () in the platform call, and the result is always desired. No matter how many instances are opened, the value of getlasterror () is always 6 (invalid_handle_value )???? I am puzzled. Let us explain it. Thank you.

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.