Threads loading their own DLLs?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • frogga
    New Member
    • May 2008
    • 11

    #1

    Threads loading their own DLLs?

    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
  • PRR
    Recognized Expert Contributor
    • Dec 2007
    • 750

    #2
    Could you explain more ? where are you getting your username from? Instead of using threads you could also look into using Appdomain?...

    Comment

    • frogga
      New Member
      • May 2008
      • 11

      #3
      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

      Comment

      • PRR
        Recognized Expert Contributor
        • Dec 2007
        • 750

        #4
        if username password is different ..how come different threads variables are same? Can you explain more? provide some sample code?

        Comment

        • frogga
          New Member
          • May 2008
          • 11

          #5
          Heres the basic code.

          Code:
                  public Server()
                  {
                      try
                      {
                          //FrogSIMSLibrary.Login.Get().LogInUser();
                          Console.WriteLine("Logged into SIMS");
                      }
                      catch (Exception ex)
                      {
                          Console.WriteLine(ex.Message);
                          Console.ReadLine();
                          System.Environment.Exit(0);
                      }
          
                      //# Create New TCP Listener to listen for client connections from Frog
                      this.tcpListener = new TcpListener(IPAddress.Any, 9999);
          
                      //# Create New Thread to listen for Clients
                      this.listenerThread = new Thread(new ThreadStart(ListenForClients));
          
                      this.listenerThread.Start();
                  }
          
                  private void ListenForClients()
                  {
                      //# Start Listener
                      this.tcpListener.Start();
          
                      //# Infinite Loop to constantly listen for Clients connecting
                      while (true)
                      {
                          //# Blocks until a Client connects
                          TcpClient client = this.tcpListener.AcceptTcpClient();
          
                          ThreadPool.QueueUserWorkItem(HandleClientComm, client);
                      }
                  }
          
                  private void Test(string hashCode)
                  {
                      Console.WriteLine(hashCode + " :: " + testCount.ToString());
                  }
          
                  private void HandleClientComm(object client)
                  {
                      TcpClient tcpClient = (TcpClient)client;
                      NetworkStream networkStream = tcpClient.GetStream();
          	    
          	    SendMessage("USERNAME");		
          	    ThirdPartyDLL.LoginDetails.Username = RecieveMessage(networkStream);
          
          	    SendMessage("PASSWORD");
          	    ThirdPartyDLL.LoginDetails.Password = RecieveMessage(networkStream);
          
          	    ThirdPartyDLL.LoginDetails.Login();
          
          	    ...          
                  }
          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.

          Comment

          • vekipeki
            Recognized Expert New Member
            • Nov 2007
            • 229

            #6
            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.

            Comment

            • frogga
              New Member
              • May 2008
              • 11

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

              Comment

              • vekipeki
                Recognized Expert New Member
                • Nov 2007
                • 229

                #8
                What does your "ThirdParty Dll" 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?

                Comment

                • frogga
                  New Member
                  • May 2008
                  • 11

                  #9
                  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.

                  Comment

                  Working...