Sen Jacob
Sen Jacob

Reputation: 3442

Apply [ThreadStatic] attribute to a method in external assembly

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);
}

Upvotes: 0

Views: 619

Answers (2)

bmm6o
bmm6o

Reputation: 6505

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.

Upvotes: 1

Eric J.
Eric J.

Reputation: 150108

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.

Upvotes: 2

Related Questions