 |
|

|
To make this great class work on Terminalservers I changed the following:
The mutex may not be created gobally, remove Global\\ from the mutex name.
In GetCurrentInstanceWindowHandle() you have to check that both processes are in the same session
if (_process.Id != process.Id &&
_process.SessionId == process.SessionId &&
_process.MainModule.FileName == process.MainModule.FileName &&
_process.MainWindowHandle != IntPtr.Zero)
{
hWnd = _process.MainWindowHandle;
break;
}
modified 7-Mar-13 10:17am.
|
|
|
|

|
Thanks but the code only prevents the application from running more than one instance but what if we have to pass command line arguments to the already running instance.
Im working on a Mediaplayer, the coding is almost done for the basic tasks such as playing, showing and editing the playlist, showing ID3 tags,etc etc. But I need to add the functionality to add files from Windows Explorer to the current playlist in the running instance.
So any ideas as to how to pass command line arguments to the already running app?
|
|
|
|

|
In that case you have to develop some different kind of interface. may be some shared memory or a temp file where, every new instance will append new parameters. Another background thread will keep it waiting for new parameters.
Manish Agarwal
manish.k.agarwal @ gmail DOT com
|
|
|
|
|
|
|
|

|
Never had a sample-code that was so easy to "port" into my real application
Works great.
THX
Markus
|
|
|
|
|

|
The code seems to work perfect adding some loglines... but still 2 of my processes are started...
|
|
|
|

|
I can help you further, if you can share your code.
Manish Agarwal
manish.k.agarwal @ gmail DOT com
|
|
|
|

|
Thanks for this easy to use code! Your name is on my credit list!
|
|
|
|

|
Thanks
Manish Agarwal
manish.k.agarwal @ gmail DOT com
|
|
|
|
|

|
hi
I am using smartclient application.
i have checked in processes if the application is opened then it will not open another instance.
When an application is opened and i try to open another instance it does not allow to open it. Till here it works fine. but when i close the already opened application and then again try to open it, the application crashes saying the Shell has encountered a problem. Then i have to unistall and again reinstall the application.
Please provide a solution for it.
thx
|
|
|
|

|
Please check your application code how you are closing it. I never tested this on a smart client so no comments.
Manish Agarwal
manish.k.agarwal @ gmail DOT com
|
|
|
|

|
Hi Manish,
I found that the Single Instance Application you run as single instance actually is based on the process name.
I launched the same application binary by renaming it and it launched perfectly. Can you look into this matter and provide necessary comments.
With Best Regards,
Abhishek Dey
|
|
|
|

|
yes, you can easily resolve this issue by a minor change in IsAlreadyRunning() function. Just change the following line
string sExeName = fileInfo.Name;
to
string sExeName = "someuniquestring";
Manish Agarwal
manish.k.agarwal @ gmail DOT com
|
|
|
|

|
hey thanks manish!
modified on Thursday, May 27, 2010 6:18 AM
|
|
|
|

|
Thanks for that been a great help.
Have you any idea how to apply this to a Windows CE program
Chris
|
|
|
|

|
I think, it shld simply work on Compact framework too but I never tried the same.
Manish Agarwal
manish.k.agarwal @ gmail DOT com
|
|
|
|

|
Thanks for the code. It helped me.
|
|
|
|
|

|
| Your solution seems difficult to use. Manish Agarwal
manish.k.agarwal @ gmail DOT com
|
|
|
|

