 |
|
 |
Thanks Rehan!
I exactly got what I was looking for.
Keep it up.
|
|
|
|
 |
|
 |
Well nice Idea and good presentation good keep goin .
Aamer A. Alduais
final_zero
My Favorite Qoute
"Faliure is the beginning of Success"
Aamer A. Alduais (^_^Me^_^)
|
|
|
|
 |
|
 |
Dear Aamer
Good day..!
Thanks for your comment and support.
Regards
Rehan
RAAbbasi
|
|
|
|
 |
|
 |
Please Vote For this article
RAAbbasi
modified on Monday, August 25, 2008 5:38 AM
|
|
|
|
 |
|
 |
I haven't yet at all looked into how one goes about limiting the number of instances of one's application which one allows to be running concurrently. Your code looked simple and straightforward, so I played around with it for a bit.
But then, in disappoving the "current" instance, I wanted to (optionally) focus the "previous" instance. I couldn't figure out how to do that, so I finally turned to Google. I came across this interesting article:
Single-Instance Windows Form[^]
I haven't yet tried the code given there, but looking at it I think it has what I want. Also, it has some interesting comments about using a mutex, etc.
|
|
|
|
 |
|
 |
Dear Ilíon....!
Good day...
To Get the focus on your previous running instance
you must use native methods calls ,i.e using [DllImport("user32.dll")]
and also the caption or title of the window.
My code is simple, it will just alert ....but it wont set focus to the running instance....
Regards
RAAbbasi
modified on Saturday, August 23, 2008 5:58 AM
|
|
|
|
 |
|
 |
Rehan Ahmad Abbasi wrote: To Get the focus on your previous running instance
you must use native methods calls ,i.e using [DllImport("user32.dll")]
and also the caption or title of the window.
That's how the page I referenced goes about it.
I noticed that there is a [process.MainWindowHandle] property ... if this returns a handle, it can be used to activate the previous instance (without need for the window's caption/title).
Rehan Ahmad Abbasi wrote: My code is simple, it will just alert ....but it wont set focus to the running instance....
Yes, I understand. It was that simplicity that got me interested enough to look deeper.
|
|
|
|
 |
|
 |
Dear Ilíon,
Good Day
I will try to use this " [process.MainWindowHandle] property "
and will let you know how it works.
Thanks
Rehan
RAAbbasi
|
|
|
|
 |
|
 |
It works; I've been using it in the testing I'm doing. My "if" was just a verbal hedge. What I meant is that if you do get a handle from the [process.MainWindowHandle] property, then this handle can be used to activate the previous instance.
modified on Tuesday, August 26, 2008 11:00 AM
|
|
|
|
 |
|
 |
Previously, I said:
It works; I've been using it in the testing I'm doing. My "if" was just a verbal hedge. What I meant is that if you do get a handle from the [process.MainWindowHandle] property, then this handle can be used to activate the previous instance.
There is a caveat ... if the process has two open windows, sometimes [process.MainWindowHandle] returns the handle of one window, sometimes of the other.
I don't know whether you've continued to work at this and have come up with a solution you're satisfied with.
If you'd like to take a look at what I've written, I can email the source to you. I plan to submit an article on in, but that won't be for another week or two.
If you'd like me to email this source to you, either post your email address here or drop me a note at ilion7@hotmail.com
What I've come up with is code which can be used to "register" (and retrieve) the ProcessIds within programmer-defined categories, and "register" (and retrieve) the .MainWindowHandle of the process(es). These registrations are kept in an .XML file created/managed by my code.
|
|
|
|
 |
|
 |
Very helpful, but I want to go a step further. I have 2 versions of the same EXE in different directories: one for development, one for acceptance testing. The EXE has the same name, the only difference is the connection-string in the exe.config.
I want these two EXE to be able to run side-by-side, however I do not want two instances of the development version or two instances of the acceptance version to be active.
Can I use some compiler option that sets the ProcessName or do I need something different altogether?
|
|
|
|
 |
|
|
 |
|
 |
Hi RAAbbasi,
Thanks for the help. Will try this, but am afraid IT Support does not want different version.
Richard
|
|
|
|
 |
|
 |
Dear RichardDeZwart...!
To allow two exes to be running for different reasons such as acceptance exe....and noraml exe...we need to differentiate between then...thats why I told to use "Application.ProductVersion"
regards
Rehan
RAAbbasi
|
|
|
|
 |
|
 |
I'm working on some ideas which may help in your situation.
Or, if you'd like to explore this yourself, I'm starting with the code I found on this page: Single-Instance Windows Form[^] (no doubt you could find other examples, also) One thing I did notice is that Mr Abbasi's method of getting the running processes [ Process.GetProcessesByName (Process.GetCurrentProcess().ProcessName) ] seems to me more reliable than the method on that page [ Process.GetProcessesByName (Application.ProductName) ]
One disappointment: a statement such as [ Control ctl = Control.FromChildHandle(handle) ] doesn't work in this situation, it returns a null object. Apparently, the 'handle' argument for the .FromChildHandle(handle) method needs to have been created by the .NET Framework, but in this cross-process situation the handle we get using the "user32.dll" imported methods is created by Win32.
|
|
|
|
 |
|
 |
Dear Ilíon,
Thanks for your interest on this topic and giving me a fine article..
"Single-Instance Windows Form[^]" .
With this article I can look further deep on this point.
Regards
Rehan
RAAbbasi
|
|
|
|
 |
|
 |
Thank you for pointing me in this direction. I will try some of the tips from the article.
Richard
|
|
|
|
 |
|
 |
I, of course, don't know whether you've come up with a solution you're satisfied with.
If you'd like to take a look at what I've written, I can email the source to you. I plan to submit an article on in, but that won't be for another week or two.
If you'd like me to email this source to you, either post your email address here or drop me a note at ilion7@hotmail.com
RichardDeZwart wrote: Can I use some compiler option that sets the ProcessName or do I need something different altogether?
What I've come up with is code which can be used to "register" (and retrieve) the ProcessIds within programmer-defined categories, and "register" (and retrieve) the .MainWindowHandle of the process(es). These registrations are kept in an .XML file created/managed by my code.
|
|
|
|
 |
|
 |
if you change the process name, the check will fail and it'll allow multiple instances..
I suggest using a socket (works cross platform as well) or a Mutex (not sure if mono got this working properly already), they are more reliable as it's a program specific thing that cannot be easily changed.
|
|
|
|
 |
|
 |
I agree with you...but here we assume that we are not going to change the process name.Mutex is a better way.Soon i will write about it in this article.
RAAbbasi
|
|
|
|
 |