473,626 Members | 3,413 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

The better mousetrap with a built-in trap

If one wants to assure that there is only one instance of an app running,
then the popular solution is to use a Mutex and a message box, telling the
user there is already one running. I decided to go one further, bring the
1st instance to the foreground, no message boxes. Impressive, I thought. So
the first instance stores the main window handle in shared memory (win32
calls), second instance gets the window handle from shared memory if it
exists, and do a win32 "SetForegroundW indow" call. It works great if the
window is not minimized. Next step, when minimized, do a win32 "ShowWindow "
call. Also works great, BUT this is the trap in my mousetrap. If the main
form opened a dialog (ShowDialog), and the user minimizes the dialog, then
of course the main form minimizes too. Now start the second instance, it
finds the handle in shared memory and does a "ShowWindow " call, showing the
main window beautifully, however, the modal dialog stays minimized.
Naturally, the main form is dead, and the user has to click the dialog's
icon in the task bar to bring it up.
I don't really expect a solution, I thought I'd share my experience, and
revert back to the mutex and the message box.
Nov 20 '05 #1
4 1283
Check out the Windows Forms Tips and Tricks page.

http://www.bobpowell.net/tipstricks.htm

--
Bob Powell [MVP]
Visual C#, System.Drawing

All you ever wanted to know about ListView custom drawing is in Well Formed.
http://www.bobpowell.net/currentissue.htm

Answer those GDI+ questions with the GDI+ FAQ
http://www.bobpowell.net/gdiplus_faq.htm

Read my Blog at http://bobpowelldotnet.blogspot.com

"Chris Botha" <chris_s_botha@ AT_h.o.t.m.a.i. l.com> wrote in message
news:Oh******** ******@TK2MSFTN GP10.phx.gbl...
If one wants to assure that there is only one instance of an app running,
then the popular solution is to use a Mutex and a message box, telling the
user there is already one running. I decided to go one further, bring the
1st instance to the foreground, no message boxes. Impressive, I thought. So the first instance stores the main window handle in shared memory (win32
calls), second instance gets the window handle from shared memory if it
exists, and do a win32 "SetForegroundW indow" call. It works great if the
window is not minimized. Next step, when minimized, do a win32 "ShowWindow " call. Also works great, BUT this is the trap in my mousetrap. If the main
form opened a dialog (ShowDialog), and the user minimizes the dialog, then
of course the main form minimizes too. Now start the second instance, it
finds the handle in shared memory and does a "ShowWindow " call, showing the main window beautifully, however, the modal dialog stays minimized.
Naturally, the main form is dead, and the user has to click the dialog's
icon in the task bar to bring it up.
I don't really expect a solution, I thought I'd share my experience, and
revert back to the mutex and the message box.

Nov 20 '05 #2
Bob, using "ShowWindowAsyn c" instead of "ShowWindow " was the key. Thanks.

"Bob Powell [MVP]" <bob@_spamkille r_bobpowell.net > wrote in message
news:uB******** *****@TK2MSFTNG P10.phx.gbl...
Check out the Windows Forms Tips and Tricks page.

http://www.bobpowell.net/tipstricks.htm

--
Bob Powell [MVP]
Visual C#, System.Drawing

All you ever wanted to know about ListView custom drawing is in Well Formed. http://www.bobpowell.net/currentissue.htm

Answer those GDI+ questions with the GDI+ FAQ
http://www.bobpowell.net/gdiplus_faq.htm

Read my Blog at http://bobpowelldotnet.blogspot.com

"Chris Botha" <chris_s_botha@ AT_h.o.t.m.a.i. l.com> wrote in message
news:Oh******** ******@TK2MSFTN GP10.phx.gbl...
If one wants to assure that there is only one instance of an app running, then the popular solution is to use a Mutex and a message box, telling the user there is already one running. I decided to go one further, bring the 1st instance to the foreground, no message boxes. Impressive, I thought.

So
the first instance stores the main window handle in shared memory (win32
calls), second instance gets the window handle from shared memory if it
exists, and do a win32 "SetForegroundW indow" call. It works great if the
window is not minimized. Next step, when minimized, do a win32

"ShowWindow "
call. Also works great, BUT this is the trap in my mousetrap. If the main form opened a dialog (ShowDialog), and the user minimizes the dialog, then of course the main form minimizes too. Now start the second instance, it
finds the handle in shared memory and does a "ShowWindow " call, showing

the
main window beautifully, however, the modal dialog stays minimized.
Naturally, the main form is dead, and the user has to click the dialog's
icon in the task bar to bring it up.
I don't really expect a solution, I thought I'd share my experience, and
revert back to the mutex and the message box.


Nov 20 '05 #3
Bob, there is a subtle bug in your single instance app code. The 2 lines
minimizing and restoring the form, use
ShowWindowAsync (RunningProcess es(0).MainWindo wHandle ...
Nine out of ten times it work, but then every now and then the original
instance occurs in
RunningProcesse s(1) and not RunningProcesse s(0), and testing for a null
handle and if so, use the second process in the array, fixes it.

"Bob Powell [MVP]" <bob@_spamkille r_bobpowell.net > wrote in message
news:uB******** *****@TK2MSFTNG P10.phx.gbl...
Check out the Windows Forms Tips and Tricks page.

http://www.bobpowell.net/tipstricks.htm

--
Bob Powell [MVP]
Visual C#, System.Drawing

All you ever wanted to know about ListView custom drawing is in Well Formed. http://www.bobpowell.net/currentissue.htm

Answer those GDI+ questions with the GDI+ FAQ
http://www.bobpowell.net/gdiplus_faq.htm

Read my Blog at http://bobpowelldotnet.blogspot.com

"Chris Botha" <chris_s_botha@ AT_h.o.t.m.a.i. l.com> wrote in message
news:Oh******** ******@TK2MSFTN GP10.phx.gbl...
If one wants to assure that there is only one instance of an app running, then the popular solution is to use a Mutex and a message box, telling the user there is already one running. I decided to go one further, bring the 1st instance to the foreground, no message boxes. Impressive, I thought.

So
the first instance stores the main window handle in shared memory (win32
calls), second instance gets the window handle from shared memory if it
exists, and do a win32 "SetForegroundW indow" call. It works great if the
window is not minimized. Next step, when minimized, do a win32

"ShowWindow "
call. Also works great, BUT this is the trap in my mousetrap. If the main form opened a dialog (ShowDialog), and the user minimizes the dialog, then of course the main form minimizes too. Now start the second instance, it
finds the handle in shared memory and does a "ShowWindow " call, showing

the
main window beautifully, however, the modal dialog stays minimized.
Naturally, the main form is dead, and the user has to click the dialog's
icon in the task bar to bring it up.
I don't really expect a solution, I thought I'd share my experience, and
revert back to the mutex and the message box.


Nov 20 '05 #4
Hmm odd. Thaks I'll check up on that.

--
Bob Powell [MVP]
Visual C#, System.Drawing

All you ever wanted to know about ListView custom drawing is in Well Formed.
http://www.bobpowell.net/currentissue.htm

Answer those GDI+ questions with the GDI+ FAQ
http://www.bobpowell.net/gdiplus_faq.htm

Read my Blog at http://bobpowelldotnet.blogspot.com

"Chris Botha" <chris_s_botha@ AT_h.o.t.m.a.i. l.com> wrote in message
news:uw******** ******@tk2msftn gp13.phx.gbl...
Bob, there is a subtle bug in your single instance app code. The 2 lines
minimizing and restoring the form, use
ShowWindowAsync (RunningProcess es(0).MainWindo wHandle ...
Nine out of ten times it work, but then every now and then the original
instance occurs in
RunningProcesse s(1) and not RunningProcesse s(0), and testing for a null
handle and if so, use the second process in the array, fixes it.

"Bob Powell [MVP]" <bob@_spamkille r_bobpowell.net > wrote in message
news:uB******** *****@TK2MSFTNG P10.phx.gbl...
Check out the Windows Forms Tips and Tricks page.

http://www.bobpowell.net/tipstricks.htm

--
Bob Powell [MVP]
Visual C#, System.Drawing

All you ever wanted to know about ListView custom drawing is in Well

Formed.
http://www.bobpowell.net/currentissue.htm

Answer those GDI+ questions with the GDI+ FAQ
http://www.bobpowell.net/gdiplus_faq.htm

Read my Blog at http://bobpowelldotnet.blogspot.com

"Chris Botha" <chris_s_botha@ AT_h.o.t.m.a.i. l.com> wrote in message
news:Oh******** ******@TK2MSFTN GP10.phx.gbl...
If one wants to assure that there is only one instance of an app running, then the popular solution is to use a Mutex and a message box, telling the user there is already one running. I decided to go one further, bring the 1st instance to the foreground, no message boxes. Impressive, I thought.
So
the first instance stores the main window handle in shared memory
(win32 calls), second instance gets the window handle from shared memory if it exists, and do a win32 "SetForegroundW indow" call. It works great if the window is not minimized. Next step, when minimized, do a win32

"ShowWindow "
call. Also works great, BUT this is the trap in my mousetrap. If the main form opened a dialog (ShowDialog), and the user minimizes the dialog, then of course the main form minimizes too. Now start the second instance, it finds the handle in shared memory and does a "ShowWindow " call, showing the
main window beautifully, however, the modal dialog stays minimized.
Naturally, the main form is dead, and the user has to click the

dialog's icon in the task bar to bring it up.
I don't really expect a solution, I thought I'd share my experience, and revert back to the mutex and the message box.



Nov 20 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
2268
by: JimmyT | last post by:
I just configured and installed 2.3.4 and noticed there is no datetime module. I noticed there is a datetimemodule.c file that did not get built (ie no object file). Is there something I need to do to my system to make this module? Thanks, Jim
1
3105
by: Alex Elbert | last post by:
Hi I have built dynamic HTMLTable. Now I want to attach it directly to the Email Body - it is already built, so why not to use a ready table. However, I cannot find the way of getting plain HTML text out of dynamically built control. I tried to put my table between div and read div.innerHTML then - HTTP exception has been thrown. Any thoughts, ladies and gentelmen
0
2182
by: Andrew Crook | last post by:
does MYSQL have a quota built into it! I need it limit the size of each database AndiC
1
1885
by: Mark | last post by:
Is there a way to execute a statement that is built dynamically by a .NET application. For example I have a loop that is reading values from a database and I want to do something like the following. Dim stringa as string = "response.write somavalue" Execute(stringa) I realize this is not the best way to do this but it is
4
6569
by: Yasutaka Ito | last post by:
Hi, Is there a way to determine which version of .NET Framework any given assembly is built with? thanks! -Yasutaka
1
2094
by: William | last post by:
Looking for a pre built dotnet corporate or small business website template.
1
2175
by: William | last post by:
Looking for a pre built dot net website for consulting business. I am trying to put up a quick business web for a dot net frame work. I have a provider already. I am trying to save time. Any ideas will be appreciated. I will look at anything that can be modified VS 2003.
1
1544
by: Daniel | last post by:
is there any way to get to a unique build verion of an assembly at runtime? e.g. a version that is unique to the time that the assembly was built?
48
4916
by: meyer | last post by:
Hi everyone, which compiler will Python 2.5 on Windows (Intel) be built with? I notice that Python 2.4 apparently has been built with the VS2003 toolkit compiler, and I read a post from Scott David Daniels where he said that probably the VS2003 toolkit will be used for Python 2.5 again. However, even before the release of Python 2.5, I cannot seem to find many retailers around here that still carry Visual Studio 2003, and some were a...
3
1775
by: drewj840 | last post by:
I built a Windows service that sweeps a set of folders every 60 seconds and puts the files into a SQL Server database. I am creating a second service that will delete this set of folders and recreate them each night based on a list of active customers. I need to be able to stop the first service before starting the second service and then when the second service completes restart the first service. I haven't the slightest idea how to proceed.
0
8262
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8637
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8364
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8502
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7192
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5571
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4090
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
2623
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
1507
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.