views:

96

answers:

2

I'm using process.info, process start(); to call an exe on button click in c#.net, but every time I click on the button it calls an exe and opens a duplicate file on the taskbar. I want to just maximize the exe that was already on the taskbar.

I'm facing the problem that it is again and again opening the same file on the button click. Is there any way that it could open an exe only once and on the button click it could maximize the exe file if already opened rather than making duplicate entries?

A: 

this should get you off on the right foot: http://www.webdevbros.net/2007/11/14/singelton-application-with-c/

Joel
thanks for ur answer
zoya
is the code to be run on the button click event??
zoya
this is giving an error on IsFirstInstance() cannot declare an instace members on static class
zoya
It's a typeo. You have to make that method static: public static bool IsFirstInstance()
Joel
is there the other alternative to solve this problem?? i have gone through the code mutex.. is this the better option??if yes then how to use it on the button click event??
zoya
there is another error which im not able to resolve that is system.reflection.assembly does not contain a defination of GetEntyAssembly();
zoya
figure out where your bugs are. Besides the static problem, I was able to create an application that only allowed for one instance of it being open. Or maybe you should be a little more clear as to what you're trying to do... are the exe's you're launching apps you made? or are they other random applications?
Joel
the exe im launching is the third-party exe basically its another program to be made to run in my program.. the pbm im facing is that it is running the exe one time but its on focusing on that exe. im able to see my exe only when i close my form..
zoya
+1  A: 

Process.Start() returns a Process object. What you could do is have a class variable (for example Process startedProcess;) that is initialized when the button is clicked. If that variable is null that means the process hasn't started yet, and that application should be launched, otherwise it's already running, and we should ignore it.

Here is a basic example:

Process startedProcess = null;

public void button1_Clicked(object sender, EventArgs e)
{

     if ( startedProcess == null )
          startedProcess = Process.Start("path\\to\\process.exe");

}

If you are looking to automatically switch to that window in the case that the application is already running, .NET does not have any built in methods to do this natively. You will need to to DLLImports from user32.dll. An example can be found in the comments on this page : http://www.eggheadcafe.com/community/aspnet/14/21984/switch-to-another-runnin.aspx

Daniel Joseph