Apply [ThreadStatic] attribute to a method in external assembly
- by Sen Jacob
Can I use an external assembly's static method like [ThreadStatic] method?
Here is my situation.
The assembly class (which I do not have access to its source) has this structure
public class RegistrationManager()
{
private RegistrationManager() {}
public static void RegisterConfiguration(int ID) {}
public static object DoWork() {}
public static void UnregisterConfiguration(int ID) {}
}
Once registered, I cannot call the DoWork() with a different ID without unregistering the previously registered one. Actually I want to call the DoWork() method with different IDs simultaneously with multi-threading.
If the RegisterConfiguration(int ID) method was [ThreadStatic], I could have call it in different threads without problems with calls, right? So, can I apply the [ThreadStatic] attribute to this method or is there any other way I can call the two static methods same time without waiting for other thread to unregister it?
If I check it like the following, it should work.
for(int i=0; i < 10; i++)
{
new Thread(new ThreadStart(() => Checker(i))).Start();
}
public string Checker(int i)
{
public static void RegisterConfiguration(i); // Now i cannot register second time
public static object DoWork(i);
Thread.Sleep(5000); // DoWork() may take a little while to complete before unregistered
public static void UnregisterConfiguration(i);
}