You can use the Task Parallel Library. To be more exact, you can use Task.Wait(TimeSpan)
:
using System.Threading.Tasks;
var task = Task.Run(() => SomeMethod(input));
if (task.Wait(TimeSpan.FromSeconds(10)))
return task.Result;
else
throw new Exception("Timed out");
More Related Contents:
- How to add a Timeout to Console.ReadLine()?
- Timeout expired. The timeout period elapsed prior to completion of the operation or the server is not responding. The statement has been terminated
- How to configure socket connect timeout
- Implement C# Generic Timeout
- Adjusting HttpWebRequest Connection Timeout in C#
- Timeouts with long running ASP.NET MVC Core Controller HTTPPost Method
- 5.7.57 SMTP – Client was not authenticated to send anonymous mail during MAIL FROM error
- Set timeout to an operation
- The operation has timed out at System.Net.HttpWebRequest.GetResponse() while sending large number of requests to a host
- How can I tell when HttpClient has timed out?
- Is there a Task based replacement for System.Threading.Timer?
- C# Stream.Read with timeout
- Implementing a timeout on a function returning a value
- C# Singleton Form pattern, how to run 2 and more forms? [closed]
- Download binary file over HTTP
- Retrieving Property name from lambda expression
- How to get the type of T from a member of a generic class or method
- ProcessStartInfo hanging on “WaitForExit”? Why?
- Set global hotkeys using C#
- When to use BlockingCollection and when ConcurrentBag instead of List?
- Save byte[] into a SQL Server database from C#
- Convert DataTable to generic List?
- System.UnauthorizedAccessException: Retrieving the COM class factory for Word Interop fails with error 80070005
- Best Practice for Exception Handling in a Windows Forms Application?
- Register a dll into the GAC – but then it doesn’t show up in the assembly window
- SQL Server Database Change Listener C#
- Is it ok to await the same task from multiple threads – is await thread safe?
- Getting nested properties with System.Text.Json
- How to make correct clone of the List? [duplicate]
- How do I convert a decimal to an int in C#?