Forum Discussion

ChrisPro's avatar
ChrisPro
Contributor
11 years ago

Sys.Process("****").Exists log an error and not return a value

I would to test if a process is present :


  if (Sys.Process("****").Exists)


  {


    Log.Message("Not Exist");


  }


  else


  {


    Log.Message("Exist");


  }



But the command "Sys.Process("****").Exists" raise an error when processi in not present.



What is the solution?



  • sbkeenan's avatar
    sbkeenan
    Frequent Contributor
    Hi Chris



    Tanya is correct - you need to use the 'wait' methods with Exists.  I've attached a simple example below, which determines whether the Windows Calculator process is running.  It simply logs a message to the log file as to whether it is or not and returns a Boolean true or false accordingly...







    function isCalculatorOpen()

    {

      
    var isOpen;



      
    if (Sys.waitProcess("calc", 1000).Exists)

      
    {

         
    Log.Message("Process Exists.");

         
    isOpen = true;

      
    }

      
    else

      
    {

         
    Log.Message("Process Does Not Exist.");

         
    isOpen = false;

      
    }



      
    return isOpen;

    }





    Hope this is helpful.



    Addendum: Perhaps an explanation would also be helpful...



    When you try to access the 'Exists' property on a process or a control that does actually exist, the system is able to access that property, which would obviously be true.  However, as in your case, when the process itself doesn't exist, it's properties don't actually exist either, so trying to reference the 'Exists' property will fail.



    The 'Wait*' methods catch this error and what actually happens is that when a process or control doesn't exist, the 'Wait*' method generates a dummy or stub object with only one property, the 'Exists' property, which will be set to false, thus enabling your code to carry on without failing.



    Regards

    Stephen.



  • Hi Chris,

     


    You need to use the Exists property along with the Wait* methods. In your case, it should be the WaitProcess method instead of Process. This will help you obtain the object properly and avoid the error.


     

  • Et for a field of object :

      if (Aliases.****.Exists)



    There is an error when the object is not present?