0

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);
}
Soner Gönül
  • 97,193
  • 102
  • 206
  • 364
Sen Jacob
  • 3,384
  • 3
  • 35
  • 61

2 Answers2

2

I'm not aware that you can add [ThreadStatic] to the external assembly without having the source code to recompile it.

You can however create additional application domains in which to execute the code concurrently. Not nearly as elegant as threading, but still a potential solution.

http://msdn.microsoft.com/en-us/library/ms173139%28v=VS.100%29.aspx

Another option depending on licenses under which you may be bound would be to use a tool like Reflector to reverse engineer the source code of the external assembly and adding the [ThreadStatic] attribute.

Community
  • 1
  • 1
Eric J.
  • 147,927
  • 63
  • 340
  • 553
  • Thank you Erik, I'll try with using different Application domains. I found [`this question`](http://stackoverflow.com/questions/4298913/static-fields-in-appdomain) related to this, but I have confusion in calling static method in diferent AppDomains, let me try. – Sen Jacob Oct 29 '13 at 07:21
  • The accepted answer to the question you reference should solve the problem. You first need to create an wrapper object that you can create an instance of, and have that wrapper object interact with the static method(s). Let me know how that works out. – Eric J. Oct 29 '13 at 16:28
  • I tried to use another wrapper library and called the function, it called the function but since it returns another object which is an instance of a non-serializable class in the external library. So I got a Serialization exception. Is there anyway to create a non serializable object and return it from the wrapper library when we call `appDomain1..CreateInstanceAndUnwrap(...).FactoryMethod()` ? – Sen Jacob Nov 07 '13 at 15:44
1

ThreadStatic applies to fields, not methods, so this idea is doomed from the start. It should be possible to disassemble the dll, modify the source and reassemble, but this is moot since there's no easy way to make another component thread-safe.

You probably want to write a wrapper class like:

class RegistraionWrapper
{
    private static Object _lock = new Object();

    public static void DoWork(int ID)
    {
        lock (_lock)
        {
            RegistrationManager.RegisterConfiguration(ID);
            RegistrationManager.DoWork();
            RegistrationManager.UnregisterConfiguration(ID);
        }
    }
}

You might need to put the Unregister call in a finally block. Of course the lock makes it pointless to spawn multiple threads to do the work, but it's not clear that this library supports that at all. Your best bet is to talk to the author of this library.

bmm6o
  • 6,187
  • 3
  • 28
  • 55