包含带有计时器的对象的字典。需要找出哪个对象的计时器正在调用 elapsed 事件
我有这个 poll 类
class Poll
{
public string question { get; set; }
public Timer pollTimer { get; set; }
public List<string> userVoted { get; set; }
public Dictionary<string, int> choices { get; set; }
public bool PollRunning { get; set; }
public Poll(string question,Dictionary<string,int> choices)
{
this.question = question;
this.choices = choices;
this.pollTimer = new Timer(15000);
this.PollRunning = true;
this.userVoted = new List<string>();
}
public string pollResults()
{
string temp = "";
foreach (KeyValuePair<string, int> keyValuePair in choices)
{
temp = temp + keyValuePair.Key + " " + keyValuePair.Value + ", ";
}
return string.Format("Poll Results: {0}", temp);
}
}
,并且在 StartPool 方法中有此代码
static Dictionary<Channel, Poll> polls = new Dictionary<Channel, Poll>();
public void startPool(Channel channel)
{
polls.Add(channel, new Poll(question, tempdict));
polls[channel].pollTimer.Elapsed += new ElapsedEventHandler(pollTimer_Elapsed);
polls[channel].pollTimer.Start();
}
当调用此方法时,
static void pollTimer_Elapsed(object sender, ElapsedEventArgs e)
{
//do stuff to the poll that called this.
}
我需要知道哪个 poll 对象的计时器正在调用此方法 所以我可以做 polls[channel].pollTimer.Stop();并执行 polls[channel].pollResults();
事实上,我不知道运行时哪个轮询停止并发布结果,
如果这对您有帮助,我愿意发布整个解决方案。
i have this poll class
class Poll
{
public string question { get; set; }
public Timer pollTimer { get; set; }
public List<string> userVoted { get; set; }
public Dictionary<string, int> choices { get; set; }
public bool PollRunning { get; set; }
public Poll(string question,Dictionary<string,int> choices)
{
this.question = question;
this.choices = choices;
this.pollTimer = new Timer(15000);
this.PollRunning = true;
this.userVoted = new List<string>();
}
public string pollResults()
{
string temp = "";
foreach (KeyValuePair<string, int> keyValuePair in choices)
{
temp = temp + keyValuePair.Key + " " + keyValuePair.Value + ", ";
}
return string.Format("Poll Results: {0}", temp);
}
}
and I have this code in a StartPool method
static Dictionary<Channel, Poll> polls = new Dictionary<Channel, Poll>();
public void startPool(Channel channel)
{
polls.Add(channel, new Poll(question, tempdict));
polls[channel].pollTimer.Elapsed += new ElapsedEventHandler(pollTimer_Elapsed);
polls[channel].pollTimer.Start();
}
When this method gets called
static void pollTimer_Elapsed(object sender, ElapsedEventArgs e)
{
//do stuff to the poll that called this.
}
I need know what poll object's timer is calling this method
so I can do polls[channel].pollTimer.Stop(); and do polls[channel].pollResults();
As it is I have no idea which poll stop and post results for when this runs
i'm willing to post entire solution if that will help you help me.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您设计 Poll 类的方式的问题在于 Poll 类没有完全完成其工作。您需要其他类知道如何启动和停止轮询,这意味着一半的轮询实现在 Poll 类内部,一半的实现在 Poll 类外部。如果您要创建一个 Poll 类,请向其他人隐藏所有实现细节。
这就是我的意思。我将在 Poll 中创建一个事件,如下所示:
在 Poll 的构造函数中,添加这一行:
pollTimer_elapsed 如下所示:
在 Poll 中添加一个新的公共方法来启动计时器:
所以现在你的 startPool 方法如下所示:
恕我直言,这种方法是稍微好一些,因为 Poll 对象向外部对象隐藏了更多的实现。从 startPool 的角度来看,Poll 类使用起来更简单,并且您也不需要 Poll 类之外的任何内容来了解计时器。
The problem with the way you've designed the Poll class is that the Poll class doesn't completely do its job. You require other classes to know how to start and stop polling, meaning half of the polling implementation is inside the Poll class and half of the implementation is outside the Poll class. If you're going to create a Poll class, hide ALL the implementation details from everyone else.
Here is what I mean. I would create an event in Poll like this:
In Poll's constructor, add this line:
and the pollTimer_elapsed looks like this:
Add a new public method in Poll to start the timer:
So now your startPool method looks like this:
IMHO this approach is slightly better because the Poll object is hiding more of its implementation from external objects. From startPool's point of view, the Poll class is simpler to use, and you also don't require anything outside of your Poll class to know about Timers.
您可以添加一个属性:
公共布尔 pollTimerElapsed { 得到;放;并
在 pollTimer 的 Poll of Elapsed 事件的构造函数中订阅一个处理程序,其中您将属性 pollTimerElapsed 设置为 true
,然后您可以通过此属性过滤已过的轮询
you can add a property :
public bool pollTimerElapsed { get; set; }
and subscribe a handler in the contsructeur of Poll of Elapsed event of pollTimer where you set to true the property pollTimerElapsed
then you can filter elapsed Polls by this property