点击或拖拽改变大小

SoftIncrementCount 类

一个简单的不持久化的序号自增类,采用线程安全实现,并允许指定最大数字,将包含该最大值,到达后清空从指定数开始
A simple non-persistent serial number auto-increment class, which is implemented with thread safety, and allows the maximum number to be specified, which will contain the maximum number, and will be cleared from the specified number upon arrival.
继承层次
SystemObject
  HslCommunication.BasicFrameworkSoftIncrementCount

命名空间:  HslCommunication.BasicFramework
程序集:  HslCommunication (在 HslCommunication.dll 中) 版本:11.8.2.0 (11.8.2.0)
语法
public sealed class SoftIncrementCount : IDisposable

SoftIncrementCount 类型公开以下成员。

构造函数
  名称说明
公共方法SoftIncrementCount
实例化一个自增信息的对象,包括最大值,初始值,增量值
Instantiate an object with incremental information, including the maximum value and initial value, IncreaseTick
Top
属性
  名称说明
公共属性IncreaseTick
增加的单元,如果设置为0,就是不增加。如果为小于0,那就是减少,会变成负数的可能。
Increased units, if set to 0, do not increase. If it is less than 0, it is a decrease and it may become a negative number.
公共属性MaxValue
获取当前的计数器的最大的设置值。
Get the maximum setting value of the current counter.
Top
方法
  名称说明
公共方法Dispose
释放被 SoftIncrementCount 使用的所有资源
公共方法Equals (继承自 Object。)
公共方法GetCurrentValue
获取自增信息,获得数据之后,下一次获取将会自增,如果自增后大于最大值,则会重置为最小值,如果小于最小值,则会重置为最大值。
Get the auto-increment information. After getting the data, the next acquisition will auto-increase. If the auto-increment is greater than the maximum value, it will reset to the minimum value. If the auto-increment is smaller than the minimum value, it will reset to the maximum value.
公共方法GetHashCode (继承自 Object。)
公共方法GetType (继承自 Object。)
公共方法ResetCurrentValue
将当前的值重置为初始值。
Reset the current value to the initial value.
公共方法ResetCurrentValue(Int64)
将当前的值重置为指定值,该值不能大于max,如果大于max值,就会自动设置为max
Reset the current value to the specified value. The value cannot be greater than max. If it is greater than max, it will be automatically set to max.
公共方法ResetMaxValue
重置当前序号的最大值,最大值应该大于初始值,如果当前值大于最大值,则当前值被重置为最大值
Reset the maximum value of the current serial number. The maximum value should be greater than the initial value. If the current value is greater than the maximum value, the current value is reset to the maximum value.
公共方法ResetStartValue
重置当前序号的初始值,需要小于最大值,如果当前值小于初始值,则当前值被重置为初始值。
To reset the initial value of the current serial number, it must be less than the maximum value. If the current value is less than the initial value, the current value is reset to the initial value.
公共方法ToString (重写 ObjectToString.)
Top
扩展方法
  名称说明
公共扩展器方法ToJsonString
获取当前对象的JSON格式表示的字符串。
Gets the string represented by the JSON format of the current object.
(由 HslExtension 定义。)
Top
示例
先来看看一个简单的应用的
简单示例
// 举例一个从0-65535的例子
SoftIncrementCount softIncrement = new SoftIncrementCount( ushort.MaxValue );

long call1 = softIncrement.GetCurrentValue( );     // 获得0,下次调用为1
long call2 = softIncrement.GetCurrentValue( );     // 获得1,下次调用为2
long call3 = softIncrement.GetCurrentValue( );     // 获得2,下次调用为3
                                                   // .....
long call65536 = softIncrement.GetCurrentValue( ); // 获得65535,下次调用为0
long call65537 = softIncrement.GetCurrentValue( ); // 获得0,下次调用为1
                                                   // 如此循环

// 如果需要反向计数的话
softIncrement = new SoftIncrementCount( ushort.MaxValue, 0, -1 );
softIncrement.ResetCurrentValue( ushort.MaxValue );
// 在获取的话就是  65535,65534,65533,......3,2,1,0
再来看看一些复杂的情况
复杂示例
// 举例一个从1000-2000的例子
SoftIncrementCount softIncrement = new SoftIncrementCount( 2000, 1000 );

long call1 = softIncrement.GetCurrentValue( );     // 获得1000,下次调用为1001
long call2 = softIncrement.GetCurrentValue( );     // 获得1001,下次调用为1002
long call3 = softIncrement.GetCurrentValue( );     // 获得1002,下次调用为1003
                                                   // .....
long call2000 = softIncrement.GetCurrentValue( );  // 获得1999,下次调用为2000
long call2001 = softIncrement.GetCurrentValue( );  // 获得2000,下次调用为1000
long call2002 = softIncrement.GetCurrentValue( );  // 获得1000,下次调用为1000
                                                   // 如此循环

// 如果需要反向计数的话
softIncrement = new SoftIncrementCount( 2000, 1000, -1 );
softIncrement.ResetCurrentValue( 2000 );
// 在获取的话就是  2000,1999,1998,......3,2,1,0
其他一些特殊的设定
其他示例
// 其他的例子说明
SoftIncrementCount softIncrement = new SoftIncrementCount( 2000, 1000 );
// 如果我们需要一直保持消息号在0,不需要增长的计数
softIncrement.IncreaseTick = 0;

// 同理,我们需要消息号保持在1000
softIncrement.ResetCurrentValue( 1000 );
softIncrement.IncreaseTick = 0;

// 如果消息号需要为1,3,5,7,9,......999
softIncrement.ResetStartValue( 1 );
softIncrement.ResetMaxValue( 999 );
softIncrement.IncreaseTick = 2;

// 同理的操作,从999,997,995.....3,1
softIncrement = new SoftIncrementCount( 999, 1 );
softIncrement.ResetCurrentValue( 999 );
softIncrement.IncreaseTick = -2;
参见