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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T01:01:26+00:00 2026-06-11T01:01:26+00:00

I have created a timeout function based on things I have seen in various

  • 0

I have created a timeout function based on things I have seen in various places but am pretty sure I am not doing it a great way! (But it does seem to work.)

I am connecting to a piece of hardware that if working connects in a few seconds but if not takes around 1 minute to timeout. So if I can create my own timeout function I can set it at 20 seconds and save lots of time and waiting.

I have tried to make it so my timeout returns a string:

static string CallWithTimeout(Action action, int timeoutMilliseconds)
{
    string reply = "";
    Thread threadToKill = null;
    Action wrappedAction = () =>
    {
        threadToKill = Thread.CurrentThread;
        action();
    };

    IAsyncResult result = wrappedAction.BeginInvoke(null, null);
    if (result.AsyncWaitHandle.WaitOne(timeoutMilliseconds))
    {
        reply = "Connected";
        wrappedAction.EndInvoke(result);
        return reply;
    }
    else
    {
        threadToKill.Abort();
        reply = "Error";
        return reply;            
    }      
}

then I call it with something like :

string replyfromreader = CallWithTimeout(connectToHardware, 20000);

the connectToHardware is just a one liner so no need to post.

  • 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-11T01:01:27+00:00Added an answer on June 11, 2026 at 1:01 am

    Avoiding Thread.Abort is always a good idea. Avoiding it on a thread you did not create is even better.

    Assuming if the hardware is not working, and you want the timeout, it does not matter if connectToHardware is left to timeout on its own and no error/exception details are wanted, then you can use the Task Parallel Library (TPL): System.Threading.Tasks.Task:

    // True => worked, False => timeout
    public static bool CallWithTimeout(Action method, TimeSpan timeout) {
      Exception e;
      Task worker = Task.Factory.StartNew(method)
                        .ContineueWith(t => {
                          // Ensure any exception is observed, is no-op if no exception.
                          // Using closure to help avoid this being optimised out.
                          e = t.Exception;
                        });
      return worker.Wait(timeout);
    }
    

    (If the passed Action could interact with a passed CancellationToken this could be made cleaner, allowing the underlying method to fail quickly on timeout.)

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

Sidebar

Related Questions

I have created an android application that calls (using kSOAP library) a SOAP based
I have created a function that shows/hides different messages according to a combination of
I have created simple example with @Singleton, @Schedule and @Timeout annotations to try if
I have created the following function for checking the connection status: private void checkConnectionStatus()
I have created a jquery slider. On clicking next or back buttons .animate function
All, I have the following code: $context = stream_context_create(array( 'http' => array( 'timeout' =>
I have created a QWidget with a bunch of QToolButtons in it and I
I have created 3 classes as following Ext.mine.TextParent - Inherting from Textfield Ext.mine.child.TextChildA -
I have created some JQuery that will expand a div 'popup' on hover and
I have created a JSP / servlets application running in Tomcat 7. It runs

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.