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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T03:26:33+00:00 2026-05-28T03:26:33+00:00

I spent some time searching for an answer to this and found plenty of

  • 0

I spent some time searching for an answer to this and found plenty of helpful information in other threads. I believe I’ve written the code in a way that works, but I am not happy with the outcome.

I designed a piece of hardware that I am communicating with via C#. The hardware connects via USB and runs initialization routines after enumerating with the OS. At that point, it simply waits for the C# program to start sending commands. In my C# code, the user must press a “Connect” button, which sends a command and the required payload to let the hardware know it should continue running. The hardware then sends a command back as an ACK. The problem is that my C# program must wait to receive the ACK, but the GUI is totally frozen until the hardware responds as I don’t know how to partition it out to another thread that can block freely. If the hardware responds immediately, then it works fine, but if it can’t connect, then the program stays frozen indefinitely.

With that said, I know a few things need to happen, but I’m not sure how to implement them. First and foremost, I don’t think sitting in a loop waiting on a boolean is the right way to go, but using AutoResetEvent doesn’t really seem to be much better. There has to be a better way involving timers, more threads, or something similar.

I am using the DataReceived event with the serialPort object as follows:

private void serialPort1_DataReceived(object sender, System.IO.Ports.SerialDataReceivedEventArgs e)
{
    byte cmd = (byte)serialPort1.ReadByte();

    if (cmd == (byte)Commands.USB_UART_CMD_MCU_CONNECT)
        MCU_Connect_Received.Set();

}

In the buttonClick function (“main” thread), the program stops while it waits for the ACK:

//Send the command to signal a connection
Send_Connection_Packet((byte)Commands.USB_UART_CMD_PC_CONNECT);

textBox1.AppendText("-I- Attempting to contact hardware...");

MCU_Connect_Received.WaitOne();

textBox1.AppendText("Success!" + Environment.NewLine);

Ideally, I’d like to know if a timeout expired so I can print “Failed!” instead of “Success!”. Not having a timeout also means it will sit there forever, as I mentioned above, until I kill the process. It’s possible that it won’t find any hardware, but if it does, it should respond in < 1 second, so a timeout of 2 seconds would be more than enough. I tried using Thread.Sleep, but that froze the GUI as well.

  • 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-28T03:26:34+00:00Added an answer on May 28, 2026 at 3:26 am

    I recommend you use the Task class. You can use a TaskCompletionSource to complete the task when the operation completes.

    Using the new async support, your code then becomes:

    textBox1.AppendText("-I- Attempting to contact hardware...");
    await Send_Connection_Packet((byte)Commands.USB_UART_CMD_PC_CONNECT);
    textBox1.AppendText("Success!" + Environment.NewLine); 
    

    If you don’t want to use the Async CTP, then you can call Task.ContinueWith and pass TaskScheduler.FromCurrentSynchronizationContext to schedule the textBox1.AppendText("Success!") line to run on the UI thread.

    The async support also includes timers (TaskEx.Delay) and combinators (TaskEx.WhenAny), so you can easily check for timeouts:

    textBox1.AppendText("-I- Attempting to contact hardware...");
    var commTask = Send_Connection_Packet((byte)Commands.USB_UART_CMD_PC_CONNECT);
    var timeoutTask = TaskEx.Delay(1000);
    var completedTask = TaskEx.WhenAny(commTask, timeoutTask);
    if (completedTask == commTask)
      textBox1.AppendText("Success!" + Environment.NewLine); 
    else
      textBox1.AppendText("Timeout :(" + Environment.NewLine);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've spent some time searching for this answer on SO, but couldn't find it,
I've spent some time searching on the Internet and playing around with my code
I've spent some time researching this and simply can't find a definitive answer. Here
I've spent some time searching around how to configure NHibernate's FlushMode so it could
i've spent some time searching and there is probably is an easy way to
I spent some time trying to figure out why this query isn't pulling the
I've spent some time looking over other questions but I dont really understand some
I just spent a long time breaking my teeth on why this code was
I spent some time this morning trying to help a colleague with an odd
I've spent quite some time searching to see if there was a similar issue.

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.