C# 通过 timer.Elapsed 和点击调用方法
C# Invoking method by timer.Elapsed and by click
我的定时器有问题。
这是我的代码:
_uploadHelper = new DataProcessorUploadHelper((sender, args) => Start())
public void Start()
{
Task.Factory.StartNew(() =>
{
if (UploadHelper.UploadState == UploadState.UploadOn)
{
UploadHelper.IncrementSavingRequestCounter();
}
else
{
UploadHelper.StopTimer();
lock (_lock)
{
UploadHelper.UploadState = UploadState.UploadOn;
UploadData();
UploadHelper.UploadState = UploadState.UploadOff;
}
UploadHelper.StartTimer();
}
});
}
UploadState是这样实现的
private int _uploadState;
public UploadState UploadState
{
get { return (UploadState)_uploadState; }
set { Interlocked.Exchange(ref _uploadState, (int)value); }
}
我正在使用 System.Timers.Timer,它应该以 10 分钟的间隔调用 Start 方法(在我的助手 class 中实现)。
private Timer _intervalTimer;
private long _savingRequestsCounter;
public DataProcessorUploadHelper(ElapsedEventHandler callback)
{
UploadState = UploadState.UploadOff;;
_intervalTimer = new Timer();
_intervalTimer.Interval = UploadIntervalTimeSpan.TotalMilliseconds;
_intervalTimer.Elapsed += callback;
_intervalTimer.Start();
_intervalTimer.Enabled = true;
}
public void StartTimer()
{
_intervalTimer.Start();
}
public void ResetTimer()
{
_intervalTimer.Start();
_intervalTimer.Stop();
}
public void StopTimer()
{
_intervalTimer.Stop();
}
public void IncrementSavingRequestCounter()
{
Interlocked.Increment(ref _savingRequestsCounter);
}
public void DecrementSavingRequestCounter()
{
Interlocked.Decrement(ref _savingRequestsCounter);
}
当用户点击特定按钮时也会调用此方法。
还有我的麻烦。
定时器调用 Start 方法 => UploadHelper.UploadState = UploadState.UploadOn.
而且应该被_lock锁定了。
但是当用户在 UploadData() 期间点击
我看到 UploadState = UploadState.UploadOff
并且未锁定。
这段代码有什么问题?
为什么定时器调用这个方法时状态不一样?
"...Moreover should be locked by _lock"
是的,如果 _lock 在您的情况下被声明为 static。
由于您的计时器的 Elapsed-Event 导致在单独的线程中调用您的 UploadData(),因此 _lock(如果不是静态的)每次都不是相同的 _lock 并且不用作同步对象。
- 如果您的计时器来自 System.Timers,那么还有另一种同步方式:
见 SynchronizingObject
我的定时器有问题。
这是我的代码:
_uploadHelper = new DataProcessorUploadHelper((sender, args) => Start())
public void Start()
{
Task.Factory.StartNew(() =>
{
if (UploadHelper.UploadState == UploadState.UploadOn)
{
UploadHelper.IncrementSavingRequestCounter();
}
else
{
UploadHelper.StopTimer();
lock (_lock)
{
UploadHelper.UploadState = UploadState.UploadOn;
UploadData();
UploadHelper.UploadState = UploadState.UploadOff;
}
UploadHelper.StartTimer();
}
});
}
UploadState是这样实现的
private int _uploadState;
public UploadState UploadState
{
get { return (UploadState)_uploadState; }
set { Interlocked.Exchange(ref _uploadState, (int)value); }
}
我正在使用 System.Timers.Timer,它应该以 10 分钟的间隔调用 Start 方法(在我的助手 class 中实现)。
private Timer _intervalTimer;
private long _savingRequestsCounter;
public DataProcessorUploadHelper(ElapsedEventHandler callback)
{
UploadState = UploadState.UploadOff;;
_intervalTimer = new Timer();
_intervalTimer.Interval = UploadIntervalTimeSpan.TotalMilliseconds;
_intervalTimer.Elapsed += callback;
_intervalTimer.Start();
_intervalTimer.Enabled = true;
}
public void StartTimer()
{
_intervalTimer.Start();
}
public void ResetTimer()
{
_intervalTimer.Start();
_intervalTimer.Stop();
}
public void StopTimer()
{
_intervalTimer.Stop();
}
public void IncrementSavingRequestCounter()
{
Interlocked.Increment(ref _savingRequestsCounter);
}
public void DecrementSavingRequestCounter()
{
Interlocked.Decrement(ref _savingRequestsCounter);
}
当用户点击特定按钮时也会调用此方法。
还有我的麻烦。
定时器调用 Start 方法 => UploadHelper.UploadState = UploadState.UploadOn.
而且应该被_lock锁定了。
但是当用户在 UploadData() 期间点击 我看到 UploadState = UploadState.UploadOff 并且未锁定。
这段代码有什么问题?
为什么定时器调用这个方法时状态不一样?
"...Moreover should be locked by _lock"
是的,如果 _lock 在您的情况下被声明为 static。
由于您的计时器的 Elapsed-Event 导致在单独的线程中调用您的 UploadData(),因此 _lock(如果不是静态的)每次都不是相同的 _lock 并且不用作同步对象。
- 如果您的计时器来自 System.Timers,那么还有另一种同步方式: 见 SynchronizingObject