1

I have a windows form application which does 1 thing: launch Edge, the kill the process:

private void Form1_Load(object sender, EventArgs e)
{
    try
    {
        Process edgeProc = new Process();
        edgeProc = Process.Start("microsoft-edge:.exe");
        edgeProc.Kill();
    }
    catch (Exception ex)
    {
        MessageBox.Show(ex.Message + Environment.NewLine + Environment.NewLine + ex.StackTrace);
    }
}

I don't have a machine with Win10 + Edge to debug this code on, but I indirectly have access to a Windows 10 VM. I build my application and run the exe on that VM, Edge launches but then an exception is thrown:

Object reference not set to an instance of an object.

at EdgeLauncher.Form1.Form1_Load(Object sender, EventArgs e)

I understand what a NullReferenceException is and am plenty familiar with this question.

MSDN says:

A new Process that is associated with the process resource, or null if no process resource is started.

Edge is being launched, so edgeProcess shouldn't be null. So why am I getting this error?

one noa
  • 345
  • 1
  • 3
  • 10
sab669
  • 3,984
  • 8
  • 38
  • 75
  • I'm not familiar with launching UWP apps from the command line. But I suspect `microsoft-edge:.exe` is incorrect due to the colon. And does even have a .exe if it's a UWP? – mason Apr 20 '17 at 14:41
  • 3
    As an aside, note that you are pointlessly creating a default instance of `Process` and then immediately losing the reference to it when you replace it with the result of the call to `Process.Start()`. – Matthew Watson Apr 20 '17 at 14:43
  • @MatthewWatson Thanks; wasn't paying attention. I initially wrote `Process edgeProcess = new Process("microsoft-edge.exe")` or whatever but forgot there is no such overload for the constructor. Removed the string and just left it. This is simply for my own personal testing while looking into a greater is, nothing crucial right now – sab669 Apr 20 '17 at 14:46
  • `Process.Start()` *throws an exception* if it can't start a process or use an existing process. It returns null if the call was successful, but an existing process was reused. – Matthew Watson Apr 20 '17 at 14:46

2 Answers2

2

You're using the shell to execute that command. There is no guarantee a process associated with that. Just because a new window appears doesn't mean a new process has been started :)

If you always want to start a new process, don't use UseShellExecute - needless to say, this has complications of its own.

Luaan
  • 62,244
  • 7
  • 97
  • 116
-2

The new Process() is useless in this usecase. You can do:

private void Form1_Load(object sender, EventArgs e)
{
    try
    {
        Process edgeProc = Process.Start("microsoft-edge:.exe");
        edgeProc?.Kill(); // the "?." will prevent the NullReferenceException 
    }
    catch (Exception ex)
    {
        MessageBox.Show(ex.Message + Environment.NewLine +    Environment.NewLine + ex.StackTrace);
    }
}

The Process.Start(...) returns null, if no process was started.

TcKs
  • 25,849
  • 11
  • 66
  • 104
  • This doesn't compile, at least not in VS2013 which is what I'm stuck with at work – sab669 Apr 20 '17 at 14:48
  • The "?." operator is available in C# 6. In older version, you need check the process for null: if (edgeProc != null) { edgeProc.Kill(); } – TcKs Apr 20 '17 at 19:37
  • I know I could've simply added a check for `null` but as I said in my question, I understand what an NRE is and how to fix them generally -- I just didn't understand *why* the value was `null` in this specific case. – sab669 Apr 20 '17 at 20:21
  • As I said in answer - the "null" is returned if no process was started. – TcKs Apr 20 '17 at 20:41