2017-04-10 Update 2 Package
This commit is contained in:
@@ -16,6 +16,7 @@
|
||||
<RepositoryUrl>https://github.com/parallelbgls/Modbus.Net</RepositoryUrl>
|
||||
<RepositoryType>git</RepositoryType>
|
||||
<PackageTags>hardware communicate protocal modbus Delian</PackageTags>
|
||||
<GeneratePackageOnBuild>True</GeneratePackageOnBuild>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
|
||||
@@ -15,6 +15,8 @@
|
||||
<RepositoryUrl>https://github.com/parallelbgls/Modbus.Net</RepositoryUrl>
|
||||
<RepositoryType>git</RepositoryType>
|
||||
<PackageTags>hardware communicate protocal OPC DA Delian</PackageTags>
|
||||
<PackageRequireLicenseAcceptance>False</PackageRequireLicenseAcceptance>
|
||||
<GeneratePackageOnBuild>True</GeneratePackageOnBuild>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
|
||||
@@ -15,6 +15,7 @@
|
||||
<RepositoryUrl>https://github.com/parallelbgls/Modbus.Net</RepositoryUrl>
|
||||
<RepositoryType>git</RepositoryType>
|
||||
<PackageTags>hardware communicate protocal Siemens profinet Delian</PackageTags>
|
||||
<GeneratePackageOnBuild>True</GeneratePackageOnBuild>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
|
||||
@@ -16,6 +16,8 @@
|
||||
<Copyright>Copyright 2017 Hangzhou Delian IoT Science Technology Co.,Ltd.</Copyright>
|
||||
<PackageTags>hardware communicate protocal Delian</PackageTags>
|
||||
<RepositoryType>git</RepositoryType>
|
||||
<PackageRequireLicenseAcceptance>False</PackageRequireLicenseAcceptance>
|
||||
<GeneratePackageOnBuild>True</GeneratePackageOnBuild>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
|
||||
@@ -16,6 +16,7 @@
|
||||
<Copyright>Copyright 2017 Hangzhou Delian IoT Science Technology Co.,Ltd.</Copyright>
|
||||
<PackageTags>hardware communicate protocal Delian</PackageTags>
|
||||
<RepositoryType>git</RepositoryType>
|
||||
<GeneratePackageOnBuild>True</GeneratePackageOnBuild>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
|
||||
@@ -1,15 +1,12 @@
|
||||
/* AsyncHelper 注释
|
||||
* -- AsyncHelper来自于AsyncEx,为了引用方便直接拷贝了代码。Modbus.Net的作者不保留对AsyncHeloper类的版权。
|
||||
* -- AsyncHelper copied from AsyncEx. The author of "Modbus.Net" <b>donnot</b> obtain the copyright of AsyncHelper(Only).
|
||||
*/
|
||||
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace Modbus.Net
|
||||
{
|
||||
/// <summary>
|
||||
/// AsyncHelper Class
|
||||
/// </summary>
|
||||
public static class AsyncHelper
|
||||
{
|
||||
private static readonly TaskFactory _myTaskFactory = new
|
||||
@@ -64,106 +61,4 @@ namespace Modbus.Net
|
||||
return task.ContinueWith(t => t.GetAwaiter().GetResult(), token);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// AsyncLock locks across one or several await calls.
|
||||
/// </summary>
|
||||
public class AsyncLock
|
||||
{
|
||||
private readonly Task<Releaser> _releaser;
|
||||
private readonly AsyncSemaphore _semaphore;
|
||||
|
||||
public AsyncLock()
|
||||
{
|
||||
_semaphore = new AsyncSemaphore(1);
|
||||
_releaser = Task.FromResult(new Releaser(this));
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Lock the async method. Call like: using (await asynclock.LockAsync())
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
public Task<Releaser> LockAsync()
|
||||
{
|
||||
var wait = _semaphore.WaitAsync();
|
||||
return wait.IsCompleted
|
||||
? _releaser
|
||||
: wait.ContinueWith((_, state) => new Releaser((AsyncLock) state),
|
||||
this, CancellationToken.None,
|
||||
TaskContinuationOptions.ExecuteSynchronously, TaskScheduler.Default);
|
||||
}
|
||||
|
||||
|
||||
public struct Releaser : IDisposable
|
||||
{
|
||||
private readonly AsyncLock _toRelease;
|
||||
|
||||
internal Releaser(AsyncLock toRelease)
|
||||
{
|
||||
_toRelease = toRelease;
|
||||
}
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
if (_toRelease != null)
|
||||
{
|
||||
_toRelease._semaphore.Release();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// AsyncSemaphore semaphore the multi run tasks.
|
||||
/// </summary>
|
||||
public class AsyncSemaphore
|
||||
{
|
||||
private static readonly Task _completed = Task.FromResult(true);
|
||||
private readonly Queue<TaskCompletionSource<bool>> _waiters = new Queue<TaskCompletionSource<bool>>();
|
||||
private int _currentCount;
|
||||
|
||||
public AsyncSemaphore(int initialCount)
|
||||
{
|
||||
if (initialCount < 0)
|
||||
{
|
||||
throw new ArgumentOutOfRangeException("initialCount");
|
||||
}
|
||||
_currentCount = initialCount;
|
||||
}
|
||||
|
||||
public Task WaitAsync()
|
||||
{
|
||||
lock (_waiters)
|
||||
{
|
||||
if (_currentCount > 0)
|
||||
{
|
||||
_currentCount--;
|
||||
return _completed;
|
||||
}
|
||||
var waiter = new TaskCompletionSource<bool>();
|
||||
_waiters.Enqueue(waiter);
|
||||
return waiter.Task;
|
||||
}
|
||||
}
|
||||
|
||||
public void Release()
|
||||
{
|
||||
TaskCompletionSource<bool> toRelease = null;
|
||||
lock (_waiters)
|
||||
{
|
||||
if (_waiters.Count > 0)
|
||||
{
|
||||
toRelease = _waiters.Dequeue();
|
||||
}
|
||||
else
|
||||
{
|
||||
_currentCount++;
|
||||
}
|
||||
}
|
||||
if (toRelease != null)
|
||||
{
|
||||
toRelease.SetResult(true);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user