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. Returning value from threads

Returning value from threads

Scheduled Pinned Locked Moved C#
questioncsharp
2 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.
  • S Offline
    S Offline
    samtam
    wrote on last edited by
    #1

    can anyone tell me, how can i return values from threads in C#? Reply will be appreciated Thanx in advance

    W 1 Reply Last reply
    0
    • S samtam

      can anyone tell me, how can i return values from threads in C#? Reply will be appreciated Thanx in advance

      W Offline
      W Offline
      WillemM
      wrote on last edited by
      #2

      You will have to drop the return value in a place that is accessable by another thread. You will have to protect the storage with a mutex, also add a condition to signal the other thread that processing is done. This is needed because processes can't return things to eachother, instead they storage data that has to be exchanged in a central place that is accessable by others. These two steps start the threads and let the first thread wait for a signal of the other thread. Thread 1 -> Thread2.Start(); Thread 1 -> Condition.Wait(); These steps are performed by the second thread in such a way that the storage can only be accessed by one party at a time. After the second thread is done, it will signal the first thread that it's done. Thread 2 -> Mutex.Lock(); Thread 2 -> Set some value Thread 2 -> Mutex.Unlock(); Thread 2 -> Condition.Signal(); These last steps are performed after the first thread gets a signal that the second thread is done. Thread 1 -> Mutex.Lock(); Thread 1 -> Get value Thread 1 -> Mutex.Unlock(); I'm not sure to what extend Microsoft implemented threading and concurrency tools, but if you don't have the Mutex or the Condition, you can do it with a Semaphore. The same trick, except for these changes: Thread 1 -> Semaphore.P(); Thread 2 -> Set value in storage Thread 2 -> Semaphore.V(); Thread 1 -> Get value from storage This classic is also called the producer/consumer scenario.

      WM.
      What about weapons of mass-construction?

      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