Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7612777
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T01:59:58+00:00 2026-05-31T01:59:58+00:00

I want to implement a scheduler class, which any object can use to schedule

  • 0

I want to implement a scheduler class, which any object can use to schedule timeouts and cancel then if necessary. When a timeout expires, this information will be sent to the timeout setter/owner at that time asynchronously.

So, for this purpose, I have 2 fundamental classes WindowsTimeout and WindowsScheduler.

class WindowsTimeout
{
  bool mCancelled;
  int mTimerID; // Windows handle to identify the actual timer set.
  ITimeoutReceiver* mSetter;

  int cancel()
  {
    mCancelled = true;

    if ( timeKillEvent(mTimerID) == SUCCESS) //  Line under question # 1
    {
        delete this; // Timeout instance is self-destroyed.
        return 0; // ok. OS Timer resource given back.
    }
    return 1; // fail. OS Timer resource not given back.
  }

  WindowsTimeout(ITimeoutReceiver* setter, int timerID)
  {
     mSetter = setter;
     mTimerID = timerID;
  }

};

class WindowsScheduler
{
    static void CALLBACK timerFunction(UINT uID,UINT uMsg,DWORD dwUser,DWORD dw1,DWORD dw2)    
{
     WindowsTimeout* timeout = (WindowsTimeout*) uMsg;
     if (timeout->mCancelled) 
          delete timeout;
     else
          timeout->mDestination->GEN(evTimeout(timeout));
}
WindowsTimeout* schedule(ITimeoutReceiver* setter, TimeUnit t)
    {
        int timerID = timeSetEvent(...);
        if (timerID == SUCCESS)
        {
             return WindowsTimeout(setter, timerID);
        }
        return 0;
    }
};

My questions are:

Q.1. When a WindowsScheduler::timerFunction() call is made, this call is performed in which context ? It is simply a callback function and I think, it is performed by the OS context, right ? If it is so, does this calling pre-empt any other tasks already running ? I mean do callbacks have higher priority than any other user-task ?

Q.2. When a timeout setter wants to cancel its timeout, it calls WindowsTimeout::cancel().
However, there is always a possibility that timerFunction static call to be callbacked by OS, pre-empting the cancel operation, for example, just after mCancelled = true statement. In such a case, the timeout instance will be deleted by the callback function.
When the pre-empted cancel() function comes again, after the callback function completes execution, will try to access an attribute of the deleted instance (mTimerID), as you can see on the line : “Line under question # 1” in the code.

How can I avoid such a case ?

Please note that, this question is an improved version of the previos one of my own here:
Windows multimedia timer with callback argument

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T02:00:00+00:00Added an answer on May 31, 2026 at 2:00 am

    Q1 – I believe it gets called within a thread allocated by the timer API. I’m not sure, but I wouldn’t be surprised if the thread ran at a very high priority. (In Windows, that doesn’t necessarily mean it will completely preempt other threads, it just means it will get more cycles than other threads).

    Q2 – I started to sketch out a solution for this, but then realized it was a bit harder than I thought. Personally, I would maintain a hash table that maps timerIDs to your WindowsTimeout object instances. The hash table could be a simple std::map instance that’s guarded by a critical section. When the timer callback occurs, it enters the critical section and tries to obtain the WindowsTimer instance pointer, and then flags the WindowsTimer instance as having been executed, exits the critical section, and then actually executes the callback. In the event that the hash table doesn’t contain the WindowsTimer instance, it means the caller has already removed it. Be very careful here.

    One subtle bug in your own code above:

    WindowsTimeout* schedule(ITimeoutReceiver* setter, TimeUnit t)
        {
            int timerID = timeSetEvent(...);
            if (timerID == SUCCESS)
            {
                 return WindowsTimeout(setter, timerID);
            }
            return 0;
        }
    };
    

    In your schedule method, it’s entirely possible that the callback scheduled by timeSetEvent will return BEFORE you can create an instance of WindowsTimeout.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

given this class definition: public class Frame { IFrameStream CapturedFrom; } I want implement
I want to implement a paperless filing system and was looking to use WIA
I want to implement in Java a class for handling graph data structures. I
I want to implement a simple debug log, which consists of a table into
Is there any way to customize or control which type of collection class types
I want to implement a schedule function in my project. So I Googled for
I want implement a program that can do a a Vision-based Page Segmentation. I
I'm trying to implement scheduler to my application. I use spring and quartz support.
I want implement in my software solution an VBA editor but in c# 3.0.
Microsoft has announce that WindowsLiveID become a OpenID provider . I want implement it

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.