Automatically activates the first instance when only one instance is running and an attempt is made to run the second instance

Source: Internet
Author: User
Tags mutex

This article supporting source code

Many programs, such as Winamp,ttplayer,realplayer, and so on are automatically activated before an instance of the function, in fact, this thing is very simple, in VB can be used app.hinstance to determine whether or not to run an instance of the program, Use the API function FindWindow to find the handle to the previous instance, thus preparing for activating her. But App.hinstance has a lot of limitations, an EXE that doesn't require support from other resources, and if you copy to another directory, the replica can run unimpeded, regardless of whether his predecessors are running or not, if the program needs to monopolize a particular hardware or other resource, There is a potential conflict, so we have to look for other reliable ways to ensure that our work is free of any duality.

The concept of mutexes in Windows makes it easy to solve the above problems. We create a specific mutex at the first run of the program, and when the second or more times run, the program creates a mutex, and we catch the error and properly handle it to ensure that the program is running uniquely.

So how to activate the previous instance, don't worry, Windows API function is rich, we as long as the program handle (the same as people's identity card, the system will guarantee his uniqueness), you can do random operation, Showwindow,setforeground, SetActiveWindow and so on. How to get this ID, FindWindow, yes, a lot of people would say that. Yes, it's a method, and it's a good function, but the sensitivity of her program title makes it a lot of cases that we have to abandon her. For example, winmap her title will change as the song is played. How to do, not urgent, since the handle is unique, why we do not start after the program to record her on our hard disk.

Well, the previous analysis made us theoretically able to achieve a winamp effect, so why not add something new: When we activate the first form, do we let her blink a few times to get the user's attention? FlashWindow effect, yes, is FlashWindow, take a closer look at her use methods, to be used with a timer to use, hey, to achieve this small function, but also with the timer, really a bit not cost-effective ah, and in sub main How to use the timer is also worth some research. Is there a simple way, of course, to look at the FlashWindowEx, the ex-extended function is not the same, she has a struct parameter can specify the number of flashes and the time of each flicker. cool!

Said a bunch, also put into practice, code, I want is code, don't worry, came.

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.