简体   繁体   中英

How do I prevent two instances of my application from loading after updating using ClickOnce?

I have an application that I am deploying using ClickOnce. I am using the default InstallUpdateUpdateSyncWithInfo() method provided here . I made two changes though; I made the method public and static as I am calling it from a static class. I know bad practices. This is some lazy code just to try out ClickOnce.

Everytime the application updates it loads two instances, the old one and the new one.

Other than that though I am calling the method in my app.xaml.cs like this:

public partial class App : Application
{
    private void Application_Startup(object sender, StartupEventArgs e)
    {
        MainWindow window = new MainWindow();
        CheckForUpdates.InstallUpdateSyncWithInfo();
        window.Show();
    }
}

I thought if I call Window.Show() after checking for an Update it would call the Application.Restart() method in InstallUpdateUpdateSyncWithInfo() before the old version could load, but this is not the case.

Does anyone know how I can prevent two instances of my application from loading after the application is updated?

There was another post on Stack Overflow which from the title, I thought would directly address this question, but I did not see how the poster modified his code to prevent two instances from loading.

There's no need to write the auto-update code yourself. First, I would remove your update code.

Next right-click on your C# project and select Properties . Then go to Publish and click Updates... . Tick the checkbox so your application checks for updates and ClickOnce will handle the rest.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM