0

I have System.Threading.Timer callback DoApplicationsToWorkSupervising. So it works pretty well on ThreadPool thread. What I actually need is executing application not only in separate domain but rather in STA thread to run WPF application. Sadly, uncommented variant doesn't allow me to Unload my child appDomain, corresponding thread just blocked at this line (and I have not aborted thread with not unloaded domain).

I tried MTA thread with executing console application, tried set background to false, tried to play with context on thread... same result

Could somebody explain me what I do wrong or suggest proper solution?

public void DoApplicationsToWorkSupervising(object obj)
{
    Application applicationToWork = (Application)obj;
    //var staThread = new Thread(() =>
    //{
        var appDomainSetup = new AppDomainSetup
        {
            ApplicationName = applicationToWork.FileName,
            ApplicationBase = applicationToWork.WorkingDirectory,
            ConfigurationFile = applicationToWork.FileName + ".config"
        };
        var appDomain = AppDomain.CreateDomain(
            applicationToWork.ProcessName,
            null,
            appDomainSetup);
        try 
        {
            int exitCode = appDomain.ExecuteAssembly(
                Path.Combine(applicationToWork.WorkingDirectory,    
                    applicationToWork.FileName),
                new[] {applicationToWork.Arguments});
            logger.LogInfoLow(applicationToWork.FileName + 
                " exited with code " + exitCode);
        }
        catch (Exception ex)
        {
            logger.LogErrorLow("Unhandled exception in " +      
                applicationToWork.FileName);
            logger.LogErrorMedium("Unhandled exception in " + 
                applicationToWork.FileName, ex);
        }
        finally
        {
            AppDomain.Unload(appDomain); //My problem 
        }
    //});
    //staThread.Priority = ThreadPriority.AboveNormal;
    //staThread.SetApartmentState(ApartmentState.STA);
    //staThread.Name = applicationToWork.ProcessName;
    //staThread.IsBackground = true;
    //staThread.Start();
}
alloha
  • 133
  • 1
  • 13
  • 2
    It's probably in a UI message loop so that the thread cannot be aborted. Thread aborts are not reliable. Why do you need this? A better approach would be to start a new process. – usr Feb 20 '15 at 22:26
  • I work on conversion legacy process oriented code to domain oriented. This is a business requirement and we hope to speed up loading of our system on low performance computers. Host is a console application and has no UI thread as well as message loop – alloha Feb 20 '15 at 22:41
  • 1
    Can't you cooperatively shut down the child domain? Signal it to shut itself down. – usr Feb 20 '15 at 22:54
  • Same behavior in this case – alloha Feb 21 '15 at 00:31
  • I doubt that you get the same behavior if you ask the app to shut down. That would mean that the app has a bug. – usr Feb 21 '15 at 08:59
  • 1
    Take a look in the debugger at the threads. Give us some feedback about the remaining threads after `AppDomain.Unload` fires off. Worst case scenario, you have to implement your own `AppDomainManager`, `HostExecutionContextManager` and `HostExecutionContext` to track the thread heirarchy and domain ownership and terminate the domain's tree of threads. It's important to note that we want to look at the managed threads (they're technically just 'fiber' contexts), not the native threads - but the debugger might only give you native threads. – TylerY86 Feb 22 '15 at 21:52
  • I still haven't seen an explanation as to why `Process.Start()` would not be a solution. If the code you are calling does not exit, you will always have problems regardless of your solution -- you'll need to forcefully terminate it if you can't ask it to close (and doing that with a process is more reliable than doing it with an AppDomain). – Jeroen Mostert Feb 22 '15 at 23:33

1 Answers1

0

After all executions my staThread wasn't alive, but had Stopped and WaitSleepJoin flags. I still don't understand the details, but this post was helpful for me https://stackoverflow.com/a/5949823/2987062

All my application use COM calls and my host application main thread was marked as STAThread. After changing main thread in host to MTAThread the problem disappeared.

I will appreciate for further explanation of this situation

Community
  • 1
  • 1
alloha
  • 133
  • 1
  • 13