Shoutbox

Timer in a plug-in - Printable Version

-Shoutbox (https://shoutbox.menthix.net)
+-- Forum: MsgHelp Archive (/forumdisplay.php?fid=58)
+--- Forum: Messenger Plus! for Live Messenger (/forumdisplay.php?fid=4)
+---- Forum: Scripting (/forumdisplay.php?fid=39)
+----- Forum: Plug-Ins (/forumdisplay.php?fid=28)
+------ Thread: Timer in a plug-in (/showthread.php?tid=54138)

Timer in a plug-in by Itsme-HcK on 12-21-2005 at 12:48 PM

My plug-in needs to set some things every 30 seconds. Is there a good way to do it, or should I call an external program from my DLL?


RE: Timer in a plug-in by (CyBeRDuDe) on 12-21-2005 at 01:22 PM

In VB I guess?
Add a form, and add a Timer.
In the initialize function call:

Load Form1

And you can then use whatever you want, Form1.Timer1.Enable, Form1.Timer1.Interval.. Etc...


RE: Timer in a plug-in by RaceProUK on 12-21-2005 at 02:07 PM

If you're working in C++, then let me know, and I'll knock up an example for that.


RE: Timer in a plug-in by Itsme-HcK on 12-21-2005 at 10:03 PM

* Itsme-HcK lets raceprouk now.
If I can just keep the function running, just reply with that, I'll know what to do then. :)


RE: Timer in a plug-in by RaceProUK on 12-22-2005 at 11:11 AM

To start the timer:
UINT timerID = SetTimer(NULL, 1, <delay in ms>, &TimerProc);

To kill the timer:
KillTimer(timerID);

To respond to timer events:
void CALLBACK TimerProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
    // Code to respond to timer
}

Timers on MSDN
;)


RE: Timer in a plug-in by Itsme-HcK on 12-22-2005 at 01:45 PM

That will make my plug-in run all the time, while it is only supposed to be called once in the <number> minutes...


RE: Timer in a plug-in by RaceProUK on 12-22-2005 at 04:12 PM

void CALLBACK TimerProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
    // Code to respond to timer
    KillTimer(timerID);
}

:P


RE: Timer in a plug-in by Itsme-HcK on 12-22-2005 at 07:50 PM

Err, read what I said again. ;)
I will have to parse the MSG's. (Which forces me to run the whole time.)


RE: Timer in a plug-in by RaceProUK on 12-22-2005 at 08:22 PM

If you put KillTimer() in the TimerProc(), the TimerProc() will only be called once.


RE: Timer in a plug-in by J-Thread on 12-23-2005 at 12:25 AM

I don't know what you're trying to say... Off course your plugin is running all the time...that's usual for plugins...

If you want to do something every x minutes you just use raceprouk's code, and you place the code you want to execute in it.... You can also make a seperate dll for the code if you want to...


RE: Timer in a plug-in by CookieRevised on 12-23-2005 at 12:44 AM

quote:
Originally posted by Itsme-HcK
That will make my plug-in run all the time, while it is only supposed to be called once in the <number> minutes...

Your plugin (or at least a process of your plugin) will indeed always run. There is no way around in (in whatever language) as that is exactly what a timer does; it constantly needs a process to run, the process of the timer (what the timer process calls when it fires is another matter, that process doesn't need to be run all the time)...

Calling an external DLL for this is not better. True, you could do that, but even then a process needs to be running all the time. So why call an external process (with all the disadvantgaes of syncronization, or detecting when it needs to end, or whatever) while you can do this from your plugin itself (with the advantage that you have far less troubles, if not none, with syncronizations, etc), and above all, it already runs all time anyways...

quote:
Originally posted by Itsme-HcK
Err, read what I said again. ;)
I will have to parse the MSG's. (Which forces me to run the whole time.)
you parse messages (or whatever you want) in the TimerProc. TimerProc is only fired/called when a timeout occurs.



EDIT: damn, J-Thread was first :p (I always say refresh a thread before replying, but I often forget to do that also)
RE: Timer in a plug-in by Itsme-HcK on 12-23-2005 at 01:45 PM

Oh my.
You guys should READ. :|
What I'm saying is that, when I use the timer, I have to run the DLL ALL the time. So, when it's called, it will not execute that function, for it's still running the first function.


RE: RE: Timer in a plug-in by CookieRevised on 12-23-2005 at 03:00 PM

quote:
Originally posted by Itsme-HcK
Oh my.
You guys should READ. :|
we do... and your question has been answered several times though.

quote:
Originally posted by Itsme-HcK
What I'm saying is that, when I use the timer, I have to run the DLL ALL the time. So, when it's called, it will not execute that function, for it's still running the first function.
The TimerProc is a callback-function, an assyncronical function, just as events are, it is executed when a timeout occurs.

If you have programmed something which takes all the idle time (aka processing power) of the CPU so events, callbacks, etc can't be executed, you actually entirly lock out (hang) the program which calls you DLL and maybe even Windows itself and nothing will run anymore, meaning your code is bad to start with.

Did you read and fully understand the link raceprouk has given?
Timers on MSDN

RE: Timer in a plug-in by Itsme-HcK on 12-23-2005 at 03:26 PM

Does MsgPlus parse my msg's too, then?


RE: Timer in a plug-in by CookieRevised on 12-23-2005 at 04:16 PM

depends on what you actually want and what you mean by "messages".

Plus! doesn't "parse" messages from your plugin. It is the other way around, it send messages to your plugin so your plugin can parse them and return back a result.


RE: Timer in a plug-in by RaceProUK on 12-23-2005 at 06:01 PM

quote:
Originally posted by Itsme-HcK
Oh my.
You guys should READ. :|
What I'm saying is that, when I use the timer, I have to run the DLL ALL the time. So, when it's called, it will not execute that function, for it's still running the first function.
We do read, as Cookie said, and until now you hadn't explained what you were talking about.
In fact, yous till haven't explained everything fully: what's this 'first function'?
RE: Timer in a plug-in by Itsme-HcK on 12-23-2005 at 06:45 PM

Gah, ok.
When making a timer with the .net functions (the one you gave teh me), these are added to the message queue (read MSDN if you don't know them, it's the basic of almost every Windows program).
Added to this queue are also clicks with the mouse, actions, menu's, the whole crap. You have to parse those messages, so you can handle them. So, when you do that, the timer function will be called, resulting in a call to my function.
But, if the user doesn't send messages to buddies for say, 15 minutes, my plug-in isn't called the whole time. So isn't my function.
But, if MsgPlus! parses my messages too, that wouldn't be a problem, for MsgPlus! will then have it called. So, does it?
If it's not, I have to make an unending loop to get the results I want, which will probably crash MSN Messenger.


RE: Timer in a plug-in by matty on 12-23-2005 at 06:48 PM

quote:
Originally posted by Itsme-HcK
Gah, ok.
When making a timer with the .net functions (the one you gave teh me), these are added to the message queue (read MSDN if you don't know them, it's the basic of almost every Windows program).
Added to this queue are also clicks with the mouse, actions, menu's, the whole crap. You have to parse those messages, so you can handle them. So, when you do that, the timer function will be called, resulting in a call to my function.
But, if the user doesn't send messages to buddies for say, 15 minutes, my plug-in isn't called the whole time. So isn't my function.
But, if MsgPlus! parses my messages too, that wouldn't be a problem, for MsgPlus! will then have it called. So, does it?
If it's not, I have to make an unending loop to get the results I want, which will probably crash MSN Messenger.
I don't know about the rest of you but that makes absolutley no sense to me.

Are you trying to say you want a timer on your form to run so that your function is constantly called?
RE: Timer in a plug-in by (CyBeRDuDe) on 12-23-2005 at 08:09 PM

