Run a single instance of the application under WinCE

Source: Internet
Author: User

Implement applicationsProgramThere are many ways to run a single instance. If there is a window application, you can use findwindow () to find out whether a window with the same name already exists. For windowless applications, you can use the kernel objects such as mutex. In general, check whether an existing instance is running at the entrance of the program. If not, continue. If yes, exit this operation. Shows the basic process.

Single Instance reference implemented with mutexCodeAs follows.

Int _ Tmain ( Int Argc, _ tchar * Argv [])
{
Handle hmutex = Createmutex (null, false, text ( " Single_instance " ));

If ( ! Hmutex)
{
Retailmsg ( 1 , (Text ( " \ R \ nfailed to createmutex ()... " )));
Return   0 ;
}

If (Getlasterror () = Error_already_exists)
{
Retailmsg ( 1 , (Text ( " \ R \ napp already run, closehandle (% x )... " ), Hmutex ));
Closehandle (hmutex );
Return   0 ;
}

retailmsg ( 1 , (Text ( " \ r \ napp is running (% x )... " ), hmutex ));
sleep ( 3000 );
retailmsg ( 1 , (Text ( " \ r \ napp prepare to exit... " )));
sleep ( 2000 );

Retailmsg (1, (Text ("\ R \ napp exit (% x )..."), Hmutex ));
Return 0;
}

The reverse visualization and DVD playback in the project both use the tcc89 CIF interface, and they cannot run simultaneously. Among them, DVD playback is implemented by the application, while reversing is visible in the drive. During the playback of a DVD, if the playback process is reversing, you must first disable the playback process of the DVD, and then start the playback of the DVD when you exit reversing visualization. The exit time of the DVD player is long, that is, T3, which is about 2 seconds. Because the DVD player program runs on a single instance, starting the second instance within the time of T3 will fail, and the first instance will also quit. In this way, there will be no screen playing on the DVD after the video is switched out.

To solve this problem, you need to make some adjustments when checking the instance. If an instance is running and does not exit immediately, wait for a while before checking again. If the previous instance exits, continue to run the program. If the previous instance is still running, exit this operation. The waiting time can be set to 2 times of T3. The modified code is as follows.

Int _ Tmain ( Int Argc, _ tchar * Argv [])
{
Handle hmutex = Createmutex (null, false, text ( " Single_instance " ));

If ( ! hmutex)
{< br> retailmsg ( 1 , (text ( " \ r \ nfailed to createmutex ()... " )));
return 0 ;
}

If(Getlasterror ()=Error_already_exists)
{
Retailmsg (1, (Text ("\ R \ napp already run, closehandle (% x) 111..."), Hmutex ));
Closehandle (hmutex );

Sleep (4000);

Hmutex = Createmutex (null, false, text ( " Single_instance " ));
If ( ! Hmutex)
{
Retailmsg ( 1 , (Text ( " \ R \ nfailed to createmutex ()... " )));
Return   0 ;
}

If (Getlasterror () = Error_already_exists)
{
Retailmsg ( 1 , (Text ( " \ R \ napp already run, closehandle (% x) 222... " ), Hmutex ));
Closehandle (hmutex );
Return   0 ;
}
}

Retailmsg (1, (Text ("\ R \ napp is running (% x )..."), Hmutex ));
Sleep (3000);

Retailmsg (1, (Text ("\ R \ napp prepare to exit...")));
Sleep (2000);

Retailmsg (1, (Text ("\ R \ napp exit (% x )..."), Hmutex ));
Return 0;
}

Shows the running result before modification. It can be seen that the instances started in time t2 and T3 failed.

Shows the running result after modification. As you can see, after the first instance ends, the second instance starts to run, and other instances are closed one by one.

Comparing the results of the two executions, we can see that the second method should be improved. But will it bring new problems? The biggest fear of solving a bug is to solve one, but introduce another pile!

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.