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. Switching between two threads

Switching between two threads

Scheduled Pinned Locked Moved C#
helptutorialquestion
5 Posts 2 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.
  • P Offline
    P Offline
    praveen pandey
    wrote on last edited by
    #1

    hi all, How to switch between two threads ? I am having two threads first one is main thread and second one a thread in which a function is called.In second thread,while loop is there.When one iteration is completed in loop i have to switch to main thread and call a function and again come to second thread and repeat the process. Please help me..

    L 1 Reply Last reply
    0
    • P praveen pandey

      hi all, How to switch between two threads ? I am having two threads first one is main thread and second one a thread in which a function is called.In second thread,while loop is there.When one iteration is completed in loop i have to switch to main thread and call a function and again come to second thread and repeat the process. Please help me..

      L Offline
      L Offline
      Leslie Sanford
      wrote on last edited by
      #2

      praveen pandey wrote:

      I am having two threads first one is main thread and second one a thread in which a function is called.In second thread,while loop is there.When one iteration is completed in loop i have to switch to main thread and call a function and again come to second thread and repeat the process.

      You have the threads signal each other when control should pass from one to the other. While one thread is doing work, the other waits. When the work is finished, the working thread signals the waiting thread that it's done. It then begins waiting while the other thread does its work. Typically, a worker thread looks something like this:

      private void ThreadMethod()
      {
      lock(lockObject)
      {
      while(someConditionIsTrue)
      {
      Monitor.Wait(lockObject);

              // Do some work...
          }
      }
      

      }

      From another thread, you have to pulse the lock to get the worker thread running:

      lock(lockObject)
      {
      Monitor.Pulse(lockObject);
      }

      However, since one of these threads is the main thread, you probably don't want it blocking while the other thread is doing work. This could tie up your UI. So we need more information about the kind of application you're writing. Is it a console app? A Window Forms app?

      P 1 Reply Last reply
      0
      • L Leslie Sanford

        praveen pandey wrote:

        I am having two threads first one is main thread and second one a thread in which a function is called.In second thread,while loop is there.When one iteration is completed in loop i have to switch to main thread and call a function and again come to second thread and repeat the process.

        You have the threads signal each other when control should pass from one to the other. While one thread is doing work, the other waits. When the work is finished, the working thread signals the waiting thread that it's done. It then begins waiting while the other thread does its work. Typically, a worker thread looks something like this:

        private void ThreadMethod()
        {
        lock(lockObject)
        {
        while(someConditionIsTrue)
        {
        Monitor.Wait(lockObject);

                // Do some work...
            }
        }
        

        }

        From another thread, you have to pulse the lock to get the worker thread running:

        lock(lockObject)
        {
        Monitor.Pulse(lockObject);
        }

        However, since one of these threads is the main thread, you probably don't want it blocking while the other thread is doing work. This could tie up your UI. So we need more information about the kind of application you're writing. Is it a console app? A Window Forms app?

        P Offline
        P Offline
        praveen pandey
        wrote on last edited by
        #3

        It's a windows app. Here is scenario. secondThreadMethod() { while(conditonIsTrue) { some code; //After one iteration i want to come out(since conditon is still true) and goto main thread finish some work and come back to this thread and continue in this way. } } thanks

        L 1 Reply Last reply
        0
        • P praveen pandey

          It's a windows app. Here is scenario. secondThreadMethod() { while(conditonIsTrue) { some code; //After one iteration i want to come out(since conditon is still true) and goto main thread finish some work and come back to this thread and continue in this way. } } thanks

          L Offline
          L Offline
          Leslie Sanford
          wrote on last edited by
          #4

          praveen pandey wrote:

          It's a windows app.

          Ok, well in your thread loop, you could marshal a delegate invocation to the main thread:

          private void ThreadMethod()
          {
          lock(lockObject)
          {
          while(someConditionIsTrue)
          {
          Monitor.Wait(lockObject);

                  // Do some work...        
          
                  BeginInvoke(new WorkDelegate(DoWorkOnMainThread));
              }    
          }
          

          }

          private delegate void WorkDelegate();

          private void DoWorkOnMainThread()
          {
          lock(lockObject)
          {
          // Do some work on main thread.

              Monitor.Pulse(lockObject);
          }
          

          }

          This assumes you're writing a Forms application. The idea is that you have the main thread running and not blocking while the worker thread does its work. Then when you need work done on the main thread, the worker thread marshals a delegate invocation that does the work to the main thread through BeginInvoke. The method on the main thread signals the worker thread when it's done so that the worker thread can continue for another iteration.

          P 1 Reply Last reply
          0
          • L Leslie Sanford

            praveen pandey wrote:

            It's a windows app.

            Ok, well in your thread loop, you could marshal a delegate invocation to the main thread:

            private void ThreadMethod()
            {
            lock(lockObject)
            {
            while(someConditionIsTrue)
            {
            Monitor.Wait(lockObject);

                    // Do some work...        
            
                    BeginInvoke(new WorkDelegate(DoWorkOnMainThread));
                }    
            }
            

            }

            private delegate void WorkDelegate();

            private void DoWorkOnMainThread()
            {
            lock(lockObject)
            {
            // Do some work on main thread.

                Monitor.Pulse(lockObject);
            }
            

            }

            This assumes you're writing a Forms application. The idea is that you have the main thread running and not blocking while the worker thread does its work. Then when you need work done on the main thread, the worker thread marshals a delegate invocation that does the work to the main thread through BeginInvoke. The method on the main thread signals the worker thread when it's done so that the worker thread can continue for another iteration.

            P Offline
            P Offline
            praveen pandey
            wrote on last edited by
            #5

            Thanks alot. It really worked in the way what i wanted. Thanks Again...

            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