Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Application.Restart throws InvalidOperationException #2769

Closed
EatonZ opened this issue Jan 25, 2020 · 12 comments · Fixed by #2845
Closed

Application.Restart throws InvalidOperationException #2769

EatonZ opened this issue Jan 25, 2020 · 12 comments · Fixed by #2845
Labels
🪲 bug Product bug (most likely) 💥 regression-preview Regression from a preview release

Comments

@EatonZ
Copy link
Contributor

EatonZ commented Jan 25, 2020

  • .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.

@RussKie RussKie added 🪲 bug Product bug (most likely) 💥 regression-preview Regression from a preview release tracking-external-issue An issue is caused by an external system and won't be fixed in this repo labels Jan 28, 2020
@RussKie
Copy link
Member

RussKie commented Jan 28, 2020

@danmosemsft @stephentoub @davkean for us this is a regression caused by a change introduced in dotnet/corefx#1167

Could you please provide guidance on how to restart the app?

// Regular app case
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 = Process.GetCurrentProcess().StartInfo;
currentStartInfo.FileName = ExecutablePath;
if (sb.Length > 0)
{
currentStartInfo.Arguments = sb.ToString();
}
ExitInternal();
Process.Start(currentStartInfo);

@davkean
Copy link
Member

davkean commented Jan 28, 2020

Application.Restart and .NET Core 1.0 didn't exist at the time that change was checked in, so its not a regression. Probably need a bug against CoreFx to let you look at the StartInfo of a process if its the "current process".

@RussKie
Copy link
Member

RussKie commented Jan 28, 2020

For us it is a regression because it worked in .NET Framework and it doesn't work in .NET Core.

@RussKie RussKie removed the tracking-external-issue An issue is caused by an external system and won't be fixed in this repo label Jan 28, 2020
@stephentoub
Copy link
Member

stephentoub commented Jan 28, 2020

The only valid piece of Process.GetCurrentProcess().StartInfo was the environment variables, which are also populated when creating a new PSI. Just create a new PSI and populate it as you already are, e.g. replace:

Process.GetCurrentProcess().StartInfo

with

new ProcessStartInfo()

That's simpler and cheaper.

@merriemcgaw merriemcgaw added this to the 5.0 milestone Feb 7, 2020
@merriemcgaw merriemcgaw added the help wanted Good issue for external contributors label Feb 7, 2020
@EatonZ
Copy link
Contributor Author

EatonZ commented Feb 8, 2020

Hi @merriemcgaw, is the fix really only going to make it for 5.0?
If that is the case, does anyone have any temporary workarounds I can use here?

@RussKie
Copy link
Member

RussKie commented Feb 9, 2020

I'm confident once it is fixed, it will be considered for a servicing in 3.1.

As an interim workaround you could copy the implementation of Application.Restart into your app.

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 = Process.GetCurrentProcess().StartInfo;
currentStartInfo.FileName = ExecutablePath;
if (sb.Length > 0)
{
currentStartInfo.Arguments = sb.ToString();
}
ExitInternal();
Process.Start(currentStartInfo);

@EatonZ
Copy link
Contributor Author

EatonZ commented 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);
}

@RussKie
Copy link
Member

RussKie commented Feb 10, 2020

Btw you're welcome to send a PR with the fix 😉

@ghost ghost added the 🚧 work in progress Work that is current in progress label Feb 12, 2020
@EatonZ
Copy link
Contributor Author

EatonZ commented Feb 12, 2020

@RussKie I wasn't sure if the proper fix was that simple... but I guess it is, so I just submitted a PR.😁

@merriemcgaw
Copy link
Member

This particular bug, since it's not blocking you at the moment, doesn't meet the servicing bar for 3.1, but I am very glad you got it in for 5.0. If we find out that this is blocking someone and impacting customers then I believe it would meet the bar and we'd take it. But at this point the workaround is a viable one I'm afraid.

@EatonZ
Copy link
Contributor Author

EatonZ commented Feb 13, 2020

Hi @merriemcgaw, I think it's a long time to wait until November for a 1-line fix. If that is what you want to do, then I suggest documenting this breakage somewhere so people will be able to find the workaround when searching.

@RussKie RussKie modified the milestones: 5.0 Previews 1-4, 5.0 Apr 20, 2020
@ghost ghost removed the 🚧 work in progress Work that is current in progress label May 21, 2020
@RussKie RussKie removed this from the 5.0 milestone May 21, 2020
@RussKie RussKie removed the help wanted Good issue for external contributors label May 21, 2020
@kirsan31
Copy link
Contributor

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

tznind added a commit to HicServices/RDMP that referenced this issue Feb 24, 2021
@ghost ghost locked as resolved and limited conversation to collaborators Feb 2, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
🪲 bug Product bug (most likely) 💥 regression-preview Regression from a preview release
Projects
None yet
Development

Successfully merging a pull request may close this issue.

6 participants