From: hermann leinen on
Hello!

I want to migrate from VB6 to C#.

In VB6 I have a class that checks for mails each minute.
This mail-check class also instantiates a self-firing timer class
(cSelfTimer) because I did not want to put a timer in one of my forms.

It looks like this

Class clsMail

Private WithEvents cTmr as clsTimer

Private Sub cTmr_TimerEvent
'// Check for new mails
End sub

End ClsMail

Can somebody please tell me how I should do this in C#?

Should I write a class in C# that also has a timer class (if such
exists) in it, or what would be a nice way to do it in C#?

Thank you,
Hermann



From: Peter Duniho on
hermann leinen wrote:
> Hello!
>
> I want to migrate from VB6 to C#.
>
> In VB6 I have a class that checks for mails each minute.
> This mail-check class also instantiates a self-firing timer class
> (cSelfTimer) because I did not want to put a timer in one of my forms.

What's "cSelfTimer"? A class you wrote from scratch? Something else?

> [...]
> Should I write a class in C# that also has a timer class (if such
> exists) in it, or what would be a nice way to do it in C#?

It's not clear why you don't want to use the System.Windows.Forms.Timer
class, given a Forms application. However, with that restriction as
given, you can use either of the other Timer classes in .NET:
System.Threading.Timer, or System.Timers.Timer (the latter uses the
former�which to use depends mainly on which offers the API best suited
to your particular use).

Note that by not using System.Windows.Forms.Timer, you will have to deal
with cross-thread invocation issues yourself. If there's no interaction
at all with the GUI, that won't be an issue, but otherwise you may find
System.Windows.Forms.Timer to be the best choice after all.

Pete
From: hermann leinen on
cSelfTimer is class that uses the API SetTimer.
SetTimer calls a callback function within cSelfTimer at each timer event.
The class which "hosts" cSelfTimer receives the fire event because
cSelfTimer raises it.

I did it this because I didn't like to have separate forms just to host
controls like timers, WinSock, etc.

Did I understand it correctly that this is in fact how it should be done
in C#?
-> I should make a form and just place the timer on it and then when it
fires, I say something like (pseudocode)

Form1.Timer1.TimerEvent
someclass.MakeSomething

I hope I got it correctly.

Regards,
Hermann


Am 25.02.2010 08:51, schrieb Peter Duniho:
> hermann leinen wrote:
>> Hello!
>>
>> I want to migrate from VB6 to C#.
>>
>> In VB6 I have a class that checks for mails each minute.
>> This mail-check class also instantiates a self-firing timer class
>> (cSelfTimer) because I did not want to put a timer in one of my forms.
>
> What's "cSelfTimer"? A class you wrote from scratch? Something else?
>
>> [...]
>> Should I write a class in C# that also has a timer class (if such
>> exists) in it, or what would be a nice way to do it in C#?
>
> It's not clear why you don't want to use the System.Windows.Forms.Timer
> class, given a Forms application. However, with that restriction as
> given, you can use either of the other Timer classes in .NET:
> System.Threading.Timer, or System.Timers.Timer (the latter uses the
> former�which to use depends mainly on which offers the API best suited
> to your particular use).
>
> Note that by not using System.Windows.Forms.Timer, you will have to deal
> with cross-thread invocation issues yourself. If there's no interaction
> at all with the GUI, that won't be an issue, but otherwise you may find
> System.Windows.Forms.Timer to be the best choice after all.
>
> Pete

From: Peter Duniho on
hermann leinen wrote:
> cSelfTimer is class that uses the API SetTimer.

What API SetTimer? The unmanaged SetTimer() function requires a window.

> SetTimer calls a callback function within cSelfTimer at each timer event.
> The class which "hosts" cSelfTimer receives the fire event because
> cSelfTimer raises it.
>
> I did it this because I didn't like to have separate forms just to host
> controls like timers, WinSock, etc.

If you had any form at all, you could use that one for the timer. If
you're using the unmanaged SetTimer() function, you must have a window
handle for that (e.g. a form). If there's some VB6 function called
SetTimer() that doesn't require you to provide a window, it almost
certainly creates one on your behalf.

Which is all a long way of saying that if your current VB code uses
SetTimer() or anything related to it, that you are currently using a
window-associated timer, and the System.Windows.Forms.Timer timer will
be the most direct "drop-in" replacement for your needs.

> Did I understand it correctly that this is in fact how it should be done
> in C#?
> -> I should make a form and just place the timer on it and then when it
> fires, I say something like (pseudocode)
>
> Form1.Timer1.TimerEvent
> someclass.MakeSomething
>
> I hope I got it correctly.

If you are using System.Windows.Forms.Timer, then yes�you can drag it
from the "Components" section of the Toolbox in the Designer, onto your
form, where it will wind up in the "components tray" below the design
area. You can select it there and set properties, such as the Interval
to use, and/or subscribe to the Tick event to create an event handler
that will be called every time the timer period elapses.

The timer will be available in your code, using the variable name
created for it by the Designer. By default, this will be "timer1", a
private field in your Form sub-class. You can use the Start() and
Stop() methods, or the Enabled property, to control whether the timer is
active.

MSDN has a lot more details, if the above is not sufficient.

Pete
From: hermann leinen on
Thanks for the reply.

Btw, no, I don't need a hwnd for the SetTimer function (it's the windows
api function), I am using ASM code and vtable manipulation to have it
callback directly in/on my function.

I only thought that Microsoft had perhaps invented something of that
kind on their own, but it seems not.

Thanks again,

Hermann

Am 25.02.2010 18:31, schrieb Peter Duniho:
> hermann leinen wrote:
>> cSelfTimer is class that uses the API SetTimer.
>
> What API SetTimer? The unmanaged SetTimer() function requires a window.
>
>> SetTimer calls a callback function within cSelfTimer at each timer event.
>> The class which "hosts" cSelfTimer receives the fire event because
>> cSelfTimer raises it.
>>
>> I did it this because I didn't like to have separate forms just to
>> host controls like timers, WinSock, etc.
>
> If you had any form at all, you could use that one for the timer. If
> you're using the unmanaged SetTimer() function, you must have a window
> handle for that (e.g. a form). If there's some VB6 function called
> SetTimer() that doesn't require you to provide a window, it almost
> certainly creates one on your behalf.
>
> Which is all a long way of saying that if your current VB code uses
> SetTimer() or anything related to it, that you are currently using a
> window-associated timer, and the System.Windows.Forms.Timer timer will
> be the most direct "drop-in" replacement for your needs.
>
>> Did I understand it correctly that this is in fact how it should be
>> done in C#?
>> -> I should make a form and just place the timer on it and then when
>> it fires, I say something like (pseudocode)
>>
>> Form1.Timer1.TimerEvent
>> someclass.MakeSomething
>> I hope I got it correctly.
>
> If you are using System.Windows.Forms.Timer, then yes�you can drag it
> from the "Components" section of the Toolbox in the Designer, onto your
> form, where it will wind up in the "components tray" below the design
> area. You can select it there and set properties, such as the Interval
> to use, and/or subscribe to the Tick event to create an event handler
> that will be called every time the timer period elapses.
>
> The timer will be available in your code, using the variable name
> created for it by the Designer. By default, this will be "timer1", a
> private field in your Form sub-class. You can use the Start() and Stop()
> methods, or the Enabled property, to control whether the timer is active.
>
> MSDN has a lot more details, if the above is not sufficient.
>
> Pete