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 6545857
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T11:38:12+00:00 2026-05-25T11:38:12+00:00

I have a method that listens for data send through a stream. because the

  • 0

I have a method that listens for data send through a stream. because the networkstream.read method is synchronous I have to execute that on a separate thread. so the method that I execute on a separate thread is:

void someMethod(param1, param2....)
{
    try{
     while( stream.read(data...) != 0 )  // code waits here until data is received
     {
        // do stufff

     } 
    }catch{}//
}

the only way I am able to exit that thread is by closing the stream and the catch block will execute. I sometimes need to start listening for data on the same stream but on a different method. so if I create the thread as Thread t = new Thread(new threadstart(somemethod……) then do t.start( someObject); later when I do t.abort() ; that method will still be listening for data. How can I terminate it?

also I have tried creating a global variable such as:

bool someBoolean = false;

void someMethod(param1, param2....)
{
    try{
     while(someBoolean ==false && stream.read(data...) != 0 )  // code waits here until data is received
     {
        // do stufff

     } 
    }catch{}//
}

then I figured that I changed someBoolean to true the method will stop executing. for some reason it does not. Why? it seems like someBoolean is two variables at once. Because changing its value from the main thread does not seem to impact it on the second thread…

  • 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-25T11:38:13+00:00Added an answer on May 25, 2026 at 11:38 am

    I guess the thread might be abortet but then the resources from that thread (unmanaged because of stream) will not be freed – that is your problem.

    Don’t stop the thread with abort – never.

    Do stop it try using a async version of stream.read together with a CancellationToken.
    If you give us more info (what stream? – read is strange (“R”ead?), starting of the thread) I might try to give you more details.

    PS: try using System.Threading.Task instead of threads – way nicer to work with.

    as a quick fix without further info, you could try something like this:

    ManualThreadEvent terminate = new ManualThreadEvent(false);
    bool someBoolean = false;
    
    void someMethod(param1, param2....)
    {
        try{
         // wait for enough data or for termination:
         while(terminate.WaitOne(100) == false && stream.DataAvaiable <= bytesNeeded) 
         { /* nothing to do here */ }
    
         if (terminate.WaitOne(0)) return; // terminate on request
    
         // To your stuff with read
        }catch{}//
    }
    
    void Terminate()
    {
      terminate.Set();
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a small method I use to disable my socket that listens for
I have a COM (C++) API that listens for data updates from a server
I have class method that returns a list of employees that I can iterate
I have a method that where I want to redirect the user back to
I have a method that can return either a single object or a collection
I have a method that takes an IQueryable. Is there a LINQ query that
I have a method that periodically (e.g. once in every 10 secs) try to
I have a method that's about ten lines of code. I want to create
I have a method that needs to accept an array of country names, and
I have a method that creates a MessageDigest (a hash) from a file, and

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.