简体   繁体   English

如何重新计划C#System.Threading.Timer?

[英]How to reschedule C# System.Threading.Timer?

I'm attempting to create a Session implementation. 我正在尝试创建一个Session实现。 In order to complete it, I need to create Session timeouts. 为了完成它,我需要创建会话超时。 To do this, I decided I should use a Timer that executes after x seconds. 为此,我决定我应该使用一个在x秒后执行的Timer。 However, if a request is received before that timer expires, then it should be rescheduled. 但是,如果在该计时器到期之前收到请求,则应重新安排该请求的时间。

So, I have a timer: 因此,我有一个计时器:

using System.Threading.Timer;

public class SessionManager {
    private int timeToLive; //Initialized in the constructor.
    private ConcurrentDictionary<Guid, Session> sessions; //Populated in establishSession. Removed in abandonSession.

    public Session establishSession(...)
    {
        Session session = ...; //I have a session object here. It's been added to the dictionary.

        TimerCallback tcb = abandonSession;
        Timer sessionTimer = new Timer(tcb, null, timeToLive, Timeout.Infinite);
    }

    public void abandonSession(Object stateInfo)
    {
        //I need to cancel the session here, which means I need to retrieve the Session, but how?
    }

    public void refreshSession(Session session)
    {
        //A request has come in; I have the session object, now I need to reschedule its timer. How can I get reference to the timer? How can I reschedule it?
    }
}

What I need help with: 我需要什么帮助:

  1. I could make the sessionTimer a member of the Session object. 我可以使sessionTimer成为Session对象的成员。 That would give me access to the Timer object in refreshSession() but I don't know how to "reschedule" it. 这将使我能够访问refreshSession()的Timer对象,但是我不知道如何“重新安排”它。

  2. I still don't have any idea how I can get a reference to the Session in abandonSession() callback. 我仍然不知道如何在abandonSession()回调中获得对Session的引用。 Is there a way to send the Session object in the stateInfo ? 有没有一种方法可以在stateInfo发送Session对象?

I was thinking I could store a reference to the SessionManager on the Session object, and have the callback reference a method on the Session object for the abandonSession() call. 我当时想我可以在Session对象上存储对SessionManager的引用,并让回调对Session对象上的方法进行abandonSession()调用的回调。 That seemed sloppy though. 不过这似乎草率。 What do you think? 你怎么看?

Please, let me know if additional information is needed. 请让我知道是否需要其他信息。

Use the Change method to set a new invocation delay: 使用Change方法设置新的调用延迟:

sessionTimer.Change(timeToLive, timeToLive)

As for getting a value in your callback method, the second parameter that you currently pass as null is your callback object... Your Timer callback method forces a signature of object and you can cast that object to your passed in type to use it. 至于在回调方法中获取值,您当前作为null传递的第二个参数是回调对象...您的Timer回调方法将强制object签名,您可以将该对象转换为传入的类型以使用它。

var myState = new Something();
var sessionTimer = new Timer(tcb, myState, timeToLive, Timeout.Infinite);

...

public void abandonSession(Object stateInfo)
{
    var myState = (Something)stateInfo;
    //I need to cancel the session here, which means I need to retrieve the Session, but how?
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM