Blocking a thread

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Zack00000
    New Member
    • Sep 2008
    • 5

    Blocking a thread

    Hi,

    I'm new with programming and I have a question relating thread. Please look at the following example:

    ....
    thread = new Thread(new ThreadStart(Ope n));
    thread.Start();
    ....

    private void Open()
    {
    try
    {
    while (true)
    {
    receiver = new StreamReader(cl ient.GetStream( ));
    input = receiver.ReadLi ne();
    ......

    I noticed that the thread is paused (or blocked) until "receiver" receive data. Now my question is how to replicates this behavior?
    Example:

    int DoSomething()
    {
    int result = 0;
    // do something here
    return result;
    }

    void ThreadFunction
    {
    while(true)
    {
    // process here
    int b = DoSomething() <-- i want the thread to "stop" here until there is actually something to do
    // process here some more
    }
    }

    Thanks in advance.
  • pootle
    New Member
    • Apr 2008
    • 68

    #2
    Check out System.Threadin g.WaitHandle.Wa itOne. Is this the behaviour you need? Check out MSDN for sample code.

    HTH

    Comment

    • joedeene
      Contributor
      • Jul 2008
      • 579

      #3
      Originally posted by Zack00000
      Hi,

      I'm new with programming and I have a question relating thread. Please look at the following example:

      ....
      thread = new Thread(new ThreadStart(Ope n));
      thread.Start();
      ....

      private void Open()
      {
      try
      {
      while (true)
      {
      receiver = new StreamReader(cl ient.GetStream( ));
      input = receiver.ReadLi ne();
      ......

      I noticed that the thread is paused (or blocked) until "receiver" receive data. Now my question is how to replicates this behavior?
      Example:

      int DoSomething()
      {
      int result = 0;
      // do something here
      return result;
      }

      void ThreadFunction
      {
      while(true)
      {
      // process here
      int b = DoSomething() <-- i want the thread to "stop" here until there is actually something to do
      // process here some more
      }
      }

      Thanks in advance.
      also please use code<#> tags for the code, to make it easier for members/experts to assist you with your development problems.

      joedeene

      Comment

      • Zack00000
        New Member
        • Sep 2008
        • 5

        #4
        Thanks for the link pootle, it is what I'm looking for. And sorry for the missing code tags joedeene.

        Comment

        Working...