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

  • SEARCH
  • Home
  • 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 8324763
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T00:11:31+00:00 2026-06-09T00:11:31+00:00

I have a utility I’ve written in C# to communicate back and forth with

  • 0

I have a utility I’ve written in C# to communicate back and forth with our USB device. We use the generic HID driver and wrap a handle to the device in a FileStream object. I read/write data using its BeginRead and BeginWrite methods, not because I need asynchronous IO, but simply so I can timeout in case the device enters a noncommunicable state (intentionally or not). All reading/writing is done in my own dedicated IO thread.

I’m worried that I’m not quite doing things properly, because I’ve seen a few cases of what I suspect is thread deadlock. Here is a stripped down version of my relevant Read method (which seems to work just fine).

if (_readResult == null)
{
  _readResult = _deviceStream.BeginRead(_readBuffer, 0, _readBuffer.Length, null, null);
}

if (_readResult.AsyncWaitHandle.WaitOne(IOTimeout, true))
{
  int bytesRead = _deviceStream.EndRead(_readResult);
  _readResult.AsyncWaitHandle.Close();
  _readResult= null;
  // … Copy bytes to another buffer
}
else
{
  // … Timeout, so retry again in a bit
}

My primary question is how to properly stop an unfinished BeginRead or BeginWrite call if I need to terminate my IO thread and my device is no longer communicating. I can’t just call EndRead because it will sit there and block forever. Is it safe to call Filestream.Close while read/write operations are pending?

I also have to ask, is it safe to have pending read and write operations running simultaneously? For example, if my read method times out, can I still go ahead and try to write something?

It’s hard to reproduce my current deadlock issue, but the really strange part is it seems to start when the IO thread gets “stuck” in my read method. I’m not sure how that would even happen unless my code isn’t working the way I think it is.

  • 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-06-09T00:11:34+00:00Added an answer on June 9, 2026 at 12:11 am

    There is no built-in cancellation. Closing the stream is the recommended solution. Just make sure you catch exceptions when you call EndRead…

    If you want to use .NET 4.5, there is a new FileStream.ReadAsync that supports cancelation: http://msdn.microsoft.com/en-us/library/hh158566(v=vs.110)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a utility that I have written in VB.net that runs as a
I have a utility class that needs to work on a generic Class but
I have a utility that is written in Python and is meant to be
I have a utility class library I am writing. A have written a factory
I have a utility class from which I want to use one of the
Okay, so we have a utility here in-house that generates business-model classes from our
We have a utility spring-mvc application that doesn't use a database, it is just
I have a utility class with some static methods which I use in some
I have a utility app with the flipsideview and I'm trying to use the
I have some utility class with worker thread with simple exit condition. I use

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.