Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>You can load each thread in different <a href="http://msdn.microsoft.com/en-us/library/system.appdomain.aspx" rel="nofollow">AppDomain</a>, that would give you different static methods.</p> <p>Also, <a href="http://msdn.microsoft.com/en-us/library/system.threadstaticattribute.aspx" rel="nofollow">ThreadStaticAttribute</a> might be helpful for you. Don't sure if it fits you, but give it a look.</p> <p>Upd: More info about using AppDomains. Lets assume, that you have 3-rd party class <code>Memory</code> defined as follows. (And you cannot change it, and it uses inner static variables)</p> <pre><code>// Cannot be changed public class Memory { static int StaticId; public static void OpenProcess(int id) { StaticId = id; } public static int GetOpenedId() { return StaticId; } } </code></pre> <p>You can write a wrapper, deriving from <a href="http://msdn.microsoft.com/en-us/library/system.marshalbyrefobject.aspx" rel="nofollow">MarshalByRefObject</a> (that's important):</p> <pre><code>class MemoryWrap : MarshalByRefObject { public void OpenProcess(int id) { Memory.OpenProcess(id); } public int GetOpenedId() { return Memory.GetOpenedId(); } } </code></pre> <p>So if you create instances of <code>MemoryWrap</code> not by <code>new</code> keyword, but using <code>AppDomain.CreateInstanceAndUnwrap</code> in another domain, each instance would have it's own static contexts. Example:</p> <pre><code>class Program { static void Main(string[] args) { var type = typeof(MemoryWrap); var domain1 = AppDomain.CreateDomain("Domain 1"); var memory1 = (MemoryWrap)domain1.CreateInstanceAndUnwrap(type.Assembly.FullName, type.FullName); var domain2 = AppDomain.CreateDomain("Domain 2"); var memory2 = (MemoryWrap)domain2.CreateInstanceAndUnwrap(type.Assembly.FullName, type.FullName); memory1.OpenProcess(1); memory2.OpenProcess(2); Console.WriteLine(memory1.GetOpenedId()); Console.WriteLine(memory2.GetOpenedId()); Console.ReadLine(); } } </code></pre> <p>It would print:</p> <pre><code>1 2 </code></pre> <p>PS: in that example I didn't do the clean up just for readability (unloading domains with <code>AppDomain.Unload()</code> and other things). Don't forget to do it in you code. + There is some mess with lifetime of objects in another domain, but it is next level of problems)))</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload