question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Application.Restart throws InvalidOperationException

See original GitHub issue
  • .NET Core Version: 3.1.1

  • Have you experienced this same bug with .NET Framework?: No - this has always worked fine on .NET Framework!

Problem description: Calling Application.Restart in a .NET Core WinForms application does not work as it does on .NET Framework. An exception is thrown:

System.InvalidOperationException: Process was not started by this object, so requested information cannot be determined.
   at System.Diagnostics.Process.get_StartInfo()
   at System.Windows.Forms.Application.Restart()

I also tried launching the exe without the debugger, but that doesn’t make a difference.

Additionally, there is no note or breaking change detailing any .NET Core changes.

Expected behavior: Application.Restart should work the same as it does on .NET Framework.

Minimal repro: Create a new .NET Core WinForms project in Visual Studio, put Application.Restart anywhere (even under InitializeComponent in Form1), run it, and observe the exception.

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Reactions:4
  • Comments:12 (12 by maintainers)

github_iconTop GitHub Comments

2reactions
EatonZcommented, Feb 9, 2020

@RussKie I couldn’t get it working when I tried before, but I tried again and this time I realized why. Turns out, there is a second bug in there, but that one is already being fixed, by you!😁

I modified the method a bit and it works fine now.

private void Restart()
{
    string[] arguments = Environment.GetCommandLineArgs();
    Debug.Assert(arguments != null && arguments.Length > 0);
    StringBuilder sb = new StringBuilder((arguments.Length - 1) * 16);
    for (int argumentIndex = 1; argumentIndex < arguments.Length - 1; argumentIndex++)
    {
        sb.Append('"');
        sb.Append(arguments[argumentIndex]);
        sb.Append("\" ");
    }
    if (arguments.Length > 1)
    {
        sb.Append('"');
        sb.Append(arguments[arguments.Length - 1]);
        sb.Append('"');
    }
    ProcessStartInfo currentStartInfo = new ProcessStartInfo();
    currentStartInfo.FileName = Path.ChangeExtension(Application.ExecutablePath, "exe");
    if (sb.Length > 0)
    {
        currentStartInfo.Arguments = sb.ToString();
    }
    Application.Exit();
    Process.Start(currentStartInfo);
}
1reaction
kirsan31commented, Jul 17, 2020

OMG, I just realized that we are using Application.Restart too 😱 Another one obstruction on migrating process 😡 @EatonZ thank you very match for workaround!!!

Read more comments on GitHub >

github_iconTop Results From Across the Web

c# - InvalidOperationException on Application.Exit
Do not call Application. · This is because you are trying to close the form in the closing event of it. · I...
Read more >
FIX: You receive a System.InvalidOperationException ...
Fixes an issue in which an exception and an error message occur when you start a managed application that runs Extensible Object Markup...
Read more >
Thread: [RESOLVED] Problems with Application.Restart()
I decided to add Application.Restart method into my program to enable restarting and applying new settings. After that I got the following ...
Read more >
InvalidOperationException thrown after server restart
An unhandled exception might be thrown when the server restarts after ... The issue occurs only for the first request to the application...
Read more >
Diagnosing an ASP.NET Core hard crash
The first of these causes an InvalidOperationException to be thrown. The second and third, between them, cause the app to crash. The debugger ......
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found