c#,VS2005中的定时器阵列 [英] Timer Array in c#, VS2005

查看:67
本文介绍了c#,VS2005中的定时器阵列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一组计时器(1-n)。起初我刚刚创建了

windows窗口定时器,但我读到系统定时器更适合

后台工作。计时器将监视不同的
目录并更新数据库。没有与GUI的交互。


问题是系统计时器没有标签属性所以我可以将b $ b绑在一个对象上。


示例(旧方式):


公共类TimerInfo

{

public string sPath;

public int iInterval;

}


private ArrayList arrTimers = new ArrayList();


private void CreateTimers(int NUMBER_TIMERS)

{

TimerInfo信息;

System.Timers.Timer newTimer;


for(int i = 1; i< = NUM​​BER_TIMERS; i ++)

{

Info = new TimerInfo();

Info.sPath =" c:\\" ;;

Info.iInterval = 60000;


newTimer = new System .Timers.Timer();

newTimer.Elapsed + = timerWatch_Tick;

newTimer.Interval = 20000;

newTimer.Tag = TimerInfo;

newTimer.Enabled = true;


arrTimers.Add(newTimer);

}

}


private void timerWatch_Tick(object sender,EventArgs e)

{

string sPath =((TimerInfo)( (计时器)发送者).Tag).sPath;

CheckForFile(sPath);

}

如果需要,我是否需要调用Dispose重置计时器阵列?


foreach(arrTimers中的System.Timers.Timer atimer)

{

atimer.Dispose( );

}

arrTimers.Clear();


感谢您的帮助。我正在通过C#作为新手挣扎甚至

虽然它是一种非常简洁的语言。


~吉娜〜

解决方案

目的观察者就是为了这个目的。您可以告诉它

观看某些文件,通配符和目录。然后,当发生某些事情时,你会收到回调



在你的帮助下查找文件系统观察器。

" Gina_Marano" < gi ******* @ gmail.com写信息

新闻:11 ********************** @ h48g2000cwc.googlegr oups.com ...


>我创建了一组计时器(1-n)。起初我刚刚创建了

windows窗口定时器,但我读到系统定时器更适合

后台工作。计时器将监视不同的
目录并更新数据库。没有与GUI的交互。


问题是系统计时器没有标签属性所以我可以将b $ b绑在一个对象上。


示例(旧方式):


公共类TimerInfo

{

public string sPath;

public int iInterval;

}


private ArrayList arrTimers = new ArrayList();


private void CreateTimers(int NUMBER_TIMERS)

{

TimerInfo信息;

System.Timers.Timer newTimer;


for(int i = 1; i< = NUM​​BER_TIMERS; i ++)

{

Info = new TimerInfo();

Info.sPath =" c:\\" ;;

Info.iInterval = 60000;


newTimer = new System .Timers.Timer();

newTimer.Elapsed + = timerWatch_Tick;

newTimer.Interval = 20000;

newTimer.Tag = TimerInfo;

newTimer.Enabled = true;


arrTime rs.Add(newTimer);

}

}


private void timerWatch_Tick(object sender,EventArgs e)

{

string sPath =((TimerInfo)((Timer)sender).Tag).sPath;

CheckForFile(sPath);

}


如果我想重置定时器阵列,是否需要调用Dispose?


foreach(System.Timers。 arrTimers中的定时器atimer)

{

atimer.Dispose();

}

arrTimers.Clear() ;


感谢您的帮助。我正在通过C#挣扎作为一个新手甚至

虽然它是一个非常简洁的语言。


~Gina~



除了那些目录观察者运气不好之外我什么都没有。一个原因可能是因为我正在看网络

目录,我怀疑我会抓住这些事件。


谢谢


~吉娜〜


10月4日下午4:33,EmeraldShield < emeraldshi ... @ noemail.noemail>

写道:


有目录观察者就是为此目的。您可以告诉它

观看某些文件,通配符和目录。然后,当有事情发生时,你会收到回调




在你的帮助下查找文件系统观察器。


" Gina_Marano" < ginals ... @ gmail.comwrote in messagenews:11 ********************** @ h48g2000cwc.g ooglegroups.com ...


我创建了一个定时器数组(1-n)。起初我刚刚创建了

windows窗口定时器,但我读到系统定时器更适合

后台工作。计时器将监视不同的
目录并更新数据库。没有与GUI的交互。


问题是系统计时器没有标签属性所以我可以在一个对象中绑定


示例(旧方式):


公共类TimerInfo

{

public string sPath;

public int iInterval;

}

< blockquote class =post_quotes>
private ArrayList arrTimers = new ArrayList();


private void CreateTimers(int NUMBER_TIMERS)

{

TimerInfo Info;

System.Timers.Timer newTimer;


for(int i = 1; i< = NUM​​BER_TIMERS; i ++)

{

Info = new TimerInfo();

Info.sPath =" c:\\" ;;

Info.iInterval = 60000;


newTimer = new System.Timers.Timer();

newTimer.Elapsed + = timerWatch_Tick;

newTimer.Interval = 20000;

newTimer.Tag = TimerInfo;

newTimer.Enabled = true;


arrTimers.Add(newTimer);

}

}


private void timerWatch_Tick(object sender,EventArgs e)

{

string sPath =((TimerInfo)( (计时器)发送者).Tag).sPath;

CheckForFile(sPath);

}


如果我想重置定时器阵列,是否需要调用Dispose?


foreach(arrTimers中的System.Timers.Timer atimer)

{

atimer。 Dispose();

}

arrTimers.Clear();


感谢您的帮助。我作为一个新手通过C#挣扎,即使它是一个非常简洁的语言。


~Gina~ - 隐藏引用的文字 - 显示引用的文字 -


我最好清楚地说明我的主要问题(除了可能代码审查)

是:


是否有一个技巧所以我可以分配系统计时器的一个对象因为它没有标签字段吗?


例子(使用表单计时器的旧方法)但是我需要


公共类TimerInfo

{

public string sPath;

public int iInterval;

}


private ArrayList arrTimers = new ArrayList();


private void CreateTimers(int NUMBER_TIMERS)

$

TimerInfo信息;

计时器newTimer;


for(int i = 1; i< = NUM​​BER_TIMERS; i ++)

{

Info = new TimerInfo();

Info.sPath =" c:\\" ;; < br $>
Info.iInterval = 60000;


newTime r = Timers.Timer();

newTimer.Tick + = timerWatch_Tick;

newTimer.Interval = 20000;

newTimer.Tag = TimerInfo ; //< - 这对于

系统计时器不存在

newTimer.Enabled = true;


arrTimers。添加(newTimer);

}

}


private void timerWatch_Tick(对象发送者,EventArgs e)

{

(TimerInfo)((计时器)发送者).Enabled = false;

尝试

{

string sPath =((TimerInfo)((Timer)sender).Tag).sPath;

CheckForFile(sPath);

}

终于

{

(TimerInfo)((计时器)发送者).Enabled = true;

}

}


~吉娜〜


I have created an array of timers (1-n). At first I just created
windows form timers but I read that system timers are better for
background work. The timers will just be monitoring different
directories and updating a database. No interaction with the GUI.

Problem is that the system timers do not have a tag property so I can
tie in an object.

example (old way):

public class TimerInfo
{
public string sPath;
public int iInterval;
}

private ArrayList arrTimers = new ArrayList();

private void CreateTimers(int NUMBER_TIMERS)
{
TimerInfo Info;
System.Timers.Timer newTimer;

for (int i = 1; i <= NUMBER_TIMERS; i++)
{
Info = new TimerInfo();
Info.sPath = "c:\\";
Info.iInterval = 60000;

newTimer = new System.Timers.Timer();
newTimer.Elapsed += timerWatch_Tick;
newTimer.Interval = 20000;
newTimer.Tag = TimerInfo;
newTimer.Enabled = true;

arrTimers.Add(newTimer);
}
}

private void timerWatch_Tick(object sender, EventArgs e)
{
string sPath = ((TimerInfo)((Timer)sender).Tag).sPath;
CheckForFile(sPath);
}
Do I need to call Dispose if I want to reset the timer array?

foreach (System.Timers.Timer atimer in arrTimers)
{
atimer.Dispose();
}
arrTimers.Clear();

Thanks for your help. I am struggling as a newbie through C# even
though it is a really neat language.

~Gina~

解决方案

There are directory watchers just for this purpose. You can tell it to
watch certain files, wildcards, and directories. You then get callbacks
when something happens.

Lookup File System Watcher in your help.
"Gina_Marano" <gi*******@gmail.comwrote in message
news:11**********************@h48g2000cwc.googlegr oups.com...

>I have created an array of timers (1-n). At first I just created
windows form timers but I read that system timers are better for
background work. The timers will just be monitoring different
directories and updating a database. No interaction with the GUI.

