Computer Hope

Software => Computer programming => Topic started by: gixmoguy on January 07, 2017, 01:25:48 PM

Title: Not sure why my code is not working
Post by: gixmoguy on January 07, 2017, 01:25:48 PM
So im trying to create a program pretty much that will open other programs upon exit or with a time delay. I get no build errors but everytime i run my program i get the error Cannot start process because a file name has not been provided. but launches the first program anyways just not the other one upon exit. anyone see any issues with the code i provided. Thanks alot appreciate any help :)

Code: [Select]
using System;
using System.Diagnostics;
using System.Diagnostics.Contracts;

namespace ProcessExitSample
{
class testsandboxprogram
{
static void Main(string[] args)
{
Contract.Requires(args != null);
try
{
//why in the literal *censored* am i getting an error for no name!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!WHEN I DEFINED THE PATH
var firstProc = new Process();
Process.Start(@"PATH TO EXE I WANT TO LAUNCH");
firstProc.EnableRaisingEvents = true;

firstProc.Start();

firstProc.WaitForExit();

//so upon exit should run the second program here
Console.WriteLine("First process exited: " + firstProc.ExitCode);

var secondProc = new Process();
Process.Start(@"PATH TO PROGRAM I WANT TO LAUNCH");
secondProc.Start();

}
catch (Exception ex)
{
Console.WriteLine("Something went wrong sorry :(: " + ex.Message);
return;
}
}
}
}
Title: Re: Not sure why my code is not working
Post by: BC_Programmer on January 07, 2017, 02:51:49 PM
Process.Start() isn't going to give the firstProc instance any information about the file you specified as a parameter, as it creates a new Process and starts it and returns that Process as the return value.

You probably want something like this:

Code: [Select]
    ProcessStartInfo FirstProcInfo = new ProcessStartInfo(@"C:\Path\To\executable.exe");
    var FirstProc = Process.Start(FirstProcInfo);
    FirstProc.WaitForExit();
    ProcessStartInfo SecondProcInfo = new ProcessStartInfo(@"C:\Path\To\second\executable.exe");
    var SecondProc = Process.Start(SecondProcInfo);





Title: Re: Not sure why my code is not working
Post by: gixmoguy on January 07, 2017, 03:22:39 PM
Process.Start() isn't going to give the firstProc instance any information about the file you specified as a parameter, as it creates a new Process and starts it and returns that Process as the return value.

You probably want something like this:

Code: [Select]
    ProcessStartInfo FirstProcInfo = new ProcessStartInfo(@"C:\Path\To\executable.exe");
    var FirstProc = Process.Start(FirstProcInfo);
    FirstProc.WaitForExit();
    ProcessStartInfo SecondProcInfo = new ProcessStartInfo(@"C:\Path\To\second\executable.exe");
    var SecondProc = Process.Start(SecondProcInfo);

thanks alot for the help im now using

Code: [Select]
ProcessStartInfo FirstProcInfo = new ProcessStartInfo(@"C:\Program Files (x86)\Steam\steamapps\common\BattleBlock Theater\BattleBlockTheater.exe");

var FirstProc = Process.Start(FirstProcInfo);

firstProc.WaitForExit();


ProcessStartInfo SecondProcInfo = new ProcessStartInfo(@"C:\Program Files (x86)\Steam\steamapps\common\BattleBlock Theater\BattleBlockTheater.exe");


var SecondProc = Process.Start(SecondProcInfo);

}

catch (Exception ex)
{
Console.WriteLine("Something went wrong :) " + ex.Message);
return;
}
}
}
}



Not sure if it matters or not but im trying to open the same exe after it exits from the first one so opens up exe if you exit that exe it opens the same one up again. Not sure if that matters but im getting the method or operation is not implemented the first exe opens up correctly. the second one doesnt open up apon closing it tho
Title: Re: Not sure why my code is not working
Post by: BC_Programmer on January 07, 2017, 03:40:11 PM
That code wouldn't even compile. You defined the v ariable as FirstProc but then used it as firstProc.

anyway, Steam changes everything. When you launch a steam-managed game by itself it will launch steam telling it to launch itself and exit, so WaitForExit() will return immediately.

I don't get "method or Operation is Not Implemented".
Title: Re: Not sure why my code is not working
Post by: gixmoguy on January 07, 2017, 03:44:08 PM
That code wouldn't even compile. You defined the v ariable as FirstProc but then used it as firstProc.

anyway, Steam changes everything. When you launch a steam-managed game by itself it will launch steam telling it to launch itself and exit, so WaitForExit() will return immediately.

I don't get "method or Operation is Not Implemented".




changed up my code a bit now compiles without any errors

Code: [Select]
using System;
using System.Diagnostics;
using System.Diagnostics.Contracts;
using System.Threading.Tasks;

namespace ProcessExitSample
{
class testsandboxprogram
{
static void Main(string[] args)
{

try
{
//why in the literal *censored*
ProcessStartInfo FirstProcInfo = new ProcessStartInfo(@"C:\Program Files (x86)\Steam\steamapps\common\BattleBlock Theater\BattleBlockTheater.exe");
var FirstProc = Process.Start(FirstProcInfo);




Console.WriteLine("First process Started: ");

System.Threading.Thread.Sleep(4000);
Task.Delay(2000);

                                // should run the second program here

ProcessStartInfo SecondProcInfo = new ProcessStartInfo(@"C:\Program Files (x86)\Steam\steamapps\common\BattleBlock Theater\BattleBlockTheater.exe");
var SecondProc = Process.Start(SecondProcInfo);
}
catch (Exception ex)
{
Console.WriteLine("Something went wrong :) " + ex.Message);
return;

}
}
}
}


the only thing now is session 1 terminates once session 2 launches decided ill just work with launching two at the same time.