Thursday, January 12, 2012

Launch of User Apps by Display App


Launch of User Apps by Display App

I have been thinking about having the first launched application launch the others in the configuration.  The Visual C# interface to Windows functions seems to allow the launch of both local and remote applications – that is, applications on the same PC as the already running application/process and applications that are on another PC.  The older Win32Ada interface only seems to have a function to launch another process on the same PC as the invoking application/process.

I have just modified the Visual C# display application to start the user applications of the local PC.  It first checks if the application is already running.  Then, if not, it starts the application process.  This turned out to be very easy.

Nothing has been done as yet about checking whether the application is running on a remote computer or to start an application on a remote computer as can be specified in the configuration.  The change was only made to the display application since the user applications would need access to recent Windows interface functions.  Therefore, to use this feature the operator needs to start by launching the display application.

There is the possibility of race conditions.  That is, the currently running processes are obtained and then a check is made for each user application in the configuration to determine if it is in the list of running processes.  If not, it is launched.  Therefore, there is the possibility that the user application could be independently launched in the interval between obtaining the list of running processes and programmatically launching it due to not being in the list.  This is highly unlikely to happen since a single operator would be running the set of applications (that is, Windows processes).

This change required three modifications to the display application.  First, an additional configuration field for user applications was extracted – the application executable path and name.  (The PC of the application will also need to be extracted and stored when also do remote PC applications.) 

Second, this application path and name was stored in the internal table (C# struct) of the instance of the Program class as executable as well as adding additional entries for whether the application is running (opened) and the process name (processName) portion of the executable path.

Third, the Remote class Initialize method was changed to invoke a new runRemoteApplications method.  This new method was implemented as

private void runRemoteApplications()
{ // Launch the other applications of the configuration

    // Get the list of process identifiers.
    System.Diagnostics.Process[] aProccesses =
      System.Diagnostics.Process.GetProcesses();

    string processName = "";
    for (int r = 0; r < Program.userAppConfiguration.count; r++)
    {
        if (!Program.userAppConfiguration.opened[r])
         { // check if the application is running
            for (int i = 0; i < aProccesses.Length - 1; i++)
            {
                if (aProccesses[i].ProcessName ==
                    Program.userAppConfiguration.processName[r])
                {
                    processName = Program.userAppConfiguration.processName[r];
                    // application is running via manual start
                    Program.userAppConfiguration.opened[r] = true;
                    break; // exit inner loop
                }
            } // end for
        }

        if (!Program.userAppConfiguration.opened[r])
        { // Start the application
            try
            {
                Program.userAppConfiguration.userProcess[r] =
                  System.Diagnostics.Process.Start(
                    Program.userAppConfiguration.executable[r]);
                Program.userAppConfiguration.opened[r] = true;
            }
            catch { }
        }

    } // end remote user app of configuration search

} // end method runRemoteApplications

This worked on the first attempt, whether or not a user app had already been launched.  So I give myself a pat on the back.

No comments: