timers on the fly.
my current working code is on pastebin
http://pastebin.com/LEWkC5E8
What it is for is I have embedded js that is ran
inside of a c# class. I have the app talking the the JS already.
Im trying to make support for calling a javascript function once the timer elapses.
This is defined in the javascript call to setTimer(interval,callback,arguments)
What I want to do is every time setTimer() is called (will only be from javascript)
I want to create a new (one time) timer that will delete itself after it has elapsed and calls its callback.
my code does not even try to do this,
I know I will have to use an array of timers just not sure how as I'm new to c#
The code I posted will only work if the setTimer is not used more than once before
the timer elapses. Otherwise it still works but the event handler from the first call is removed and the second one is only called.
The other issue is it restarts the timers when setTimer() is called,
thus making the first call to it useless.
I was thinking there is some way using a class that will add/remove timers on the fly
and still be able to support the [ tryCallBack(cb, "('" + arg + "');"); ] as this is used to call
a callback defined by the js.
this is the javascript code i used to test this against so you get the idea.
when using it like this only the second timer works.
function cb(arg){
nprint(arg);
};
setTimer(3000,'cb','hi from cb');
setTimer(1000,'cb','hi from cb2');
I was hoping to to get an example of away around this so i can finish implementing the timer functions.
thanks for any advice you can give and please keep in mind im pretty new to c# or even manage code in general.
Jonathan BorgnePosted Mar 28, 2012, 10:26 PM
making the 2 second timer being called after 4 seconds of the 10 second timer has passed.
its just an example but i have to consider it esp once this gets down to milliseconds. Also I wont ever know
the order of intervals as it will defined at runtime.
the other problem is the event thats fired needs 2 arguments that are passed in from the javascript this is
the javascript callback name and arguments and it is used to call the javascript defined function once the timer elasped.
I wont know the names of the javascript functions before hand there fore the c# event needs to be updated with the new callback name and arguments. The javascript thats loaded by the c# engine will be in files and will be very different from engine instance to engine instance.
Do you think this would work good if I made an array that would hold the times, callback names and arguments
that looping threw them every millisecond would be okay? Can a c# timer handle that?
I could even make a limitation of how many timers can be active at any giving time(maybe 100), keeping the array size down.
I dont know much about the list types in c# or the dictionary so Im not sure if i should be looking into them or just use an array.
Any further suggestions and/or advice will be greatly appreciated.
Sam HobbsPosted Mar 28, 2012, 6:44 PM
Jonathan BorgnePosted Mar 28, 2012, 6:13 PM
any schooling / jobs doing programming. So I may be way out of line on something from the simple fact I just dont know
what the terminology is for something i'm trying to describe.
Well you got the most of it, and I do (agree/prefer) maybe this can be done with one timer.
Javascript will be the scripting language of a larger c# engine.
The thing about the Javascript is the way I have it embedded into c#
it does not support standard javascript setTimeOut();
or any other browser related functions.
So i'm just trying to replicate setTimeOut(); when I run the code in C#,
and support more that one javascript timer at a time.
the [c#]function trycallback that i posted is executing (inline javascript) to call a javascript function predefined at runtime.
so when i load a js file that contains
it will call the c# code to set a timer with a event that pass's the javascripts callback name and arguments to the c# event function
to call the javascript function at the set interval. after this it should remove this c# event for the timer so it can
be reused. If im waiting for only 1 event thats fine and simple but
if i go ahead and set up 3 or 4 timers in the js with different intervals
they should all execute at there set interval from the time the js was executed and not have to wait for another setTimer
to finish before this one starts counting down its interval.
I hope this clears it up a bit. I know it sounds like a big circle but if I want to support a scripting language for my engine
it must support the use of timers.
I can code some of this in javascript to support one timer in c# if you think thats a better route.
I think I would need my c# timer to fire every 1ms to keep up with any intervals I have set in the js.
My guess is this may cause a lag in either the engine or the js.
thanks, Jon
Sam HobbsPosted Mar 28, 2012, 1:11 PM
I have a suggestion however. I think it is innefficient and unnecessary to have more than one timer at a time. I think you can have an array of times and then set the timer for just the next time; the time that is the least amount of time in the future. Then when that time expires, set the interval to the amount of time until the next time is to occur.
So I am not sure what you need help with, but it seems that you need help with passing timers to/from JavaScript from/to C#. So my suggestion might help since then all that needs to be passed is an array of times. If that does not help then please try to make it more clear what it is that you need help with.