Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7428105
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T08:41:31+00:00 2026-05-29T08:41:31+00:00

I have a little problem with a Thread. I execute the function Receive with

  • 0

I have a little problem with a Thread. I execute the function Receive with time-out set to 600, but it ends here

if ((thr.ThreadState != ThreadState.Running) 
    && (thr.ThreadState != ThreadState.WaitSleepJoin))
{
  ThreadState st = thr.ThreadState;
  lg.WriteString("EXIT BY THREAD END");
  lg.WriteString("LAST: "+st.ToString());
  break;
}

The function should end there only if the thread is over and I have received all the data, but when I open the file errores.txt I see that the ThreadState value is Running or WaitSleepJoin.

Any idea why the function is aborting?

More code below ->

bool exit = false;
void WaitAndRead() {
if (buffer == null)
  buffer = new List<byte>();
buffer.Clear();
try
{
  while (sock_client.Available < 1 && !exit)
    Thread.Sleep(10);
  while (sock_client.Available > 0 && !exit)
  {
    byte[] b = new byte[sock_client.Available];
    sock_client.Receive(b, 0, b.Length, SocketFlags.None);
    buffer.AddRange(b);
    Thread.Sleep(25);
  }
}
catch (Exception ex) 
{ 
  Log lg = new Log("C:\\TPV\\errores.txt"); 
  lg.WriteException(ex, true); 
}

public int Receive(int timeout,out List<byte> result) 
{
  result = new List<byte>();
  try
  {
    if (!Connected)
      return NOT_CONNECTED;
    bool ok = true;
    ThreadStart str = delegate 
    {
      try { WaitAndRead(); }
      catch (Exception ex) 
      { 
        Log lg = new Log("C:\\TPV\\errores.txt"); 
        lg.WriteException(ex, true); ok = false; }
      };
      exit = false;
      Thread thr = new Thread(str);
      thr.Start();
      int x = 0;
      int max = 10 * timeout;
      while (x < max)
      {
        Log lg = new Log("C:\\TPV\\errores.txt");
        if ((thr.ThreadState != ThreadState.Running) 
            && (thr.ThreadState != ThreadState.WaitSleepJoin))
        {
          ThreadState st = thr.ThreadState;
          lg.WriteString("EXIT BY THREAD END");
          lg.WriteString("LAST: "+st.ToString());
          break;
        }
        x++;
        Thread.Sleep(10);
      }
      exit = true;
      if (x >= max)
        return Timeout;
      result = new List<byte>(buffer.ToArray());
      return ok?OK:NOT_CONNECTED;
    }
    catch(Exception ex) 
    {
      Log lg = new Log("C:\\TPV\\errores.txt"); 
      lg.WriteException(ex, true);
      return NOT_CONNECTED;
    }
  }
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-29T08:41:32+00:00Added an answer on May 29, 2026 at 8:41 am

    It may be possible that the value of the ThreadState property is changing while this code is evaluating:

    if ((thr.ThreadState != ThreadState.Running) && (thr.ThreadState != ThreadState.WaitSleepJoin))
    {
        ThreadState st = thr.ThreadState;
        lg.WriteString("EXIT BY THREAD END");
        lg.WriteString("LAST: "+st.ToString());
        break;
    }
    

    Consider the following scenario:

    1. thr.ThreadState is WaitSleepJoin
    2. first part of if statement evaluates to true
    3. thr.ThreadState changes to Running before second part of if is evaluated
    4. second part of if statement evaluates to true
    5. since both parts evaluated to true, the if block is entered

    You could try rewriting the code as shown below so you are only checking the value of the ThreadState property once:

    var state = thr.ThreadState;
    if (state != ThreadState.Running && state != ThreadState.WaitSleepJoin)
    {
        lg.WriteString("EXIT BY THREAD END");
        lg.WriteString("LAST: "+ state.ToString());
        break;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

i have little problem with boost::asio library. My app receive and process data asynchronously,
I have a little problem with some jquery and http://www.mikage.to/jquery/jquery_history_noc.html The function works great,
I have this little problem, that I cannot figure out which arguments to pass
I have a little problem. I had some JComboBox to a JDialog but they
I have multithreaded application and I've got a little problem when application ends: I
I have a little problem with AlertDialogs in my application. So here is my
I have a little problem I spend a lot of time now. I hope
I have a little problem with a Listview. I can load it with listview
I have a little problem with a simple vbScript. The script has to run
I have a little problem where I would like to insert a svn diff

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.