Skip to content

Class TaskWithTimeout ​

Namespace: Tool.Utils.TaskHelper
Assembly: Tool.Net.dll

允许自定义触发异步任务

csharp
public class TaskWithTimeout : TaskCompletionSource, IDisposable

Inheritance ​

object ← TaskCompletionSource ← TaskWithTimeout

Implements ​

IDisposable

Inherited Members ​

TaskCompletionSource.SetCanceled(), TaskCompletionSource.SetCanceled(CancellationToken), TaskCompletionSource.SetException(IEnumerable<Exception>), TaskCompletionSource.SetException(Exception), TaskCompletionSource.SetResult(), TaskCompletionSource.TrySetCanceled(), TaskCompletionSource.TrySetCanceled(CancellationToken), TaskCompletionSource.TrySetException(IEnumerable<Exception>), TaskCompletionSource.TrySetException(Exception), TaskCompletionSource.TrySetResult(), TaskCompletionSource.Task, object.Equals(object?), object.Equals(object?, object?), object.GetHashCode(), object.GetType(), object.MemberwiseClone(), object.ReferenceEquals(object?, object?), object.ToString()

Extension Methods ​

ObjectExtension.Add<T>(object, object, object), ObjectExtension.CopyEntity(object, object, params string[]), ObjectExtension.EntityToJson(object), ObjectExtension.EntityToJson(object, bool), ObjectExtension.EntityToJson(object, bool, string), DictionaryExtension.GetDictionary(object), TypeExtension.GetFieldKey(object, string, out bool), TypeExtension.GetFieldKey<T>(object, string, out bool), TypeExtension.GetFieldKey(object, Type, string, out bool), ObjectExtension.GetIntPtr(object), ObjectExtension.GetIntPtrInt(object), TypeExtension.GetPropertieFind(object, string, bool), TypeExtension.GetProperties(object), TypeExtension.GetPropertyKey(object, string, out bool), TypeExtension.GetPropertyKey<T>(object, string, out bool), TypeExtension.GetPropertyKey(object, Type, string, out bool), TypeExtension.GetValue(object, PropertyDescriptor), TypeExtension.GetValue(object, string, bool), TypeExtension.GetValue(object, string), ObjectExtension.Read<T>(object, object, int, int), ObjectExtension.Read<T>(object, int, object, int, int), DictionaryExtension.SetDictionary(object, IDictionary<string, object>), TypeExtension.SetFieldKey(object, string, object), TypeExtension.SetFieldKey<T>(object, string, object), TypeExtension.SetFieldKey(object, Type, string, object), TypeExtension.SetPropertyKey(object, string, object), TypeExtension.SetPropertyKey<T>(object, string, object), TypeExtension.SetPropertyKey(object, Type, string, object), TypeExtension.SetValue(object, PropertyDescriptor, object), TypeExtension.SetValue(object, string, object, bool), TypeExtension.SetValue(object, string, object), ObjectExtension.ToBase64String(object), ObjectExtension.ToBytes(object), ObjectExtension.ToBytes(object, out Type), DictionaryExtension.ToDictionary(object), DictionaryExtension.ToDictionary<T>(object), DictionaryExtension.ToIDictionary(object), DictionaryExtension.ToIDictionary<T>(object), ObjectExtension.ToJson(object), ObjectExtension.ToJson(object, JsonSerializerOptions), ObjectExtension.ToJsonWeb(object), ObjectExtension.ToJsonWeb(object, Action<JsonSerializerOptions>), ObjectExtension.ToTryVar<T>(object, T), ObjectExtension.ToVar<T>(object), ObjectExtension.ToVar(object, Type, bool), ObjectExtension.ToVar(object, string), ObjectExtension.ToXml(object)

Remarks ​

代码由逆血提供支持

Constructors ​

TaskWithTimeout(TimeSpan) ​

初始化

csharp
public TaskWithTimeout(TimeSpan timeout)

Parameters ​

timeout TimeSpan

超时时间

TaskWithTimeout(TimeSpan, object) ​

初始化

csharp
public TaskWithTimeout(TimeSpan timeout, object state)

Parameters ​

timeout TimeSpan

超时时间

state object

带传递值

TaskWithTimeout(TimeSpan, TaskCreationOptions) ​

初始化

csharp
public TaskWithTimeout(TimeSpan timeout, TaskCreationOptions creationOptions)

Parameters ​

timeout TimeSpan

超时时间

creationOptions TaskCreationOptions

任务枚举类型

TaskWithTimeout(TimeSpan, object, TaskCreationOptions) ​

初始化

csharp
public TaskWithTimeout(TimeSpan timeout, object state, TaskCreationOptions creationOptions)

Parameters ​

timeout TimeSpan

超时时间

state object

带传递值

creationOptions TaskCreationOptions

任务枚举类型

Properties ​

IsCancellationRequested ​

是否被取消

csharp
public bool IsCancellationRequested { get; }

Property Value ​

bool

Timeout ​

超时时间

csharp
public TimeSpan Timeout { get; }

Property Value ​

TimeSpan

Methods ​

Dispose() ​

回收资源

csharp
public void Dispose()

GetAwaiter() ​

返回任务调度器

csharp
public TaskAwaiter GetAwaiter()

Returns ​

TaskAwaiter

RunWithTimeout<T>(Func<Task<T>>, TimeSpan) ​

公共任务,执行时允许设置超时时间

csharp
public static Task<T> RunWithTimeout<T>(Func<Task<T>> taskFactory, TimeSpan timeout)

Parameters ​

taskFactory Func<Task<T>>

任务

timeout TimeSpan

超时时间

Returns ​

Task<T>

任务

Type Parameters ​

T

返回泛型

Exceptions ​

TimeoutException

超时错误

RunWithTimeout<T>(Func<CancellationToken, Task<T>>, TimeSpan) ​

公共任务,执行时允许设置超时时间(带取消模式的)

csharp
public static Task<T> RunWithTimeout<T>(Func<CancellationToken, Task<T>> taskFactory, TimeSpan timeout)

Parameters ​

taskFactory Func<CancellationToken, Task<T>>

任务

timeout TimeSpan

超时时间

Returns ​

Task<T>

任务

Type Parameters ​

T

返回泛型

Exceptions ​

TimeoutException

超时错误

基于Apache-2.0协议开源