Problem is that the system timers do not have a tag property so I can
tie in an object.

example (old way):

public class TimerInfo
{
public string sPath;
public int iInterval;
}

private ArrayList arrTimers = new ArrayList();

private void CreateTimers(int NUMBER_TIMERS)
{
TimerInfo Info;
System.Timers.Timer newTimer;

for (int i = 1; i <= NUMBER_TIMERS; i++)
{
Info = new TimerInfo();
Info.sPath = "c:\\";
Info.iInterval = 60000;

newTimer = new System.Timers.Timer();
newTimer.Elapsed += timerWatch_Tick;
newTimer.Interval = 20000;
newTimer.Tag = TimerInfo;
newTimer.Enabled = true;

arrTimers.Add(newTimer);
}
}

private void timerWatch_Tick(object sender, EventArgs e)
{
string sPath = ((TimerInfo)((Timer)sender).Tag).sPath;
CheckForFile(sPath);
}
Do I need to call Dispose if I want to reset the timer array?

foreach (System.Timers.Timer atimer in arrTimers)
{
atimer.Dispose();
}
arrTimers.Clear();

Thanks for your help. I am struggling as a newbie through C# even
though it is a really neat language.

~Gina~



I have had nothing except bad luck with those directory watchers. One
reason that might be the reason is that I am watching network
directories and I doubt I will catch those events.

thanks though

~Gina~

On Oct 4, 4:33 pm, "EmeraldShield" <emeraldshi...@noemail.noemail>
wrote:

There are directory watchers just for this purpose. You can tell it to
watch certain files, wildcards, and directories. You then get callbacks
when something happens.

Lookup File System Watcher in your help.

"Gina_Marano" <ginals...@gmail.comwrote in messagenews:11**********************@h48g2000cwc.g ooglegroups.com...

I have created an array of timers (1-n). At first I just created
windows form timers but I read that system timers are better for
background work. The timers will just be monitoring different
directories and updating a database. No interaction with the GUI.

Problem is that the system timers do not have a tag property so I can
tie in an object.

example (old way):

public class TimerInfo
{
public string sPath;
public int iInterval;
}

private ArrayList arrTimers = new ArrayList();

private void CreateTimers(int NUMBER_TIMERS)
{
TimerInfo Info;
System.Timers.Timer newTimer;

for (int i = 1; i <= NUMBER_TIMERS; i++)
{
Info = new TimerInfo();
Info.sPath = "c:\\";
Info.iInterval = 60000;

newTimer = new System.Timers.Timer();
newTimer.Elapsed += timerWatch_Tick;
newTimer.Interval = 20000;
newTimer.Tag = TimerInfo;
newTimer.Enabled = true;

arrTimers.Add(newTimer);
}
}

private void timerWatch_Tick(object sender, EventArgs e)
{
string sPath = ((TimerInfo)((Timer)sender).Tag).sPath;
CheckForFile(sPath);
}

Do I need to call Dispose if I want to reset the timer array?

foreach (System.Timers.Timer atimer in arrTimers)
{
atimer.Dispose();
}
arrTimers.Clear();

Thanks for your help. I am struggling as a newbie through C# even
though it is a really neat language.

~Gina~- Hide quoted text -- Show quoted text -


I better clearly state my main question (besides maybe a code review)
is:

Is there a trick so I can assign an object to a system timer since it
does not have a tag field?

example (old way using form timer) but I need to

public class TimerInfo
{
public string sPath;
public int iInterval;
}

private ArrayList arrTimers = new ArrayList();

private void CreateTimers(int NUMBER_TIMERS)
{
TimerInfo Info;
Timer newTimer;

for (int i = 1; i <= NUMBER_TIMERS; i++)
{
Info = new TimerInfo();
Info.sPath = "c:\\";
Info.iInterval = 60000;

newTimer = Timers.Timer();
newTimer.Tick += timerWatch_Tick;
newTimer.Interval = 20000;
newTimer.Tag = TimerInfo; //<-- this does not exist for the
system timer
newTimer.Enabled = true;

arrTimers.Add(newTimer);
}
}

private void timerWatch_Tick(object sender, EventArgs e)
{
(TimerInfo)((Timer)sender).Enabled = false;
try
{
string sPath = ((TimerInfo)((Timer)sender).Tag).sPath;
CheckForFile(sPath);
}
finally
{
(TimerInfo)((Timer)sender).Enabled = true;
}
}

~Gina~


这篇关于c#,VS2005中的定时器阵列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