Laggetjeuh
(Just in Town)
2009-05-05 08:49 AM
Check if process is running and activate

Hi,

This is my first post here, I've used the search function but couldn't find what I was looking for...

So here's my problem/request:
I've created an application using kixscript and kixforms.

When this application is launched it starts another executable (program2.exe). This program2.exe is started minimized by using the "start /MIN program2.exe" command.

I created a button that launches program2.exe when it's clicked.
However when someone clicks this button it launches another instance of program2.exe, instead of maximizing it from the taskbar.

So I created some sort of workaround that kills program2.exe using the StopProc() process (found here on kixtart.org) and then launches it again.

Unfortunately this is not the way it should work, program2.exe should keep on running and maximize when someone clicks the button in the application.
It should not be running twice either.

So I was wondering if there's a way to do the following:

- Check if program2.exe is running
- Maximize/activate program2.exe if it is running

If this would exist (for example functions called ProcRunning and ProcActivate) I can then put this function under the button like:

If ProcRunning(program2.exe)
ProcActivate(program2.exe)
Else
Run program2.exe
EndIf

I hope someone can help me out.
Thanks in advance!

Regards,
Jeroen


Mart
(KiX Supporter)
2009-05-05 09:16 AM
Re: Check if process is running and activate

First a small comment.
This post does not belong in this section. It should be in basic or advanced scripting. Can a MOD please move it?

You can control an application (minimize, maximize, restore and close) with the code below.

 Code:
Break on

$app = "Untitled - Notepad"
$action = 2
$rc = AppWindow($app, $action)

Function AppWindow($app, $action)
	Dim $rc
	Select
			;minimize alt + space + n
		Case $action = 1
			$keys = "~n"
			;maximize alt + space + x
		Case $action = 2
			$keys = "~x"
			;restore alt + space + r
		Case $action = 3
			$keys = "~r"
			;close alt + F4
		Case $action = 4
			$keys = "~{F4}"
	EndSelect
	
	If SetFocus($app) = 0
		$rc = SendKeys($keys)
	Else
		Exit @error
	EndIf
EndFunction


Laggetjeuh
(Just in Town)
2009-05-05 10:53 AM
Re: Check if process is running and activate

oh sry, wasn't sure whether to post it in basic or advanced scripting (don't consider myself an advanced scripter yet), but I thought I posted this in basic scripting..

I'll test the function right away!
Thanks!


Mart
(KiX Supporter)
2009-05-05 11:00 AM
Re: Check if process is running and activate

That’s OK. A mod moved it already.

If you have any questions then please post them.


Laggetjeuh
(Just in Town)
2009-05-05 11:13 AM
Re: Check if process is running and activate

The function seems to work, though not for my application \:\(
This is not the functions fault, but the application's because the application doesn't have a maximize/minimize button or function.

(so starting it with the /MIN parameter was already a cheap way of minimizing an unminimizable application, rightclicking the application in the taskbar also doesn't show a maximize option)

I'll see if I can get the creator of the application to include these options so I can use the function to maximize it.

Is there also a function to check if a process is running?
This would at least help me out for now so I can skip starting program2.exe if it's already running.

Thanks in advance!

Jeroen


Mart
(KiX Supporter)
2009-05-05 11:21 AM
Re: Check if process is running and activate

Have a look at the EnumProcces() UDF in the UDF section. This UDF will let you check if a process is running and if needed kill it.

Laggetjeuh
(Just in Town)
2009-05-05 12:56 PM
Re: Check if process is running and activate

thanks, that did the trick so far!