mirror of
https://github.com/ElectronNET/Electron.NET.git
synced 2026-11-16 09:21:01 +00:00
Introduce TimeSpan extensions
This commit is contained in:
@@ -31,7 +31,7 @@ namespace ElectronNET.API
|
||||
CamelCase,
|
||||
}
|
||||
|
||||
private const int InvocationTimeout = 1000;
|
||||
private static readonly TimeSpan InvocationTimeout = 1000.ms();
|
||||
|
||||
private readonly string objectName;
|
||||
private readonly ConcurrentDictionary<string, Invocator> invocators;
|
||||
@@ -120,7 +120,7 @@ namespace ElectronNET.API
|
||||
return this.InvokeAsyncWithTimeout<T>(InvocationTimeout, arg, callerName);
|
||||
}
|
||||
|
||||
protected Task<T> InvokeAsyncWithTimeout<T>(int invocationTimeout, object arg = null, [CallerMemberName] string callerName = null)
|
||||
protected Task<T> InvokeAsyncWithTimeout<T>(TimeSpan invocationTimeout, object arg = null, [CallerMemberName] string callerName = null)
|
||||
{
|
||||
Debug.Assert(callerName != null, nameof(callerName) + " != null");
|
||||
|
||||
@@ -245,7 +245,7 @@ namespace ElectronNET.API
|
||||
private readonly Task<T> tcsTask;
|
||||
private TaskCompletionSource<T> tcs;
|
||||
|
||||
public Invocator(ApiBase apiBase, string callerName, int timeoutMs, object arg = null)
|
||||
public Invocator(ApiBase apiBase, string callerName, TimeSpan timeout, object arg = null)
|
||||
{
|
||||
this.tcs = new TaskCompletionSource<T>(TaskCreationOptions.RunContinuationsAsynchronously);
|
||||
this.tcsTask = this.tcs.Task;
|
||||
@@ -306,7 +306,7 @@ namespace ElectronNET.API
|
||||
_ = apiBase.Id >= 0 ? BridgeConnector.Socket.Emit(messageName, apiBase.Id) : BridgeConnector.Socket.Emit(messageName);
|
||||
}
|
||||
|
||||
System.Threading.Tasks.Task.Delay(timeoutMs).ContinueWith(_ =>
|
||||
System.Threading.Tasks.Task.Delay(timeout).ContinueWith(_ =>
|
||||
{
|
||||
if (this.tcs != null)
|
||||
{
|
||||
@@ -314,7 +314,7 @@ namespace ElectronNET.API
|
||||
{
|
||||
if (this.tcs != null)
|
||||
{
|
||||
var ex = new TimeoutException($"No response after {timeoutMs:D}ms trying to retrieve value {apiBase.objectName}.{callerName}()");
|
||||
var ex = new TimeoutException($"No response after {timeout:D}ms trying to retrieve value {apiBase.objectName}.{callerName}()");
|
||||
this.tcs.TrySetException(ex);
|
||||
this.tcs = null;
|
||||
}
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
using ElectronNET.API.Entities;
|
||||
using System;
|
||||
using System.Threading.Tasks;
|
||||
using ElectronNET.API.Entities;
|
||||
using ElectronNET.Common;
|
||||
|
||||
// ReSharper disable InconsistentNaming
|
||||
|
||||
@@ -173,7 +174,7 @@ public class WebContents : ApiBase
|
||||
/// Get system printers.
|
||||
/// </summary>
|
||||
/// <returns>printers</returns>
|
||||
public Task<PrinterInfo[]> GetPrintersAsync() => this.InvokeAsyncWithTimeout<PrinterInfo[]>(8_000);
|
||||
public Task<PrinterInfo[]> GetPrintersAsync() => this.InvokeAsyncWithTimeout<PrinterInfo[]>(8.seconds());
|
||||
|
||||
/// <summary>
|
||||
/// Prints window's web page.
|
||||
@@ -388,7 +389,7 @@ public class WebContents : ApiBase
|
||||
/// Returns string - The user agent for this web page.
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
public Task<string> GetUserAgentAsync() => InvokeAsyncWithTimeout<string>(3000);
|
||||
public Task<string> GetUserAgentAsync() => InvokeAsyncWithTimeout<string>(3.seconds());
|
||||
|
||||
/// <summary>
|
||||
/// Overrides the user agent for this web page.
|
||||
|
||||
74
src/ElectronNET.API/Common/TimeSpanExtensions.cs
Normal file
74
src/ElectronNET.API/Common/TimeSpanExtensions.cs
Normal file
@@ -0,0 +1,74 @@
|
||||
// <copyright file="TimeSpanExtensions.cs" company="Emby LLC">
|
||||
// Copyright © Emby LLC. All rights reserved.
|
||||
// </copyright>
|
||||
|
||||
namespace ElectronNET.Common
|
||||
{
|
||||
using System;
|
||||
using System.Diagnostics.CodeAnalysis;
|
||||
|
||||
/// <summary>
|
||||
/// The TimeSpanExtensions class.
|
||||
/// </summary>
|
||||
[SuppressMessage("StyleCop.CSharp.NamingRules", "SA1300:Element should begin with upper-case letter", Justification = "OK")]
|
||||
[SuppressMessage("StyleCop.CSharp.DocumentationRules", "SA1600:Elements should be documented", Justification = "OK")]
|
||||
[SuppressMessage("ReSharper", "StyleCop.SA1300", Justification = "OK")]
|
||||
[SuppressMessage("ReSharper", "InconsistentNaming", Justification = "OK")]
|
||||
internal static class TimeSpanExtensions
|
||||
{
|
||||
public static TimeSpan ms(this int value)
|
||||
{
|
||||
return TimeSpan.FromMilliseconds(value);
|
||||
}
|
||||
|
||||
public static TimeSpan ms(this long value)
|
||||
{
|
||||
return TimeSpan.FromMilliseconds(value);
|
||||
}
|
||||
|
||||
public static TimeSpan seconds(this int value)
|
||||
{
|
||||
return TimeSpan.FromSeconds(value);
|
||||
}
|
||||
|
||||
public static TimeSpan minutes(this int value)
|
||||
{
|
||||
return TimeSpan.FromMinutes(value);
|
||||
}
|
||||
|
||||
public static TimeSpan hours(this int value)
|
||||
{
|
||||
return TimeSpan.FromHours(value);
|
||||
}
|
||||
|
||||
public static TimeSpan days(this int value)
|
||||
{
|
||||
return TimeSpan.FromDays(value);
|
||||
}
|
||||
|
||||
public static TimeSpan ms(this double value)
|
||||
{
|
||||
return TimeSpan.FromMilliseconds(value);
|
||||
}
|
||||
|
||||
public static TimeSpan seconds(this double value)
|
||||
{
|
||||
return TimeSpan.FromSeconds(value);
|
||||
}
|
||||
|
||||
public static TimeSpan minutes(this double value)
|
||||
{
|
||||
return TimeSpan.FromMinutes(value);
|
||||
}
|
||||
|
||||
public static TimeSpan hours(this double value)
|
||||
{
|
||||
return TimeSpan.FromHours(value);
|
||||
}
|
||||
|
||||
public static TimeSpan days(this double value)
|
||||
{
|
||||
return TimeSpan.FromDays(value);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -35,5 +35,6 @@
|
||||
<ItemGroup>
|
||||
<InternalsVisibleTo Include="ElectronNET.AspNet" />
|
||||
<InternalsVisibleTo Include="ElectronNET.Core.AspNet" />
|
||||
<InternalsVisibleTo Include="ElectronNET.IntegrationTests" />
|
||||
</ItemGroup>
|
||||
</Project>
|
||||
|
||||
@@ -76,7 +76,7 @@
|
||||
{
|
||||
try
|
||||
{
|
||||
await Task.Delay(10).ConfigureAwait(false);
|
||||
await Task.Delay(10.ms()).ConfigureAwait(false);
|
||||
|
||||
Console.Error.WriteLine("[StartInternal]: startCmd: {0}", startCmd);
|
||||
Console.Error.WriteLine("[StartInternal]: args: {0}", args);
|
||||
@@ -85,7 +85,7 @@
|
||||
this.process.ProcessExited += this.Process_Exited;
|
||||
this.process.Run(startCmd, args, directoriy);
|
||||
|
||||
await Task.Delay(500).ConfigureAwait(false);
|
||||
await Task.Delay(500.ms()).ConfigureAwait(false);
|
||||
|
||||
Console.Error.WriteLine("[StartInternal]: after run:");
|
||||
|
||||
|
||||
Reference in New Issue
Block a user