Click here to Skip to main content
15,888,984 members
Home / Discussions / C#
   

C#

 
AnswerRe: Inplement a precise timer Pin
PIEBALDconsult1-Sep-10 15:28
mvePIEBALDconsult1-Sep-10 15:28 
GeneralRe: Inplement a precise timer Pin
Luc Pattyn1-Sep-10 15:52
sitebuilderLuc Pattyn1-Sep-10 15:52 
GeneralRe: Inplement a precise timer Pin
PIEBALDconsult1-Sep-10 16:47
mvePIEBALDconsult1-Sep-10 16:47 
JokeRe: Inplement a precise timer Pin
Keith Barrow1-Sep-10 21:04
professionalKeith Barrow1-Sep-10 21:04 
GeneralRe: Inplement a precise timer Pin
OriginalGriff1-Sep-10 21:45
mveOriginalGriff1-Sep-10 21:45 
AnswerRe: Inplement a precise timer Pin
Garth J Lancaster1-Sep-10 17:45
professionalGarth J Lancaster1-Sep-10 17:45 
GeneralRe: Inplement a precise timer Pin
cateyes992-Sep-10 12:28
cateyes992-Sep-10 12:28 
AnswerRe: Inplement a precise timer Pin
Pete O'Hanlon1-Sep-10 22:30
mvePete O'Hanlon1-Sep-10 22:30 
Don't use a timer for this - take a look, rather, at using something like a Monitor where you can specify an elapsed interval before the monitor "wakes up". It would look something like this:
private static readonly object _syncLock = new object();
private bool _cancel;
private void ScheduleProcess(DateTime scheduledDate)
{
  if (scheduledDate >= DateTime.Now)
    throw new ArgumentOutOfRangeException("The scheduled time is set for a time that has already passed.");
  TimeSpan waittime = scheduledDate - DateTime.Now;
  lock (_syncLock)
  {
    Monitor.Wait(_syncLock, waittime);
  }
}

public void Cancel()
{
  _cancel = true;
  lock(_syncLock)
  {
    Monitor.PulseAll(_syncLock);
  }
}
Create a background thread, and use ScheduleProcess to block access to your functionality until the time has passed. For instance:
C#
public void DoSomething(DateTime scheduledTime)
{
  SmartThreadPool stp = new SmartThreadPool();
  stp.QueueWorkItem(new WorkItemCallback(RunProcess), scheduledTime);
}

private object RunProcess(object scheduledTime)
{
  ScheduleProcess((DateTime)scheduledTime);
  if (!_cancel)
  {
    // Run my process
  }

  return null;
}
This example uses the SmartThreadPool[^].

I have CDO, it's OCD with the letters in the right order; just as they ruddy well should be

Forgive your enemies - it messes with their heads


My blog | My articles | MoXAML PowerToys | Onyx


GeneralRe: Inplement a precise timer Pin
Tom Foswick2-Sep-10 5:55
Tom Foswick2-Sep-10 5:55 
GeneralRe: Inplement a precise timer Pin
cateyes992-Sep-10 11:50
cateyes992-Sep-10 11:50 
GeneralRe: Inplement a precise timer Pin
cateyes992-Sep-10 11:48
cateyes992-Sep-10 11:48 
AnswerRe: Inplement a precise timer Pin
roman4002-Sep-10 1:31
roman4002-Sep-10 1:31 
GeneralRe: Inplement a precise timer Pin
cateyes992-Sep-10 11:51
cateyes992-Sep-10 11:51 
AnswerRe: Inplement a precise timer Pin
englebart2-Sep-10 2:07
professionalenglebart2-Sep-10 2:07 
GeneralRe: Inplement a precise timer [modified] Pin
cateyes992-Sep-10 11:54
cateyes992-Sep-10 11:54 
AnswerRe: Inplement a precise timer Pin
agolddog2-Sep-10 4:08
agolddog2-Sep-10 4:08 
GeneralRe: Inplement a precise timer Pin
cateyes992-Sep-10 12:10
cateyes992-Sep-10 12:10 
GeneralRe: Inplement a precise timer Pin
agolddog2-Sep-10 12:16
agolddog2-Sep-10 12:16 
GeneralRe: Inplement a precise timer Pin
rhp80902-Sep-10 15:28
rhp80902-Sep-10 15:28 
QuestionGenericize access to variables Pin
David Knechtges1-Sep-10 3:59
David Knechtges1-Sep-10 3:59 
AnswerRe: Genericize access to variables PinPopular
Ennis Ray Lynch, Jr.1-Sep-10 4:07
Ennis Ray Lynch, Jr.1-Sep-10 4:07 
GeneralRe: Genericize access to variables Pin
Chris Trelawny-Ross1-Sep-10 4:28
Chris Trelawny-Ross1-Sep-10 4:28 
GeneralRe: Genericize access to variables Pin
harold aptroot1-Sep-10 4:42
harold aptroot1-Sep-10 4:42 
GeneralRe: Genericize access to variables Pin
Luc Pattyn1-Sep-10 5:12
sitebuilderLuc Pattyn1-Sep-10 5:12 
GeneralRe: Genericize access to variables Pin
Chris Trelawny-Ross1-Sep-10 5:19
Chris Trelawny-Ross1-Sep-10 5:19 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.