procedure for calling an exe only one time
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?
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
this should get you off on the right foot: http://www.webdevbros.net/2007/11/14/singelton-application-with-c/
精彩评论