ted*_*ski 5 c# asp.net static timer
我试图使用Timer
触发事件来通过网络发送数据.我创建了一个简单的类来进行调试.基本上我有一个List<string>
我想发送.我希望发生以下情况:
List
Timer
10秒钟List
之前添加第二个字符串Timer.Elapsed
Timer
10秒钟.到目前为止我有这个:
public static List<string> list;
public static Timer timer;
public static bool isWiredUp = false;
public static void Log(string value) {
if (list == null) list = new List<string>();
list.Add(value);
//this does not reset the timer, elapsed still happens 10s after #1
if (timer != null) {
timer = null;
}
timer = new Timer(10000);
timer.Start();
timer.Enabled = true;
timer.AutoReset = false;
if (!isWiredUp) {
timer.Elapsed += new ElapsedEventHandler(SendToServer);
isWiredUp = true;
}
}
static void SendToServer(object sender, ElapsedEventArgs e) {
timer.Enabled = false;
timer.Stop();
}
Run Code Online (Sandbox Code Playgroud)
有任何想法吗?
Ser*_*rvy 11
您可以使用Stop
函数后面的Start
函数来"重启"计时器.使用它可以创建Timer
第一次创建类时,在那时连接Elapsed事件,然后在添加项时调用这两种方法.它将启动或重新启动计时器.请注意,调用Stop
尚未启动的计时器不会执行任何操作,它不会引发异常或导致任何其他问题.
public class Foo
{
public static List<string> list;
public static Timer timer;
static Foo()
{
list = new List<string>();
timer = new Timer(10000);
timer.Enabled = true;
timer.AutoReset = false;
timer.Elapsed += SendToServer;
}
public static void Log(string value)
{
list.Add(value);
timer.Stop();
timer.Start();
}
static void SendToServer(object sender, ElapsedEventArgs e)
{
//TODO send data to server
//AutoReset is false, so neither of these are needed
//timer.Enabled = false;
//timer.Stop();
}
}
Run Code Online (Sandbox Code Playgroud)
请注意,List
您可能不希望使用它而非使用它BlockingCollection<string>
.这有几个优点.首先,Log
如果从多个线程同时调用,这些方法将起作用; 因为多个并发日志可能会破坏列表.这也意味着SendToServer
可以在添加新项目的同时将项目从队列中取出.如果您使用a List
,则需要对lock
列表进行所有访问(这可能不是问题,但不是那么简单).
归档时间: |
|
查看次数: |
20417 次 |
最近记录: |