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. Thread problem.

Thread problem.

Scheduled Pinned Locked Moved C#
helpquestion
8 Posts 5 Posters 1 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.
  • P Offline
    P Offline
    pubududilena
    wrote on last edited by
    #1

    Hi All, I need to run 3 methods parallaly.Then I have used Threading for that. Here is the code. public void threadtest() { Thread SpecialThread = new Thread(new ThreadStart(this.GetSpecial)); Thread BulkThread = new Thread(new ThreadStart(this.GetBulk)); Thread NormalThread = new Thread(new ThreadStart(this.GetNormal)); SpecialThread.Start(); BulkThread.Start(); NormalThread.Start(); SpecialThread.Join(); BulkThread.Join(); NormalThread.Join(); while((NormalThread.ThreadState==System.Threading.ThreadState.Running) || (BulkThread.ThreadState==System.Threading.ThreadState.Running) || (SpecialThread.ThreadState==System.Threading.ThreadState.Running) ) { } } But most of time one or two threads are going to aborted or something happen and I loose their results.Sometimes this worked properly and I can get all results from 3 methods. Can any one correct this code?

    P M M L 4 Replies Last reply
    0
    • P pubududilena

      Hi All, I need to run 3 methods parallaly.Then I have used Threading for that. Here is the code. public void threadtest() { Thread SpecialThread = new Thread(new ThreadStart(this.GetSpecial)); Thread BulkThread = new Thread(new ThreadStart(this.GetBulk)); Thread NormalThread = new Thread(new ThreadStart(this.GetNormal)); SpecialThread.Start(); BulkThread.Start(); NormalThread.Start(); SpecialThread.Join(); BulkThread.Join(); NormalThread.Join(); while((NormalThread.ThreadState==System.Threading.ThreadState.Running) || (BulkThread.ThreadState==System.Threading.ThreadState.Running) || (SpecialThread.ThreadState==System.Threading.ThreadState.Running) ) { } } But most of time one or two threads are going to aborted or something happen and I loose their results.Sometimes this worked properly and I can get all results from 3 methods. Can any one correct this code?

      P Offline
      P Offline
      phannon86
      wrote on last edited by
      #2

      Repost. Though I'd seen it before and yes, a week ago you posted the same code. A complete c+p actually. No-one helped you then, what makes you think they will now?

      He who makes a beast out of himself gets rid of the pain of being a man

      L 1 Reply Last reply
      0
      • P pubududilena

        Hi All, I need to run 3 methods parallaly.Then I have used Threading for that. Here is the code. public void threadtest() { Thread SpecialThread = new Thread(new ThreadStart(this.GetSpecial)); Thread BulkThread = new Thread(new ThreadStart(this.GetBulk)); Thread NormalThread = new Thread(new ThreadStart(this.GetNormal)); SpecialThread.Start(); BulkThread.Start(); NormalThread.Start(); SpecialThread.Join(); BulkThread.Join(); NormalThread.Join(); while((NormalThread.ThreadState==System.Threading.ThreadState.Running) || (BulkThread.ThreadState==System.Threading.ThreadState.Running) || (SpecialThread.ThreadState==System.Threading.ThreadState.Running) ) { } } But most of time one or two threads are going to aborted or something happen and I loose their results.Sometimes this worked properly and I can get all results from 3 methods. Can any one correct this code?

        M Offline
        M Offline
        Martin 0
        wrote on last edited by
        #3

        Hello, Why are you doing the "Join()"? What do the Methods of the Threads do? What are the results you are expecting and where do you get them from?

        All the best, Martin

        P 1 Reply Last reply
        0
        • P pubududilena

          Hi All, I need to run 3 methods parallaly.Then I have used Threading for that. Here is the code. public void threadtest() { Thread SpecialThread = new Thread(new ThreadStart(this.GetSpecial)); Thread BulkThread = new Thread(new ThreadStart(this.GetBulk)); Thread NormalThread = new Thread(new ThreadStart(this.GetNormal)); SpecialThread.Start(); BulkThread.Start(); NormalThread.Start(); SpecialThread.Join(); BulkThread.Join(); NormalThread.Join(); while((NormalThread.ThreadState==System.Threading.ThreadState.Running) || (BulkThread.ThreadState==System.Threading.ThreadState.Running) || (SpecialThread.ThreadState==System.Threading.ThreadState.Running) ) { } } But most of time one or two threads are going to aborted or something happen and I loose their results.Sometimes this worked properly and I can get all results from 3 methods. Can any one correct this code?

          M Offline
          M Offline
          mav northwind
          wrote on last edited by
          #4

          Hi! The whole while(... construct is superfluous. You start 3 threads and then call Join() for each of them. So the last Join() will return only when NormalThread has finished and the other two threads have finished as well. Depending on how you access the thread's "results", declaring the respective members as volatile might help.

          Regards, mav -- Black holes are the places where God divided by 0...

          1 Reply Last reply
          0
          • P phannon86

            Repost. Though I'd seen it before and yes, a week ago you posted the same code. A complete c+p actually. No-one helped you then, what makes you think they will now?

            He who makes a beast out of himself gets rid of the pain of being a man

            L Offline
            L Offline
            Le centriste
            wrote on last edited by
            #5

            So what? He is trying again and may have better luck. Why didn't you just ignore his post?

            1 Reply Last reply
            0
            • P pubududilena

              Hi All, I need to run 3 methods parallaly.Then I have used Threading for that. Here is the code. public void threadtest() { Thread SpecialThread = new Thread(new ThreadStart(this.GetSpecial)); Thread BulkThread = new Thread(new ThreadStart(this.GetBulk)); Thread NormalThread = new Thread(new ThreadStart(this.GetNormal)); SpecialThread.Start(); BulkThread.Start(); NormalThread.Start(); SpecialThread.Join(); BulkThread.Join(); NormalThread.Join(); while((NormalThread.ThreadState==System.Threading.ThreadState.Running) || (BulkThread.ThreadState==System.Threading.ThreadState.Running) || (SpecialThread.ThreadState==System.Threading.ThreadState.Running) ) { } } But most of time one or two threads are going to aborted or something happen and I loose their results.Sometimes this worked properly and I can get all results from 3 methods. Can any one correct this code?

              L Offline
              L Offline
              Le centriste
              wrote on last edited by
              #6

              I suggest using 3 AutoResetEvent that each thread would Set() when done. So, you start your three thread, and then, in your main thread, do a WaitHandle.WaitAll(....). It would be advised to use the overload with a timeout. Your code could look something like this:

              public MyClass
              {
                  private AutoResetEvent _specialEvent = new AutoResetEvent();
                  private AutoResetEvent _normalEvent = new AutoResetEvent();
                  private AutoResetEvent _bulkEvent = new AutoResetEvent();
              
                  public threadtest()
                  {
                      Thread SpecialThread = new Thread(new ThreadStart(this.GetSpecial));
                      Thread BulkThread = new Thread(new ThreadStart(this.GetBulk));
                      Thread NormalThread = new Thread(new ThreadStart(this.GetNormal));
              
                      SpecialThread.Start();
                      BulkThread.Start();
                      NormalThread.Start();
              
                      WaitHandle.WaitAll(new WaitHandle[] { this._specialEvent, this._normalEvent, this._bulkEvent } );
              
                      // Do stuff with the result.
                  }
              
                  public GetNormal()
                  {
                      // Do normal stuff.
                      this._normalEvent.Set();
                  }
              
                  public GetSpecial()
                  {
                      // Do special stuff.
                      this._specialEvent.Set();
                  }
              
                  public GetBulk()
                  {
                      // Do bulkstuff.
                      this._bulkEvent.Set();
                  }
              }
              

              You could consider using the BackgroundWorker class also.

              P 1 Reply Last reply
              0
              • M Martin 0

                Hello, Why are you doing the "Join()"? What do the Methods of the Threads do? What are the results you are expecting and where do you get them from?

                All the best, Martin

                P Offline
                P Offline
                pubududilena
                wrote on last edited by
                #7

                hi, thanks for reply. I am joining 3 threads with Join() method. Then 3 threads can execute parallary. There are 3 methods ,all are return same types of arrays.

                1 Reply Last reply
                0
                • L Le centriste

                  I suggest using 3 AutoResetEvent that each thread would Set() when done. So, you start your three thread, and then, in your main thread, do a WaitHandle.WaitAll(....). It would be advised to use the overload with a timeout. Your code could look something like this:

                  public MyClass
                  {
                      private AutoResetEvent _specialEvent = new AutoResetEvent();
                      private AutoResetEvent _normalEvent = new AutoResetEvent();
                      private AutoResetEvent _bulkEvent = new AutoResetEvent();
                  
                      public threadtest()
                      {
                          Thread SpecialThread = new Thread(new ThreadStart(this.GetSpecial));
                          Thread BulkThread = new Thread(new ThreadStart(this.GetBulk));
                          Thread NormalThread = new Thread(new ThreadStart(this.GetNormal));
                  
                          SpecialThread.Start();
                          BulkThread.Start();
                          NormalThread.Start();
                  
                          WaitHandle.WaitAll(new WaitHandle[] { this._specialEvent, this._normalEvent, this._bulkEvent } );
                  
                          // Do stuff with the result.
                      }
                  
                      public GetNormal()
                      {
                          // Do normal stuff.
                          this._normalEvent.Set();
                      }
                  
                      public GetSpecial()
                      {
                          // Do special stuff.
                          this._specialEvent.Set();
                      }
                  
                      public GetBulk()
                      {
                          // Do bulkstuff.
                          this._bulkEvent.Set();
                      }
                  }
                  

                  You could consider using the BackgroundWorker class also.

                  P Offline
                  P Offline
                  pubududilena
                  wrote on last edited by
                  #8

                  Hi, thanks a lot for reply. I will check with your codes and comeback soon. Is there a problem ,when I am using Join() method?. I need to run my 3 methods paralaly. all the 3 methods return same type of arrays.

                  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