Ryan van den Bogaard
Ryan van den Bogaard

Reputation: 31

Launching C# program from another C# program

Due to me having knowledge of launching apps I am aware that you have multiple ways of launching an application in C# .NET, but I'm running into a issue that occurs when attempting to launch a SDL2 application.

I have attempted the following using the Process class to:

What I can observe is that the application tries to open. It takes forever to launch into the Window mode (starting the 3D environment). After a timeout it will either, render a couple of frames of a blank window before closing or close immediately after opening the window.

So my question is, does anyone have succesfully made a launcher application for a SDL app written in C# .NET? Or knows a way to debug this behaviour? Because unfortunately, the app does not send out a error message and since SDL safely closes the application I can't observe a crash either.

Edit #1

I'm not doing anything fancy with parameters as there shouldn't be any. I already have another one functioning that launches a normal C# application as my launcher requires to open 2 programs. 1 SLD application, 1 COM:VBA controlling application. Given:

string audioSpectrumProgram = "AudioSpectrum.exe";
string audioSpectrumBatchProgram = "AudioSpectrum.bat";

private void BtnLaunchPPTApp_OnClick()
{
    //Powerpoint controlling application
    pVBAApp = Process.Start(presenterProgram, $"\"{this.path}\" {this.audioFormatParams[0]} {((this.ckboxGenerate.Checked) ? "--create" : "")} lang={this.languageCodesParams[this.cboxLanguage.SelectedIndex]}");
}

Method 1:

private void BtnLaunchSDLApp_OnClick()
{
    pVizualizer = Process.Start(audioSpectrumProgram); //file launched from local path (is correct)
}

Method 2:

pVizualizer = Process.Start(audioSpectrumBatchProgram); //file launched from local path (is correct)

Method 3:

ProcessStartInfo info = new ProcessStartInfo("cmd.exe");
FileInfo spectrumFileInfo = new FileInfo(audioSpectrumProgram);
if (spectrumFileInfo.Exists)
   info.Arguments = $"/c \"{spectrumFileInfo.FullName}\"";
pVizualizer = Process.Start(info);

Method 4: based on senario of method 3. You don't have to parse arguments using ProcessStartInfo.

pVizualizer = Process.Start($"cmd.exe /K call \"{spectrumFileInfo.FullName}\"") //to observe what happens to the application

Edit #2

Not affected by changing the UseShellExecute to true or false

private void btnOpenVisualizer_Click(object sender, EventArgs e)
    {
        FileInfo spectrumFileInfo = new FileInfo(audioSpectrumProgram);
        ProcessStartInfo info = new ProcessStartInfo(spectrumFileInfo.FullName);
        info.UseShellExecute = true;
        pVizualizer = new Process();
        pVizualizer.StartInfo = info;
        pVizualizer.EnableRaisingEvents = true;
        pVizualizer.Exited += new EventHandler(myProcess_Exited);
        pVizualizer.Start();
    }

    private void myProcess_Exited(object sender, System.EventArgs e)
    {
        Console.WriteLine(
            $"Exit time    : {pVizualizer.ExitTime}\n" +
            $"Exit code    : {pVizualizer.ExitCode}\n"
            );
    }

Upvotes: 2

Views: 617

Answers (2)

Thomas Weller
Thomas Weller

Reputation: 59303

A general way of analyzing startup issues is to use SysInternals Process Monitor.

Record the application that is not starting up properly. Use a filter for your application. Then go through all items which don't have SUCCESS in the result column. Typically you want to do that bottom-up, since the last error is the one stopping your application from loading.

Like this you'll find common startup issues like:

  • missing DLLs or other dependencies
  • old DLLs or DLLs loaded from wrong location (e.g. registered COM components)
  • wrong working directory, e.g. access to non-existent config files

Upvotes: 2

Ryan van den Bogaard
Ryan van den Bogaard

Reputation: 31

Ok For Future reference: Pathing to the files can be correct and everything might be in order but if you are using DLLs for imports. Change the process's working directory.

The project will run, libs can "sometimes" be found but can cause a weird unknown bug like this one. So the most optimal way of running another C# instance with SDL or any other kind of library:

    private void RunSDLProgram()
    {
        FileInfo spectrumFileInfo = new FileInfo("pathToFile.exe");
        ProcessStartInfo info = new ProcessStartInfo(spectrumFileInfo.FullName);
        info.RedirectStandardOutput = true;
        info.RedirectStandardError = true;
        info.UseShellExecute = false;
        info.WorkingDirectory = spectrumFileInfo.DirectoryName;
        pVizualizer = new Process();
        pVizualizer.StartInfo = info;
        pVizualizer.EnableRaisingEvents = true;
        pVizualizer.Exited += new EventHandler(myProcess_Exited);
        pVizualizer.Start();
    }

    private void myProcess_Exited(object sender, System.EventArgs e)
    {
        Console.WriteLine(
            $"Exit time    : {pVizualizer.ExitTime}\n" +
            $"Exit code    : {pVizualizer.ExitCode}\n" +
            $"output    : {pVizualizer.StandardOutput}\n" +
            $"err    : {pVizualizer.StandardError}\n" 
            );
    }

Running a batch file will look at it's own directory and makes all references local, but it won't alter the working directory. (already had my suspicions about changing the work directory but I didn't see a way to call 2 opperations in process.start("cmd.exe");)

Upvotes: 1

Related Questions