Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7084275
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T07:16:53+00:00 2026-05-28T07:16:53+00:00

I’ve been working to get a windows service to automatically restart an app when

  • 0

I’ve been working to get a windows service to automatically restart an app when there is a new version. My code is below, and it actually works. My question is this: Is there a big no-no in here somewhere? I’m new to creating app domains on separate threads, and I’d like to know if there is a more elegant way to accomplish this. I’m also concerned that maybe I have a big blind spot regarding how I’m handling this. Any advice is appreciated.

There are two general pieces: the Windows service that runs, starts my app in a different process, then polls to detect when a new version of that app is available. The second piece is the actual app that gets started, updated, restarted.

First, when the service starts, it calls LoadApp() so that the app starts running.

private void LoadApp()
    {
    // ** appDomainSetup here **

    this._appDomain = AppDomain.CreateDomain("MyUpdatedApp", AppDomain.CurrentDomain.Evidence, appDomainSetup);

    this._tokenSource = new CancellationTokenSource();

    Task.Factory.StartNew(() =>
    {
        try
        {
            this._appDomain.ExecuteAssembly(assembly);
        }
        catch (AppDomainUnloadedException ex)
        {
            Debug.WriteLine(ex.ToString());
        }
    }, _tokenSource.Token);
}

Then, every 10 seconds, the timer calls this method:

private void Process(object stateInfo)
{
    if (!Monitor.TryEnter(_locker)) { return; }

    try
    {
        // Check for new binaries. If new, copy the files and restart the app domain.            
        if (!NewAppVersionReady()) { return; }
        DeleteFiles();
        CopyFiles();
        RestartAppDomain();
    }
    finally
    {
        Monitor.Exit(_locker);
    }
}

RestartAppDomain() looks like this. Note that I get an exception when I unload the app domain on the second line in this method. (I get around that by catching the exception and basically ignoring it.)

private void RestartAppDomain()
{
    this._tokenSource.Cancel();
    AppDomain.Unload(this._appDomain);
    LoadApp();
}

Edit: I’ve since found out that the AppDomain.Unload() line is unnecessary. Without it, the service still updates the app with a new version and then starts the new version of the app. My big concern is calling Cancel() on the token source. I’d like the app to be able to shut down gracefully instead of just forcing it shut.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T07:16:53+00:00Added an answer on May 28, 2026 at 7:16 am

    Through some trial and error, and learning, I seem to have settled on a decent solution. I thought I’d post it here in case it helps anyone else.

    First, I created an interface, in a separate project, so the self-updating app won’t have to reference the app that actually gets run in its own app domain. This interface was used so that the self-updating app can call Start and Stop on the other app.

    public interface IStartStop
    {
        void Start();
        void Stop();
    }
    

    In the app that gets run in a separate domain, I had the class derive from MarshalByRefObject, and implement IStartStop.

    public class PrestoTaskRunnerController : MarshalByRefObject, IStartStop, IDisposable
    

    This allows me to start and stop this app from my self-updating app:

    this._appDomain = AppDomain.CreateDomain(this._appName, AppDomain.CurrentDomain.Evidence, appDomainSetup);
    this._startStopControllerToRun = (IStartStop)this._appDomain.CreateInstanceFromAndUnwrap(assemblyName, this._fullyQualifiedClassName);
    this._startStopControllerToRun.Start();
    

    And stopping it is done this way:

    this._startStopControllerToRun.Stop();
    AppDomain.Unload(this._appDomain);
    

    There are some more details, but this is the general concept, and it seems to be working well.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a jquery bug and I've been looking for hours now, I can't
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I want use html5's new tag to play a wav file (currently only supported
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
We're building an app, our first using Rails 3, and we're having to build
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
Does anyone know how can I replace this 2 symbol below from the string
I am writing an app with both english and french support. The app requests
I am using Paperclip to handle profile photo uploads in my app. They upload

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.