|
Thanks, thats exactly what I've been trying to do.
TVMU^P[[IGIOQHG^JSH`A#@`RFJ\c^JPL>;"[,*/|+&WLEZGc`AFXc!L
%^]*IRXD#@GKCQ`R\^SF_WcHbORY87֦ʻ6ϣN8ȤBcRAV\Z^&SU~%CSWQ@#2
W_AD`EPABIKRDFVS)EVLQK)JKQUFK[M`UKs*$GwU#QDXBER@CBN%
R0~53%eYrd8mt^7Z6]iTF+(EWfJ9zaK-iTV.C\y<pjxsg-b$f4ia>
-----------------------------------------------
128 bit encrypted signature, crack if you can
|
|
|
|

|
It saved me quite a bit of time. Thanks for the post!
|
|
|
|

|
For sure, it's amazing how little code can be used to accomplish this. I had thought it would be much more complicated.
Martin of teva flip flops
|
|
|
|

|
My code looks like this:
.....
if(myAppIsNotRunningYet) {
MyTray = new MyTray();
Application.Run();
}
else {
}
.....
public MyTray()
{
notifyIcon = new NotifyIcon();
....
notifyIcon.Visible = true;
}
private void notifyIcon_DoubleClick(object sender, EventArgs e)
{
MainForm mainForm = new MainForm();
mainForm.ShowDialog();
}
|
|
|
|

|
You can do something like:
if(!SingleInstance.SingleApplication.Run())
{
return;
}
else
{
MyTray = new MyTray();
Application.Run();
}
Manish Agarwal
manish.k.agarwal @ gmail DOT com
|
|
|
|

|
how can i restore a hidden form ?
pls help me..
|
|
|
|

|
Can you elaborate your problem little bit more. Is it related my posted code or a general problem.
Manish Agarwal
manish.k.agarwal @ gmail DOT com
|
|
|
|

|
Thank you for reply.
sorry for my poor English.
if i hide my form like
this.Hide();
how can i show my running applications main form when user double click the application again.
i really need this ..
|
|
|
|

|
You can refer the code uploaded with this article.
Manish Agarwal
manish.k.agarwal @ gmail DOT com
|
|
|
|

|
i have this problem too,
are there any solutions, i want create a listener, where the original instance start if a file or somethins is created....
|
|
|
|

|
Pls explain
Manish Agarwal
manish.k.agarwal @ gmail DOT com
|
|
|
|

|
Very simple and elegant solution, worked great for me the first time I tried it. And without any reference to any VB compatibility namespace! Really appreciate this a lot! - SC59
|
|
|
|

|
"GetCurrentInstanceWindowHandle()" does not work.Therefore "SwitchToCurrentInstance()" does not work.
|
|
|
|

|
You must be checking in debugger with VS2005 or VS2008. It is because of Hosting Process SingleInstance.vshost.exe created along with SingleInstance.exe. If you directly run the application without debugger you will see, application is working correctly.
Check following link to disable the Hosting Process-
http://msdn.microsoft.com/en-us/library/ms185330(VS.80).aspx[^]
Manish Agarwal
manish.k.agarwal @ gmail DOT com
|
|
|
|

|
Thank you very much.
Excuse me for my poor english.
You are right, Hosting Process SingleInstance.vshost.exe created.But it does not work.
I add MessageBox as below:
foreach(Process _process in processes)
{
MessageBox.Show(string.Format("ID={0}\nName:{1}\nHandle{2}",_process.Id,_process.MainModule.FileName,_process.MainWindowHandle));
if (_process.Id != process.Id &&
_process.MainModule.FileName == process.MainModule.FileName &&
_process.MainWindowHandle != IntPtr.Zero)
{
hWnd = _process.MainWindowHandle;
break;
}
}
In no-debug mode,I find all of the first and second process's MainWindowHandle are 0.Why?
My ide is VS2008.
|
|
|
|

|
I have tested with VS2005 working fine. Currently I don't have VS2008 setup with me .
Manish Agarwal
manish.k.agarwal @ gmail DOT com
|
|
|
|

|
Thanks again.
Lead to the reasons for this phenomenon is a set of "ShowInTaskbar=false".
This problem can be solved?
|
|
|
|

|
In this case MainWindowHandle will be NULL so the current code will not bring the already running process in focus but there will be only one instance.
Manish Agarwal
manish.k.agarwal @ gmail DOT com
|
|
|
|

|
Hi,
I have to open application when a file (.ext) is clicked (File is associated with that application like .doc with WINWORD).
Application should be single instance. When I click the .ext file it should open the application with that content. If an instance is runnig it should ask the user whether you want to close this application and then open the new .ext file.
Need help in C#.
Thanks
Surya
|
|
|
|

|
When an extension is associated with an application and user click on a file with that extension, the file name is passed as a command line argument. You can look for the command line argument at launch time and process the request accordingly. But this need be done before SingleInstance.SingleApplication.Run(new FrmMain());
Manish Agarwal
manish.k.agarwal @ gmail DOT com
|
|
|
|

|
Hi Manish,
Thanks for the reply.
When I click on .ext file for the second time i.e already an instance is running. Now the clicked .ext file should be opened in the application.
Can you please tell me how to do it?
Thank You,
Surya
|
|
|
|

|
For this you have implement some shared memory. Where you can write the shared argument (file name) and some way to notify the first process that something is there in shared memory to process.
Manish Agarwal
manish.k.agarwal @ gmail DOT com
|
|
|
|

|
this code very useful for me
Senthil.S
Software Engineer
|
|
|
|

|
Thank You Sir....
This is what I need........
Sagar Pattnayak
Software Developer
Sun-Dew Solutions
+91-9831169962
|
|
|
|

|
u my hero
noname
|
|
|
|
 |