Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. How do I ensure that my thread is ready before my main thread continues ?

How do I ensure that my thread is ready before my main thread continues ?

Scheduled Pinned Locked Moved C#
questionsysadmindata-structures
8 Posts 5 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A Offline
    A Offline
    abiemann
    wrote on last edited by
    #1

    I'm currently experiencing a race condition: my data-receive thread isn't ready by the time it's supposed to receive data

                    //create a thread to receive responses from the server
                    Thread clientThread = new Thread(new ParameterizedThreadStart(ProcessServerResponse));
                    clientThread.Start(RDclient);
    
                    //log activity to client window
                    logText("Connected to Server");
    
                    //initiate password syncronization - ProcessServerResponse() should be up and running by now
                    //TODO eliminate race condition
                    String clientRequest = "SYNC|" + strClientPassword;
                    Byte\[\] sendBytes = Encoding.ASCII.GetBytes(clientRequest);
                    tcpClientStream.Write(sendBytes, 0, sendBytes.Length);
                    tcpClientStream.Flush();
    

    how do I ensure that my ProcessServerResponse() thread is at the .Read() before my main thread sends .Write ?

        private void ProcessServerResponse(object client)
        {
            TcpClient \_RDclient = (TcpClient)client;
            NetworkStream tcpClientStream = \_RDclient.GetStream();
            String strTemp;
    
            //data array to receive data
            byte\[\] message = new byte\[Properties.Settings.Default.iReceiveBufferSize\];
            int iBytesReceivedCount;
    
            while (true)
            {
                iBytesReceivedCount = 0;
    
                try
                {
                    //blocks thread until server receives a message
                    iBytesReceivedCount = tcpClientStream.Read(message, 0, message.Length);
                }
    
    L A 2 Replies Last reply
    0
    • A abiemann

      I'm currently experiencing a race condition: my data-receive thread isn't ready by the time it's supposed to receive data

                      //create a thread to receive responses from the server
                      Thread clientThread = new Thread(new ParameterizedThreadStart(ProcessServerResponse));
                      clientThread.Start(RDclient);
      
                      //log activity to client window
                      logText("Connected to Server");
      
                      //initiate password syncronization - ProcessServerResponse() should be up and running by now
                      //TODO eliminate race condition
                      String clientRequest = "SYNC|" + strClientPassword;
                      Byte\[\] sendBytes = Encoding.ASCII.GetBytes(clientRequest);
                      tcpClientStream.Write(sendBytes, 0, sendBytes.Length);
                      tcpClientStream.Flush();
      

      how do I ensure that my ProcessServerResponse() thread is at the .Read() before my main thread sends .Write ?

          private void ProcessServerResponse(object client)
          {
              TcpClient \_RDclient = (TcpClient)client;
              NetworkStream tcpClientStream = \_RDclient.GetStream();
              String strTemp;
      
              //data array to receive data
              byte\[\] message = new byte\[Properties.Settings.Default.iReceiveBufferSize\];
              int iBytesReceivedCount;
      
              while (true)
              {
                  iBytesReceivedCount = 0;
      
                  try
                  {
                      //blocks thread until server receives a message
                      iBytesReceivedCount = tcpClientStream.Read(message, 0, message.Length);
                  }
      
      L Offline
      L Offline
      Luc Pattyn
      wrote on last edited by
      #2

      Have a look at Thread.Join() :)

      Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles]


      I only read code that is properly indented, and rendered in a non-proportional font; hint: use PRE tags in forum messages


      1 Reply Last reply
      0
      • A abiemann

        I'm currently experiencing a race condition: my data-receive thread isn't ready by the time it's supposed to receive data

                        //create a thread to receive responses from the server
                        Thread clientThread = new Thread(new ParameterizedThreadStart(ProcessServerResponse));
                        clientThread.Start(RDclient);
        
                        //log activity to client window
                        logText("Connected to Server");
        
                        //initiate password syncronization - ProcessServerResponse() should be up and running by now
                        //TODO eliminate race condition
                        String clientRequest = "SYNC|" + strClientPassword;
                        Byte\[\] sendBytes = Encoding.ASCII.GetBytes(clientRequest);
                        tcpClientStream.Write(sendBytes, 0, sendBytes.Length);
                        tcpClientStream.Flush();
        

        how do I ensure that my ProcessServerResponse() thread is at the .Read() before my main thread sends .Write ?

            private void ProcessServerResponse(object client)
            {
                TcpClient \_RDclient = (TcpClient)client;
                NetworkStream tcpClientStream = \_RDclient.GetStream();
                String strTemp;
        
                //data array to receive data
                byte\[\] message = new byte\[Properties.Settings.Default.iReceiveBufferSize\];
                int iBytesReceivedCount;
        
                while (true)
                {
                    iBytesReceivedCount = 0;
        
                    try
                    {
                        //blocks thread until server receives a message
                        iBytesReceivedCount = tcpClientStream.Read(message, 0, message.Length);
                    }
        
        A Offline
        A Offline
        Alan N
        wrote on last edited by
        #3

        Hi, In very general terms the writing thread should wait for a ready signal from the reading thread.

        void Initialise() {
          using (ManualResetEvent mre = new ManualResetEvent(false)) {
            Thread t = new Thread(ThreadProc);
            t.Start(mre);
            // continue preparing the writer
            ...
            // wait until reader thread is ready
            mre.WaitOne();
          }
        }
        
        void ThreadProc(Object mre) {
          ...
          ((ManualResetEvent)mre).Set();
          ...
          ...
        }
        

        The reader thread should signal as soon as it can and the writer should delay waiting until it is ready to write. That way the actual wait period will be minimised. Alan.

        D A 2 Replies Last reply
        0
        • A Alan N

          Hi, In very general terms the writing thread should wait for a ready signal from the reading thread.

          void Initialise() {
            using (ManualResetEvent mre = new ManualResetEvent(false)) {
              Thread t = new Thread(ThreadProc);
              t.Start(mre);
              // continue preparing the writer
              ...
              // wait until reader thread is ready
              mre.WaitOne();
            }
          }
          
          void ThreadProc(Object mre) {
            ...
            ((ManualResetEvent)mre).Set();
            ...
            ...
          }
          

          The reader thread should signal as soon as it can and the writer should delay waiting until it is ready to write. That way the actual wait period will be minimised. Alan.

          D Offline
          D Offline
          David Skelly
          wrote on last edited by
          #4

          What is the advantage of using a ManualResetEvent over an EventWaitHandle? I always use an ewh in these situations. I don't really understand the difference between these two classes and why you would use one rather than the other.

          N A 2 Replies Last reply
          0
          • D David Skelly

            What is the advantage of using a ManualResetEvent over an EventWaitHandle? I always use an ewh in these situations. I don't really understand the difference between these two classes and why you would use one rather than the other.

            N Offline
            N Offline
            Nicholas Butler
            wrote on last edited by
            #5

            ManualResetEvent and AutoResetEvent are both derived from EventWaitHandle. All they do is set the correct EventResetMode in their constructors. Apart from that, they are identical. Nick

            ---------------------------------- Be excellent to each other :)

            1 Reply Last reply
            0
            • D David Skelly

              What is the advantage of using a ManualResetEvent over an EventWaitHandle? I always use an ewh in these situations. I don't really understand the difference between these two classes and why you would use one rather than the other.

              A Offline
              A Offline
              Alan N
              wrote on last edited by
              #6

              Hi David, I doubt that there is any reason to choose one over the other and assume that the Manual and AutoResetEvent derivations are just conveniences that give clarification of intent and self documentation of code. The member documentation shows that none of the EventWaitHandle properties or methods are overridden and only the constructor is new. When moving over from win32 to .NET programming the extent of the libraries was daunting and for me the sight of any familiar name was welcome. I saw ManualResetEvent, thought I know what that does, and stuck with it! Alan.

              D 1 Reply Last reply
              0
              • A Alan N

                Hi David, I doubt that there is any reason to choose one over the other and assume that the Manual and AutoResetEvent derivations are just conveniences that give clarification of intent and self documentation of code. The member documentation shows that none of the EventWaitHandle properties or methods are overridden and only the constructor is new. When moving over from win32 to .NET programming the extent of the libraries was daunting and for me the sight of any familiar name was welcome. I saw ManualResetEvent, thought I know what that does, and stuck with it! Alan.

                D Offline
                D Offline
                David Skelly
                wrote on last edited by
                #7

                Alan N wrote:

                Manual and AutoResetEvent derivations are just conveniences that give clarification of intent and self documentation of code.

                Yes, I can see this. Using ManualResetEvent makes it immediately clear that it relies on something elsewhere in the code manually resetting it (I can guess that from the cunning name they chose for this class). Well, today I have learned something new. Thanks, all.

                1 Reply Last reply
                0
                • A Alan N

                  Hi, In very general terms the writing thread should wait for a ready signal from the reading thread.

                  void Initialise() {
                    using (ManualResetEvent mre = new ManualResetEvent(false)) {
                      Thread t = new Thread(ThreadProc);
                      t.Start(mre);
                      // continue preparing the writer
                      ...
                      // wait until reader thread is ready
                      mre.WaitOne();
                    }
                  }
                  
                  void ThreadProc(Object mre) {
                    ...
                    ((ManualResetEvent)mre).Set();
                    ...
                    ...
                  }
                  

                  The reader thread should signal as soon as it can and the writer should delay waiting until it is ready to write. That way the actual wait period will be minimised. Alan.

                  A Offline
                  A Offline
                  abiemann
                  wrote on last edited by
                  #8

                  that works great =) thanks, Alan N

                  1 Reply Last reply
                  0
                  Reply
                  • Reply as topic
                  Log in to reply
                  • Oldest to Newest
                  • Newest to Oldest
                  • Most Votes


                  • Login

                  • Don't have an account? Register

                  • Login or register to search.
                  • First post
                    Last post
                  0
                  • Categories
                  • Recent
                  • Tags
                  • Popular
                  • World
                  • Users
                  • Groups