How to check whether the application is running
Article Metadata
Description.
Some time we need to findout whether some application/exe is running or not (specially if we want to launch them, if not running). The static class MySystemUtils contain two functions to check whether application/exe is running or not. The function IsApplicationRunningL() check whether application is running or not. TApaTask::FindApp( aApplicationUid ) doing job for IsApplicationRunningL() to find application is running or not, but fortunately FindApp() works for only Apps not for exe's, so need to write another function to find out whether exe is running or not.
Header Required:
#include <apacmdln.h>
#include <apgcli.h>
#include <apgtask.h>
#include <e32std.h>
#include <w32std.h>
Library Needed:
LIBRARY ws32.lib apgrfx.lib euser.lib
Header File MySystemUtils.h
class MySystemUtils
{
/**
* Description : Function to check
whether the applicaton is running.
* Params : UID of the application to be checked
* Returns : true if the application is running
*/
static TBool IsApplicationRunningL( const TUid& aApplicationUid );
/**
* Description : Function to check if the executable is
running or not
* Params : Descriptor containing name of Exe
* Returns : true if running
* Comments : format exename*
*/
static TBool IsExeRunning( const TDesC& aExeName );
};
Source code MySystemUtils.cpp
#include <MySystemUtils.h>
//Checking Whether the Application is Running
TBool MySystemUtils::IsApplicationRunningL( const TUid& aApplicationUid )
{
RWsSession windowSession;
User::LeaveIfError( windowSession.Connect() );
TApaTaskList apataskList( windowSession);
TApaTask apatask = apataskList.FindApp( aApplicationUid );
TBool situationFlag = apatask.Exists();
// Close window server session
windowSession.Close();
return situationFlag;
}
// Checking Whether the Exe is Running
TBool MySystemUtils::IsExeRunning( const TDesC& aExeName )
{
//make sure that the name of executable is without extension. i.e helloworld not helloworld.exe
TFileName executableName;
executableName.Copy( aExeName );
executableName.Trim();
executableName.Append(_L("*"));
TBool situationFlag = EFalse;
TFindProcess processSearch;
TFullName processFullName;
while( processSearch.Next( processFullName ) == KErrNone)
{
if( processFullName.Match(executableName) != KErrNotFound )
{
return ETrue;
}
}
return situationFlag;
}
How to use it ???
//you can call static method in your AppView/AppUI directly as follows.
if(MySystemUtils::IsExeRunning(KMyExeName))
{
//do your task
}
Related Links:


03 Sep
2009
Many a times it is required to identify if an application is running or not. This code snippet provides static function for checking the same. It is a very simple and straight forward example and is self explanatory.
Izinin - MySystemUtils::IsApplicationRunningL implementation is incorrect
Above check for application running is incorrect because according to documentation:
"Tests whether this TApaTask object is empty. This object represents the state of the task at the time at which it was constructed and is not subsequently updated. Therefore, this does not indicate that the task itself exists and should not be used to test whether or not a particular task is running or not. "
thus this function will return 'true' even application of interest has already exited (verified)izinin 15:34, 22 December 2011 (EET)
Hamishwillee - @Izinin - how would you do this then?
I think this works because the TApaTaskList only returns running applications - so if you find the app you're after in the list and the state is running then it is running. You'd have a problem if you cached the value though.
In any case, IsExeRunning should work for apps as well as other exes.hamishwillee 06:56, 3 January 2012 (EET)