Creating Events

  • Thread starter Thread starter Hoop
  • Start date Start date
H

Hoop

Hi,
I have a timer class and I would like to send an event based on specifc
times from the timer,
example at maybe every 500ms call a function, most likely a callback,
similar to the way the Systems.Timer works.
Is there a way one can create events to accomplish this?
Thanks
Jeff
 
There is already a timer class to do this. Is there any reason you
wanted to do it, yourself?
 
Hello Hoop,

Timer class supports System.Threading.TimerCallback callback
see http://msdn.microsoft.com/library/d...l/frlrfsystemthreadingtimerclassctortopic.asp

H> Hi,
H> I have a timer class and I would like to send an event based on
H> specifc
H> times from the timer,
H> example at maybe every 500ms call a function, most likely a callback,
H> similar to the way the Systems.Timer works.
H> Is there a way one can create events to accomplish this?
H> Thanks
H> Jeff
---
WBR,
Michael Nemtsev :: blog: http://spaces.msn.com/laflour

"At times one remains faithful to a cause only because its opponents do not
cease to be insipid." (c) Friedrich Nietzsche
 
Hi Sean,
I have the timer class running allready. Resolution is only 55ms at
best.
I need something much faster, so I have HighRes timer class that I
would like somehow to be event driven in the same way as the timer.
Thought may there was a way to add user defined events.
Thanks
Jeff
 
Hi Michael,
I have tried System.Threading.Timer. Works much better than standard
windows timer. That is most likely what I will
wind up using. Was just trying to see if there was a way that I could
try this HighRes timer class
that I have.
Thanks
Jeff
 
Hello Hoop,

How much faster do u need? Windows is not real time OS. AFAIK the bottom
line is 20ms

H> Hi Michael,
H> I have tried System.Threading.Timer. Works much better than standard
H> windows timer. That is most likely what I will
H> wind up using. Was just trying to see if there was a way that I could
H> try this HighRes timer class
H> that I have.
H> Thanks
H> Jeff
H> Michael Nemtsev wrote:
H>---
WBR,
Michael Nemtsev :: blog: http://spaces.msn.com/laflour

"At times one remains faithful to a cause only because its opponents do not
cease to be insipid." (c) Friedrich Nietzsche
 
Hi Michael,
Probably using the threading timer is maybe going to be OK, or maybe
just a thread and sleep() it for a some chosen time.
But I was trying to see if QueryPerformanceCounter() could beat it. I
have seen 15ms with the treading timer. A little jumpy but that might
work.
Thanks
Jeff
 
Hoop said:
Hi Sean,
I have the timer class running allready. Resolution is only 55ms at
best.
I need something much faster, so I have HighRes timer class that I
would like somehow to be event driven in the same way as the timer.
Thought may there was a way to add user defined events.

Absolutely. Add the following inside your class:

public event EventHandler Expired;
private void CallInterestedParties()
{
if (Expired != null) Expired(/* sender */ this, /* EventArgs */
EventArgs.Empty);
}
 
Hoop said:
Hi Michael,
Probably using the threading timer is maybe going to be OK, or maybe
just a thread and sleep() it for a some chosen time.
But I was trying to see if QueryPerformanceCounter() could beat it. I
have seen 15ms with the treading timer. A little jumpy but that might
work.
Thanks
Jeff

Waitable Timer, WaitForSingle/MultipleObject with a timeout, or select() all
should give better resolution. I have had 60 fps graphing using
MsgWaitForMultipleObjectsEx on a waitable timer in C++, and I think it could
have been smoother yet, but my graph only had room for 60 horizontal
pixels/second.
 
Hi Sean,
Yes, that looks like what I want to try.
I am having trouble with setting up the event to run.

After adding that code to the HiResTimer(),
In my main form, pressureForm, in the constructor, I do

public pressureForm()
{
startTimer()
}

startTimer()
{
MyTimer.HiResTimer.Start();

//Error, the name Expired does not exist in the current context
MyTimer.HiResTimer.ElapasedTime += new EventHandler(Expired);

}

Must not be exact way to do it.
Jeff

Ben said:
Hoop said:
Hi Sean,
I have the timer class running allready. Resolution is only 55ms at
best.
I need something much faster, so I have HighRes timer class that I
would like somehow to be event driven in the same way as the timer.
Thought may there was a way to add user defined events.

Absolutely. Add the following inside your class:

public event EventHandler Expired;
private void CallInterestedParties()
{
if (Expired != null) Expired(/* sender */ this, /* EventArgs */
EventArgs.Empty);
}
 
Hoop said:
Yes, that looks like what I want to try.
I am having trouble with setting up the event to run.

After adding that code to the HiResTimer(),
In my main form, pressureForm, in the constructor, I do

public pressureForm()
{
startTimer()
}

startTimer()
{
MyTimer.HiResTimer.Start();

//Error, the name Expired does not exist in the current context
MyTimer.HiResTimer.ElapasedTime += new EventHandler(Expired);

}

Must not be exact way to do it.

Expired is the name of an event, but you're trying to create an
EventHandler from it as if it were a method. When you subscribe to
ElapsedTime, you'll need to do so with a method. I suggest you write a
method which just calls Expired. You *could* just write:

MyTimer.HiResTimer.ElapasedTime += Expired;

but that will take the current value of the field-like event's delegate
instance instead of really associating the two events.

If all that was gobbledy-gook, read
http://www.pobox.com/~skeet/csharp/events.html
 

Then use a custom event:

public EventHandler Expired
{
add { MyTimer.HiResTimer.ElapasedTime += value; }
remove { MyTimer.HiResTimer.ElapasedTime -= value; }
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top