What I can understand from the last post of Itsme-Hck:
If add a plugin to MsgPlus!, it will run ALL the time, until Messenger is closed... If a user does not write to you in 15 minutes, your plugin is still "activated"/running...
This is of course logically to everyone.. Plugins don't just stop running because a person is not using Messenger..
If this is not what you are talking, I also have some sence that you are talking about Hooking or subclassing messages, recieved and sent by Messenger?...
2 Suggestions...
1: Try it out.. Your question seems very easy, though we are not sure we understand what you mean, but you seem to be confident in what's going on...
2: Write a better explantion, maybe come with some examples on what you are trying to achieve...

From what I could understand from the first post, you just want a timer that runs every 30 seconds, no matter what happens?... If so, just set the time in ms to 30000, and use raceprouk's code?...
If this is not what you want, you really need to explain better.. Because people seriously don't understand your post, and what you want to do...


RE: Timer in a plug-in by Itsme-HcK on 12-23-2005 at 08:23 PM

Maybe I should try #winprog. :P
Of course, the plug-in runs the whole time, but it is not active.
There is no main function or such, it is only activated when MsgPlus calls a function.
To use Windows timers, you need to be active all the time, and it's not.
So, only if MsgPlus! parses the messages from the DLL, it can be used.
My simple question: Does it?


RE: Timer in a plug-in by J-Thread on 12-23-2005 at 09:00 PM

What messages?

Are you trying to say you want to send a message to the other user every 30 seconds or so?

About the active plugin, when you use a timer, you should start that in the sign-in event (see the Messenger API for that) and off course kill it in the sign-out event. Your callback function will be called every 30 seconds, so then  your plugin is active. If you want to send a message to all users you have to find the input box and use API calls (sendchar or something like that) to send the text....

Is this an answer to your question?


RE: Timer in a plug-in by ShawnZ on 12-23-2005 at 09:13 PM

quote:
Originally posted by Itsme-HcK
Maybe I should try #winprog. :P
Of course, the plug-in runs the whole time, but it is not active.
There is no main function or such, it is only activated when MsgPlus calls a function.
To use Windows timers, you need to be active all the time, and it's not.
So, only if MsgPlus! parses the messages from the DLL, it can be used.
My simple question: Does it?

wtf are you on about...

Your plugin gets one of its functions called by msgplus (from within msn messenger) and then it starts a timer (again in msn messenger) and then that timer gets called (in msn messenger....) and the callback function (inserted into msn messenger) is run (from within msn messenger)

8-)
RE: Timer in a plug-in by (CyBeRDuDe) on 12-23-2005 at 10:17 PM

quote:
Originally posted by Itsme-HcK
Maybe I should try #winprog. :P
Of course, the plug-in runs the whole time, but it is not active.
There is no main function or such, it is only activated when MsgPlus calls a function.
To use Windows timers, you need to be active all the time, and it's not.
So, only if MsgPlus! parses the messages from the DLL, it can be used.
My simple question: Does it?
When using Windows timers/the timer code from raceprouk, your dll/functions/whatever you are on about, doesn't need to active all the time... When you have called the UINT TimerID function, the timer runs! No matter if your dll/form/function is active or not! The timer runs, and runs, and runs! Nothing more, nothing less...
Setting the number of ms in the timer event, adjusts how often the CallBack function is called... If time in ms is 1000, the callback funtion will be called 1 time every second... And still No matter if your dll/form/or whatever is active!
RE: Timer in a plug-in by Itsme-HcK on 12-24-2005 at 08:57 AM

Gah, but the Windows messages need to be parsed before my function gets called. MsgPlus! would, logically, only parse its own messages, not mine.
Never mind anyways, you appearantly didn't read MSDN about messages. ;)
I'll ask in #C++ or so. :)


RE: Timer in a plug-in by RaceProUK on 12-24-2005 at 11:26 AM

Excuse me, but we do all read MSDN. I myself have written several WndProcs and a message loop or two. Hell, I linked you to MSDN! And the timer code I gave you bypasses all the message processing crap!

If you're worried about a message loop, don't be. So long as you call SetTimer() in the same thread as Initialize(), Messenger's own message loop will handle dispatching the timer messages.

Edit: I know Cookie, which is why I said the same thread as Initialize() :P


RE: Timer in a plug-in by Itsme-HcK on 12-24-2005 at 06:03 PM

