Created
November 3, 2015 13:41
-
-
Save malteb247/8e05e0b28c2eb05818d7 to your computer and use it in GitHub Desktop.
Execute in seperate app domain
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public sealed class Isolated<T> : IDisposable where T : MarshalByRefObject | |
{ | |
private AppDomain _domain; | |
private T _value; | |
public Isolated() | |
{ | |
_domain = AppDomain.CreateDomain("Isolated:" + Guid.NewGuid(), | |
null, AppDomain.CurrentDomain.SetupInformation); | |
Type type = typeof(T); | |
_value = (T)_domain.CreateInstanceAndUnwrap(type.Assembly.FullName, type.FullName); | |
} | |
public T Value | |
{ | |
get | |
{ | |
return _value; | |
} | |
} | |
public void Dispose() | |
{ | |
if (_domain != null) | |
{ | |
AppDomain.Unload(_domain); | |
_domain = null; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Sometimes you're forced to use bad third party libraries or buggy, memoryleaking DOT.NET libraries. Executing them in an isolated app domain makes you able to kill to whole domain and free the memory after work.