By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
432,109 Members | 969 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 432,109 IT Pros & Developers. It's quick & easy.

Threads loading their own DLLs?

P: 11
Hi All

I'm writing an application thats using threads and a third party dll.

Each thread uses this third party dll to log into another application supplying a different username and password, the problem is that the dll stores the login details in a static class and so every thread is accessing the same value. Example:

[Thread 1] sets the Username as "Username 1";
[Thread 1] gets the Username which equals "Username 1";
[Thread 2] sets the Username as "Username 2";
[Thread 1] gets the Username which now equals "Username 2";

Is there anyway that each thread can load their own seperate version of the library?

Cheers
Feb 17 '09 #1
Share this Question
Share on Google+
8 Replies


Expert 100+
P: 750
PRR
Could you explain more ? where are you getting your username from? Instead of using threads you could also look into using Appdomain?...
Feb 17 '09 #2

P: 11
Hi DeepBlue

The application I'm writing is a TcpServer for my company and the username / password is supplied by the client so is different every time. I've never used AppDomain before, could this be used within a TcpServer app?

Cheers
Feb 17 '09 #3

Expert 100+
P: 750
PRR
if username password is different ..how come different threads variables are same? Can you explain more? provide some sample code?
Feb 17 '09 #4

P: 11
Heres the basic code.

Expand|Select|Wrap|Line Numbers
  1.  
  2.         public Server()
  3.         {
  4.             try
  5.             {
  6.                 //FrogSIMSLibrary.Login.Get().LogInUser();
  7.                 Console.WriteLine("Logged into SIMS");
  8.             }
  9.             catch (Exception ex)
  10.             {
  11.                 Console.WriteLine(ex.Message);
  12.                 Console.ReadLine();
  13.                 System.Environment.Exit(0);
  14.             }
  15.  
  16.             //# Create New TCP Listener to listen for client connections from Frog
  17.             this.tcpListener = new TcpListener(IPAddress.Any, 9999);
  18.  
  19.             //# Create New Thread to listen for Clients
  20.             this.listenerThread = new Thread(new ThreadStart(ListenForClients));
  21.  
  22.             this.listenerThread.Start();
  23.         }
  24.  
  25.         private void ListenForClients()
  26.         {
  27.             //# Start Listener
  28.             this.tcpListener.Start();
  29.  
  30.             //# Infinite Loop to constantly listen for Clients connecting
  31.             while (true)
  32.             {
  33.                 //# Blocks until a Client connects
  34.                 TcpClient client = this.tcpListener.AcceptTcpClient();
  35.  
  36.                 ThreadPool.QueueUserWorkItem(HandleClientComm, client);
  37.             }
  38.         }
  39.  
  40.         private void Test(string hashCode)
  41.         {
  42.             Console.WriteLine(hashCode + " :: " + testCount.ToString());
  43.         }
  44.  
  45.         private void HandleClientComm(object client)
  46.         {
  47.             TcpClient tcpClient = (TcpClient)client;
  48.             NetworkStream networkStream = tcpClient.GetStream();
  49.  
  50.         SendMessage("USERNAME");        
  51.         ThirdPartyDLL.LoginDetails.Username = RecieveMessage(networkStream);
  52.  
  53.         SendMessage("PASSWORD");
  54.         ThirdPartyDLL.LoginDetails.Password = RecieveMessage(networkStream);
  55.  
  56.         ThirdPartyDLL.LoginDetails.Login();
  57.  
  58.         ...          
  59.         }
The Send and Recieve methods are basic methods for such functions.

As you can see each client connects which starts a new thread. The Client must then send the username and password which is assigned to the logindetails in the thridpartydll and then used to login the user. As these values are stored in a static class every thread accesses the same values.
Feb 17 '09 #5

vekipeki
Expert 100+
P: 229
I believe you are using a class which exposes a static property (or a Singleton). So it holds a reference to the same object (string in your case), regardless of the thread from which it is being accessed.

It doesn't matter if it is in the same assembly, or a different one, if it is static/singleton.

EDIT: I just saw your post, so I guess that's it - a static property.
Feb 17 '09 #6

P: 11
Thats what I feared. So there is no way at all to have 2 dlls' loaded running parallel together?
Feb 17 '09 #7

vekipeki
Expert 100+
P: 229
What does your "ThirdPartyDll" documentation say? How does the Login() method work? If it just takes the previously entered values and uses them for login, then it should be possible to enter username/password, call the Login() method, and then pass a different username/password pair to the same static instance for the next Login().

Have you tried doing it inside a Lock/Monitor?
Feb 17 '09 #8

P: 11
Unforutnately the documentation is not very usefull and only documents how to login by passing vars. Also its not just the username and password thats stored this way, after a successful login there's a few variables that are populated in a static class. I've tried using lock and while this works some of the client connections can take a while so its very slow, I guess this is my only option.

Thanks for help though.
Feb 17 '09 #9

Post your reply

Sign in to post your reply or Sign up for a free account.