Then why the hell didn't you just SAY SO? :|
Howevers, then that's it. :)
Thank y00, for as far as it did come. :P


RE: RE: Timer in a plug-in by CookieRevised on 12-24-2005 at 09:00 PM

quote:
Originally posted by raceprouk
If you're worried about a message loop, don't be. So long as you call SetTimer() in the same thread as Initialize(), Messenger's own message loop will handle dispatching the timer messages.
For that it doesn't nessecairly need to be in the Initialize() function though. You can put it in whatever procedure, as long as SetTimer() is executed somewhere within the plugin (and TimerProc() is available to the outside world)...

(eg: if you want to start the timer only when a command is parsed, you only need to put it in the ParseCommand() function).

quote:
Originally posted by Itsme-HcK
Added to this queue are also clicks with the mouse, actions, menu's, the whole crap. You have to parse those messages, so you can handle them. So, when you do that, the timer function will be called, resulting in a call to my function.
You are confusing stuff and making it harder than it is. You do not need to parse any windows messages (unless you're in your own dialog screen or are subclassing or whatever). Windows messages are almost always handled by the owners, in this case that would be Messenger itself. In case for a timer countdown, that is handled by Windows if you use a callback function.

All can be found in the MSDN Library. (Or if you only had tried out Raceprouk's example in his very first reply)

quote:
Originally posted by Itsme-HcK
But, if the user doesn't send messages to buddies for say, 15 minutes, my plug-in isn't called the whole time. So isn't my function.
It is your function, as it is a callback function which you have set as a parameter when you set the timer.

Timer messages are handled by Windows when you use a callback function, as you can read on the MSDN pages, and when they trigger they call that given function in your program back (hence callback function).

quote:
Originally posted by Itsme-HcK
Then why the hell didn't you just SAY SO? :|
Howevers, then that's it. :)
Thank y00, for as far as it did come. :P
tbh, everything was already said from the very first replies, then it got a bit vague (because you didn't say what you meant with "messages" and it is so inlogical that you meant windows messages), but after that, again the people already said all this in their replies... :S

It is clearly explained and shown on the MSDN library help page for timers, linked to by raceprouk.
quote:
An application can process WM_TIMER messages by including a WM_TIMER case statement in the window procedure or by specifying a TimerProc callback function when creating the timer. When you specify a TimerProc callback function, the default window procedure calls the callback function when it processes WM_TIMER.
and there is even a full working examples given.
RE: Timer in a plug-in by JohnyWalker on 12-27-2005 at 09:32 PM

I created timer function without much problem using the VB.Net sample.

Declare a timer object for the class as below
Dim _Timer As System.Timers.Timer

In the Initialize function, add

Try
            _Timer = New System.Timers.Timer(30000)
            AddHandler _Timer.Elapsed, AddressOf OnTimedEvent
            _Timer.Start()
        Catch ex As Exception
            LogError(ex.Message.ToString())
        End Try

        Initialize = True



LogError just writes to a text file

Add the Timer event(callback function)


Public Sub OnTimedEvent(ByVal source As Object, ByVal e As System.Timers.ElapsedEventArgs)
        '_Timer.Stop()
        LogError("Timer Event")
        DisplayToast("Hello World", "Plus Test", "http://www.google.com", True)
    End Sub


It works great. I am getting the "Timer Event" message in the log file every 30 seconds.

Now for my problem, DisplayToast does not work. If I put it in the Initialize function, it works. Can someone please explain ?

Thanks


RE: Timer in a plug-in by J-Thread on 12-27-2005 at 09:45 PM

Because the timer is (probably) in another thread, and MSG Plus! only accepts logs and toasts from the main thread. You will have to use delegates for it, Jedimark has written something about that in the past here...


RE: Timer in a plug-in by CookieRevised on 12-28-2005 at 06:17 AM

Or simply create a window somewhere from one of the main plus! functions (thus this will be in the same thread) and when you create the timer, give the handle of that window to it. You don't need the callback function either...

all explained in MSDN... SetTimer()