包含带有计时器的对象的字典。需要找出哪个对象的计时器正在调用 elapsed 事件

发布于 2024-12-07 08:37:33 字数 1611 浏览 2 评论 0原文

我有这个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

策马西风 2024-12-14 08:37:33

您设计 Poll 类的方式的问题在于 Poll 类没有完全完成其工作。您需要其他类知道如何启动和停止轮询,这意味着一半的轮询实现在 Poll 类内部,一半的实现在 Poll 类外部。如果您要创建一个 Poll 类,请向其他人隐藏所有实现细节。

这就是我的意思。我将在 Poll 中创建一个事件,如下所示:

public event EventHandler<ElapsedEventArgs> Elapsed;

在 Poll 的构造函数中,添加这一行:

this.pollTimer.Elapsed += pollTimer_elapsed;

pollTimer_elapsed 如下所示:

private void pollTimer_elapsed(object sender, ElapsedEventArgs e)
{
    var han = this.Elapsed;
    if (han != null)
        han(this, e); // Fire the Elapsed event, passing 'this' Poll as the sender
}

在 Poll 中添加一个新的公共方法来启动计时器:

public void Start()
{
    this.pollTimer.Start();
}

所以现在你的 startPool 方法如下所示:

public void startPool(Channel channel)
{
    polls.Add(channel, new Poll(question, tempdict));
    polls[channel].Elapsed += poll_Elapsed;
    polls[channel].Start();
}

static void poll_Elapsed(object sender, ElapsedEventArgs e)
{
    //sender is now a Poll object
    var poll = sender as Poll;
    // Now you can do poll.pollTimer.Stop()
    // Or better yet, add a Stop method to the Poll class and call poll.Stop()
}

恕我直言,这种方法是稍微好一些,因为 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:

public event EventHandler<ElapsedEventArgs> Elapsed;

In Poll's constructor, add this line:

this.pollTimer.Elapsed += pollTimer_elapsed;

and the pollTimer_elapsed looks like this:

private void pollTimer_elapsed(object sender, ElapsedEventArgs e)
{
    var han = this.Elapsed;
    if (han != null)
        han(this, e); // Fire the Elapsed event, passing 'this' Poll as the sender
}

Add a new public method in Poll to start the timer:

public void Start()
{
    this.pollTimer.Start();
}

So now your startPool method looks like this:

public void startPool(Channel channel)
{
    polls.Add(channel, new Poll(question, tempdict));
    polls[channel].Elapsed += poll_Elapsed;
    polls[channel].Start();
}

static void poll_Elapsed(object sender, ElapsedEventArgs e)
{
    //sender is now a Poll object
    var poll = sender as Poll;
    // Now you can do poll.pollTimer.Stop()
    // Or better yet, add a Stop method to the Poll class and call poll.Stop()
}

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.

冷了相思 2024-12-14 08:37:33

您可以添加一个属性:
公共布尔 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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文