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. Threading Question

Threading Question

Scheduled Pinned Locked Moved C#
csharpquestiontutorial
15 Posts 6 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.
  • N Offline
    N Offline
    NameYou
    wrote on last edited by
    #1

    I am developing a windows form app in C# -.net 1.1 framework. The app creates 4 objects and executes a "Go" method in 4 threads - each thread goes off, grabs some data from a UDP socket, and processes it. What I need is to get the processed data from each of the 4 threads (objects) and combine them into one string. I can not figure out how to get the individual threads to return anything. I tried to create a string and pass it to each object's constructor as a reference, but that went nowhere. Does anyone have any ideas on how I can accomplish this? Dan Senior Developer Mitsubishi Power Systems

    Now where did I put that Char....

    N K S 3 Replies Last reply
    0
    • N NameYou

      I am developing a windows form app in C# -.net 1.1 framework. The app creates 4 objects and executes a "Go" method in 4 threads - each thread goes off, grabs some data from a UDP socket, and processes it. What I need is to get the processed data from each of the 4 threads (objects) and combine them into one string. I can not figure out how to get the individual threads to return anything. I tried to create a string and pass it to each object's constructor as a reference, but that went nowhere. Does anyone have any ideas on how I can accomplish this? Dan Senior Developer Mitsubishi Power Systems

      Now where did I put that Char....

      N Offline
      N Offline
      Nader Elshehabi
      wrote on last edited by
      #2

      As long as your threads are objects inside your class, they can manipulate any field -even private ones- including strings. My suggestion to your situation is to make a string obkect inside your class. then either you: 1- Make a single common method that takes parameters passed by the calling thread to add it to the single string object 2- Access that single string object in the different methods of threads -you don't need to return- the way that suits you -this is if you got different methods for different threads. If they all start with the same method, this won't apply-. Very important thing to note is to apply a lock on the string object to prevent threads from modifying it simultaneously.

      Regards:rose:

      N 1 Reply Last reply
      0
      • N Nader Elshehabi

        As long as your threads are objects inside your class, they can manipulate any field -even private ones- including strings. My suggestion to your situation is to make a string obkect inside your class. then either you: 1- Make a single common method that takes parameters passed by the calling thread to add it to the single string object 2- Access that single string object in the different methods of threads -you don't need to return- the way that suits you -this is if you got different methods for different threads. If they all start with the same method, this won't apply-. Very important thing to note is to apply a lock on the string object to prevent threads from modifying it simultaneously.

        Regards:rose:

        N Offline
        N Offline
        NameYou
        wrote on last edited by
        #3

        Let me give you some psudeo code - because I'm not understanding. Here is what I am doing: public class MainClass { private void GetData() { MyObject mo = new MyObject(); Thread t = new Thread(new ThreadStart(mo.Connect)); t.Start(); MyObject mo2 = new MyObject(); Thread t2 = new Thread(new ThreadStart(mo2.Connect)); t2.Start(); MyObject mo2 = new MyObject(); Thread t3 = new Thread(new ThreadStart(mo3.Connect)); t3.Start(); MyObject mo3 = new MyObject(); Thread t4 = new Thread(new ThreadStart(mo4.Connect)); t4.Start(); //WHAT I WANT TO DO IS BE ABLE TO GRAB THE RESULTS FROM THE THREADS STARTED ABOVE //AND PUSH THEM INTO A FILE. I NEED ALL OF THE DATA FROM ALL FOUR THREADS COMBINED INTO ONE FILE } } Public class MyObject { public MyObject() { } public void Connect() { //Starts all the socket stuff.. } //...OTHER SOCKET RELATED FUNCTIONS GO HERE. } How do I get the data in the Connect function to the calling function in MainClass?

        uh... yeah

        N 1 Reply Last reply
        0
        • N NameYou

          Let me give you some psudeo code - because I'm not understanding. Here is what I am doing: public class MainClass { private void GetData() { MyObject mo = new MyObject(); Thread t = new Thread(new ThreadStart(mo.Connect)); t.Start(); MyObject mo2 = new MyObject(); Thread t2 = new Thread(new ThreadStart(mo2.Connect)); t2.Start(); MyObject mo2 = new MyObject(); Thread t3 = new Thread(new ThreadStart(mo3.Connect)); t3.Start(); MyObject mo3 = new MyObject(); Thread t4 = new Thread(new ThreadStart(mo4.Connect)); t4.Start(); //WHAT I WANT TO DO IS BE ABLE TO GRAB THE RESULTS FROM THE THREADS STARTED ABOVE //AND PUSH THEM INTO A FILE. I NEED ALL OF THE DATA FROM ALL FOUR THREADS COMBINED INTO ONE FILE } } Public class MyObject { public MyObject() { } public void Connect() { //Starts all the socket stuff.. } //...OTHER SOCKET RELATED FUNCTIONS GO HERE. } How do I get the data in the Connect function to the calling function in MainClass?

          uh... yeah

          N Offline
          N Offline
          Nader Elshehabi
          wrote on last edited by
          #4

          NameYou wrote:

          How do I get the data in the Connect function to the calling function in MainClass?

          You don't need to. If there is no good reason not to access the file from that method, you should. To be specific, here is my suggestion of you MyObject.Connect() method:

          Public class MyObject
          {
          public MyObject()
          {
          }

          public void Connect()
          {
              //Starts all the socket stuff..
              object LockObject = new object()
              lock(LockObject)
              {
                  StreamWriter MyStream = File.AppendText("MyFile.txt");
                  MyStream.WriteLine(Input);
                  MyStream.Close();
              }
          }
          //...OTHER SOCKET RELATED FUNCTIONS GO HERE.
          

          }

          Of course you got to do some modifications to the code to suite your needs, but I hope this was close enough.

          Regards:rose:

          N E 2 Replies Last reply
          0
          • N Nader Elshehabi

            NameYou wrote:

            How do I get the data in the Connect function to the calling function in MainClass?

            You don't need to. If there is no good reason not to access the file from that method, you should. To be specific, here is my suggestion of you MyObject.Connect() method:

            Public class MyObject
            {
            public MyObject()
            {
            }

            public void Connect()
            {
                //Starts all the socket stuff..
                object LockObject = new object()
                lock(LockObject)
                {
                    StreamWriter MyStream = File.AppendText("MyFile.txt");
                    MyStream.WriteLine(Input);
                    MyStream.Close();
                }
            }
            //...OTHER SOCKET RELATED FUNCTIONS GO HERE.
            

            }

            Of course you got to do some modifications to the code to suite your needs, but I hope this was close enough.

            Regards:rose:

            N Offline
            N Offline
            NameYou
            wrote on last edited by
            #5

            Ah I see what you are saying - my problem is this: then I have multiple disk IO reads, which will slow the system down compared to one. In this project, speed is absolutely necessary. Is there any way that I can write it to memory, so that I can combine the 4 results and write to the file once?

            C#... king of languages

            N 1 Reply Last reply
            0
            • N Nader Elshehabi

              NameYou wrote:

              How do I get the data in the Connect function to the calling function in MainClass?

              You don't need to. If there is no good reason not to access the file from that method, you should. To be specific, here is my suggestion of you MyObject.Connect() method:

              Public class MyObject
              {
              public MyObject()
              {
              }

              public void Connect()
              {
                  //Starts all the socket stuff..
                  object LockObject = new object()
                  lock(LockObject)
                  {
                      StreamWriter MyStream = File.AppendText("MyFile.txt");
                      MyStream.WriteLine(Input);
                      MyStream.Close();
                  }
              }
              //...OTHER SOCKET RELATED FUNCTIONS GO HERE.
              

              }

              Of course you got to do some modifications to the code to suite your needs, but I hope this was close enough.

              Regards:rose:

              E Offline
              E Offline
              Eric Dahlvang
              wrote on last edited by
              #6

              I'm no threading expert, but I think you need to consider that NameYou is creating multiple instances of MyObject. When you create LockObject in each instance, that means that each instances Connect code has a lock -- which is different from locking that piece of code for EVERY instance. You can see the difference by comparing the following two classes: 1)This just locks the code in this instances connect method.

              public class MyObject
              {
                  public static string cFieldVal;
              
                  public MyObject()
                  {
                  }
              
                  public void Connect()
                  {
                      //Starts all the socket stuff..
                      object LockObject = new object();
                      lock(LockObject)
                      {
                          MessageBox.Show(MyObject.cFieldVal);
                          MyObject.cFieldVal=MyObject.cFieldVal+="x";
                          //StreamWriter MyStream = File.AppendText("MyFile.txt");
                          //MyStream.WriteLine(Input);
                         // MyStream.Close();
                      }
                  }
                  //...OTHER SOCKET RELATED FUNCTIONS GO HERE.
              }
              

              2)This code locks a static field, so it is locked for EACH EVERY instance.

              public class MyObject
              {
              
                  public static string cFieldVal="";
              
                  public MyObject()
                  {
                  }
              
                  public void Connect()
                  {
                      //Starts all the socket stuff..
                      
                      lock(cFieldVal)
                      {
                          MessageBox.Show(MyObject.cFieldVal);
                          MyObject.cFieldVal=MyObject.cFieldVal+="x";
                        //  StreamWriter MyStream = File.AppendText("MyFile.txt");
                        //  MyStream.WriteLine(Input);
                        //  MyStream.Close();
                      }
                  }
              //...OTHER SOCKET RELATED FUNCTIONS GO HERE.
              }
              

              -- modified at 8:57 Wednesday 13th December, 2006

              --EricDV Sig--------- Some problems are so complex that you have to be highly intelligent and well informed just to be undecided about them. - Laurence J. Peters

              A N 2 Replies Last reply
              0
              • N NameYou

                Ah I see what you are saying - my problem is this: then I have multiple disk IO reads, which will slow the system down compared to one. In this project, speed is absolutely necessary. Is there any way that I can write it to memory, so that I can combine the 4 results and write to the file once?

                C#... king of languages

                N Offline
                N Offline
                Nader Elshehabi
                wrote on last edited by
                #7

                NameYou wrote:

                Is there any way that I can write it to memory

                Sure! You can make a string object in Connect method, keep concatenating strings to it, then finally at the end of your data reading write the whole string to the file. It's almost the same way. PS. EricDV is right. You should first make the LockObject private static for the lock to work. Another way to do it is to use Mutex class instead.

                Regards:rose:

                N 1 Reply Last reply
                0
                • E Eric Dahlvang

                  I'm no threading expert, but I think you need to consider that NameYou is creating multiple instances of MyObject. When you create LockObject in each instance, that means that each instances Connect code has a lock -- which is different from locking that piece of code for EVERY instance. You can see the difference by comparing the following two classes: 1)This just locks the code in this instances connect method.

                  public class MyObject
                  {
                      public static string cFieldVal;
                  
                      public MyObject()
                      {
                      }
                  
                      public void Connect()
                      {
                          //Starts all the socket stuff..
                          object LockObject = new object();
                          lock(LockObject)
                          {
                              MessageBox.Show(MyObject.cFieldVal);
                              MyObject.cFieldVal=MyObject.cFieldVal+="x";
                              //StreamWriter MyStream = File.AppendText("MyFile.txt");
                              //MyStream.WriteLine(Input);
                             // MyStream.Close();
                          }
                      }
                      //...OTHER SOCKET RELATED FUNCTIONS GO HERE.
                  }
                  

                  2)This code locks a static field, so it is locked for EACH EVERY instance.

                  public class MyObject
                  {
                  
                      public static string cFieldVal="";
                  
                      public MyObject()
                      {
                      }
                  
                      public void Connect()
                      {
                          //Starts all the socket stuff..
                          
                          lock(cFieldVal)
                          {
                              MessageBox.Show(MyObject.cFieldVal);
                              MyObject.cFieldVal=MyObject.cFieldVal+="x";
                            //  StreamWriter MyStream = File.AppendText("MyFile.txt");
                            //  MyStream.WriteLine(Input);
                            //  MyStream.Close();
                          }
                      }
                  //...OTHER SOCKET RELATED FUNCTIONS GO HERE.
                  }
                  

                  -- modified at 8:57 Wednesday 13th December, 2006

                  --EricDV Sig--------- Some problems are so complex that you have to be highly intelligent and well informed just to be undecided about them. - Laurence J. Peters

                  A Offline
                  A Offline
                  Andy Brummer
                  wrote on last edited by
                  #8

                  Actually, the first block doesn't do any locking at all since each method doesn't share the lock object. Your example blocks all instances, it doesn't matter how many MyObjects there are, only one of them can execute the locked code at any time. Removing the static keyword from cFieldVal will protect the value for each instance.

                  I can imagine the sinking feeling one would have after ordering my book, only to find a laughably ridiculous theory with demented logic once the book arrives - Mark McCutcheon

                  E 1 Reply Last reply
                  0
                  • E Eric Dahlvang

                    I'm no threading expert, but I think you need to consider that NameYou is creating multiple instances of MyObject. When you create LockObject in each instance, that means that each instances Connect code has a lock -- which is different from locking that piece of code for EVERY instance. You can see the difference by comparing the following two classes: 1)This just locks the code in this instances connect method.

                    public class MyObject
                    {
                        public static string cFieldVal;
                    
                        public MyObject()
                        {
                        }
                    
                        public void Connect()
                        {
                            //Starts all the socket stuff..
                            object LockObject = new object();
                            lock(LockObject)
                            {
                                MessageBox.Show(MyObject.cFieldVal);
                                MyObject.cFieldVal=MyObject.cFieldVal+="x";
                                //StreamWriter MyStream = File.AppendText("MyFile.txt");
                                //MyStream.WriteLine(Input);
                               // MyStream.Close();
                            }
                        }
                        //...OTHER SOCKET RELATED FUNCTIONS GO HERE.
                    }
                    

                    2)This code locks a static field, so it is locked for EACH EVERY instance.

                    public class MyObject
                    {
                    
                        public static string cFieldVal="";
                    
                        public MyObject()
                        {
                        }
                    
                        public void Connect()
                        {
                            //Starts all the socket stuff..
                            
                            lock(cFieldVal)
                            {
                                MessageBox.Show(MyObject.cFieldVal);
                                MyObject.cFieldVal=MyObject.cFieldVal+="x";
                              //  StreamWriter MyStream = File.AppendText("MyFile.txt");
                              //  MyStream.WriteLine(Input);
                              //  MyStream.Close();
                            }
                        }
                    //...OTHER SOCKET RELATED FUNCTIONS GO HERE.
                    }
                    

                    -- modified at 8:57 Wednesday 13th December, 2006

                    --EricDV Sig--------- Some problems are so complex that you have to be highly intelligent and well informed just to be undecided about them. - Laurence J. Peters

                    N Offline
                    N Offline
                    Nader Elshehabi
                    wrote on last edited by
                    #9

                    You are right. I've tested it. I hope I don't miss that next time.:)

                    Regards:rose:

                    1 Reply Last reply
                    0
                    • N NameYou

                      I am developing a windows form app in C# -.net 1.1 framework. The app creates 4 objects and executes a "Go" method in 4 threads - each thread goes off, grabs some data from a UDP socket, and processes it. What I need is to get the processed data from each of the 4 threads (objects) and combine them into one string. I can not figure out how to get the individual threads to return anything. I tried to create a string and pass it to each object's constructor as a reference, but that went nowhere. Does anyone have any ideas on how I can accomplish this? Dan Senior Developer Mitsubishi Power Systems

                      Now where did I put that Char....

                      K Offline
                      K Offline
                      karam chandrabose
                      wrote on last edited by
                      #10

                      I would suggest you to use asynchronous method invocation and callbacks to make this task much much simpler

                      N 1 Reply Last reply
                      0
                      • K karam chandrabose

                        I would suggest you to use asynchronous method invocation and callbacks to make this task much much simpler

                        N Offline
                        N Offline
                        NameYou
                        wrote on last edited by
                        #11

                        I'm not sure I understand Asynchronous method calling? I've never seen that before. Could you point me in the direction of some information on that, or shoot me over some example code?

                        uh... yeah

                        1 Reply Last reply
                        0
                        • N Nader Elshehabi

                          NameYou wrote:

                          Is there any way that I can write it to memory

                          Sure! You can make a string object in Connect method, keep concatenating strings to it, then finally at the end of your data reading write the whole string to the file. It's almost the same way. PS. EricDV is right. You should first make the LockObject private static for the lock to work. Another way to do it is to use Mutex class instead.

                          Regards:rose:

                          N Offline
                          N Offline
                          NameYou
                          wrote on last edited by
                          #12

                          OK, I understand that, but what I am getting at is that if I had the data write to a file at the end of the reading routine, that file write routine would be executed 4 times (since I have four threads) which is slower than dumping all of the read data into a buffer and then writing the buffer outside of the 4 threads - am I correct? Speed is of the essence with this procedure.

                          C#, king of languages

                          N 1 Reply Last reply
                          0
                          • A Andy Brummer

                            Actually, the first block doesn't do any locking at all since each method doesn't share the lock object. Your example blocks all instances, it doesn't matter how many MyObjects there are, only one of them can execute the locked code at any time. Removing the static keyword from cFieldVal will protect the value for each instance.

                            I can imagine the sinking feeling one would have after ordering my book, only to find a laughably ridiculous theory with demented logic once the book arrives - Mark McCutcheon

                            E Offline
                            E Offline
                            Eric Dahlvang
                            wrote on last edited by
                            #13

                            Andy Brummer wrote:

                            Actually, the first block doesn't do any locking at all since each method doesn't share the lock object.

                            That's what i was trying to say.

                            Andy Brummer wrote:

                            Your example blocks all instances, it doesn't matter how many MyObjects there are, only one of them can execute the locked code at any time. Removing the static keyword from cFieldVal will protect the value for each instance.

                            The intention of the example was to have a lock that every instance shares, and I should have said EVERY rather than EACH. I have changed it. Thanks for pointing that out.

                            --EricDV Sig--------- Some problems are so complex that you have to be highly intelligent and well informed just to be undecided about them. - Laurence J. Peters

                            1 Reply Last reply
                            0
                            • N NameYou

                              OK, I understand that, but what I am getting at is that if I had the data write to a file at the end of the reading routine, that file write routine would be executed 4 times (since I have four threads) which is slower than dumping all of the read data into a buffer and then writing the buffer outside of the 4 threads - am I correct? Speed is of the essence with this procedure.

                              C#, king of languages

                              N Offline
                              N Offline
                              Nader Elshehabi
                              wrote on last edited by
                              #14

                              Again this won't hurt. It would be a matter of milliseconds. Yet, if this small time really makes a difference with you, try benchmarking your program. For example, I do it by comparing the value of DateTime.Now.Ticks before and after the procedure I'm testing. See which method would give you the most satisfying results. PS. If you really need speed this much, why don't you think of unmanaged code -e.g. C++-?

                              Regards:rose:

                              1 Reply Last reply
                              0
                              • N NameYou

                                I am developing a windows form app in C# -.net 1.1 framework. The app creates 4 objects and executes a "Go" method in 4 threads - each thread goes off, grabs some data from a UDP socket, and processes it. What I need is to get the processed data from each of the 4 threads (objects) and combine them into one string. I can not figure out how to get the individual threads to return anything. I tried to create a string and pass it to each object's constructor as a reference, but that went nowhere. Does anyone have any ideas on how I can accomplish this? Dan Senior Developer Mitsubishi Power Systems

                                Now where did I put that Char....

                                S Offline
                                S Offline
                                sno 1
                                wrote on last edited by
                                #15

                                first sorry if I give'nt you a good answer because I dont undertand you a lot, ok this is fact: if you need know the process data of this 4 thread, you can use many tools, such as delegate, create a delegate and add all this function inside this, and then put the delegate on execution, for more explanation look this example, using a ProgressBar... public delegate void ExceutionAsyncronous(); public void function1(){} public void function2(){} public void function3(){} public void RequestTime(ProgressBar som, int value, int max) { som.Increment(value++,max); } inside your function put this method "RequestTime" in a way that the increment always keeping inside a correct interval, an then ... ExceutionAsyncronous ob = new ExceutionAsyncronous(function1); ob+=new ExceutionAsyncronous(function2); ob+=new ExceutionAsyncronous(function3); ob(); Remember that this explanation only want give you a Idea, ok if you need know anything else, please write me ok :-D:-D:-D

                                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