A problem unloading Application Domains
-
Alright, so I have an application that is in constant communication with the database. As happens in the real world, sometimes (devilishly often in this case actually) the line goes down. When this happens, the application needs to be restored to its initial state. A similar thing will need to happen when the user allows the application to time out. Someone suggested on the ALT.NET list that I simply unload the application domain. After poking around online for what this actually means this seems like a great idea. I am having trouble however getting my test program to work and I fear that I must be missing something. My test has two components ApplicationDomainChild - a simple windows form with a label displaying a number and a button that when you press increments the number. I've compiled this to an .exe. ApplicationDomainParent - another windows form with a single button and the following:
public partial class Form1 : Form { private AppDomain _second_domain = null; private string _second_assembly_path = @"ApplicationDomainChild.exe"; private void Form1_Load(object sender, EventArgs e) { _second_domain = AppDomain.CreateDomain("Second Domain"); _second_domain.ExecuteAssembly(_second_assembly_path); } private void button1_Click(object sender, EventArgs e) { try { AppDomain.Unload(_second_domain); _second_domain = AppDomain.CreateDomain("Second Domain"); _second_domain.ExecuteAssembly(_second_assembly_path); } catch (Exception ex) { System.Diagnostics.Debug.WriteLine(ex); } } }
What I expect to happen is for both forms to open at the same time, me to be able to increment the values in ApplicationDomainChild as much as I want by pressing the button and then be able to press the button on ApplicationDomainParent and have that shut down and restart the ApplicationDomainChild. What happens however is that first ONLY ApplicationDomainChild runs. After 'X' out of the window ApplicationDomainParent runs. When I press the restart domain button the child program starts up again and I can interact with it, but if I attempt to click the restart button again I get a ThreadAbortException followed by an AppDomainUnloadException (or something like that). Can someone tell me what I'm doing wrong? -
Alright, so I have an application that is in constant communication with the database. As happens in the real world, sometimes (devilishly often in this case actually) the line goes down. When this happens, the application needs to be restored to its initial state. A similar thing will need to happen when the user allows the application to time out. Someone suggested on the ALT.NET list that I simply unload the application domain. After poking around online for what this actually means this seems like a great idea. I am having trouble however getting my test program to work and I fear that I must be missing something. My test has two components ApplicationDomainChild - a simple windows form with a label displaying a number and a button that when you press increments the number. I've compiled this to an .exe. ApplicationDomainParent - another windows form with a single button and the following:
public partial class Form1 : Form { private AppDomain _second_domain = null; private string _second_assembly_path = @"ApplicationDomainChild.exe"; private void Form1_Load(object sender, EventArgs e) { _second_domain = AppDomain.CreateDomain("Second Domain"); _second_domain.ExecuteAssembly(_second_assembly_path); } private void button1_Click(object sender, EventArgs e) { try { AppDomain.Unload(_second_domain); _second_domain = AppDomain.CreateDomain("Second Domain"); _second_domain.ExecuteAssembly(_second_assembly_path); } catch (Exception ex) { System.Diagnostics.Debug.WriteLine(ex); } } }
What I expect to happen is for both forms to open at the same time, me to be able to increment the values in ApplicationDomainChild as much as I want by pressing the button and then be able to press the button on ApplicationDomainParent and have that shut down and restart the ApplicationDomainChild. What happens however is that first ONLY ApplicationDomainChild runs. After 'X' out of the window ApplicationDomainParent runs. When I press the restart domain button the child program starts up again and I can interact with it, but if I attempt to click the restart button again I get a ThreadAbortException followed by an AppDomainUnloadException (or something like that). Can someone tell me what I'm doing wrong?I know its bad form to give myself a self-bump but please can someone just give me a hint? The problem seems to be that my child application is running on the same thread as the parent, therefore only one of them can process input at a time. Do I need to somehow start the appdomain in a different thread? I thought threads were inside of appdomains.