From: Peter Duniho on
hermann leinen wrote:
> Thanks for the reply.
>
> Btw, no, I don't need a hwnd for the SetTimer function (it's the windows
> api function),

Sure you do. You are allowed to pass NULL for the hWnd parameter, but
you still have to have a window somewhere, and you still need to be
dispatching messages for the window.

> I am using ASM code and vtable manipulation to have it
> callback directly in/on my function.

Why anyone would do that, I have no idea. Sounds like an awful
implementation. But regardless, it doesn't get you out of having to
have a window somewhere.

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

I also don't understand that comment. Something of what kind?
Something like SetTimer()? A timer that doesn't require a window? Both
exist in .NET. What else do you want?

Pete
From: Tom Shelton on
On 2010-02-26, Peter Duniho <no.peted.spam(a)no.nwlink.spam.com> wrote:
> hermann leinen wrote:
>> Thanks for the reply.
>>
>> Btw, no, I don't need a hwnd for the SetTimer function (it's the windows
>> api function),
>
> Sure you do. You are allowed to pass NULL for the hWnd parameter, but
> you still have to have a window somewhere, and you still need to be
> dispatching messages for the window.
>
>> I am using ASM code and vtable manipulation to have it
>> callback directly in/on my function.
>
> Why anyone would do that, I have no idea. Sounds like an awful
> implementation. But regardless, it doesn't get you out of having to
> have a window somewhere.
>
>> I only thought that Microsoft had perhaps invented something of that
>> kind on their own, but it seems not.
>
> I also don't understand that comment. Something of what kind?
> Something like SetTimer()? A timer that doesn't require a window? Both
> exist in .NET. What else do you want?
>
> Pete

Actually Peter - you don't need to have a window handle or a window to use
SetTimer. What you need to do is dispatch messages on the callign thread...

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.InteropServices;
using System.ComponentModel;

namespace SetTimerExample
{
class Program
{
private static bool done = false;
private static int count = 10;
private delegate void TimerProc(IntPtr hWnd, uint uMsg, uint idEvent, uint dwTime);

private struct POINT
{
public int X;
public int Y;
}

private struct MSG
{
public IntPtr hwnd;
public uint message;
public uint wParam;
public uint lParam;
public uint time;
public POINT pt;
}

[DllImport("user32", SetLastError=true)]
private static extern uint SetTimer ( IntPtr hWnd, uint nIDEvent, uint uElapse, TimerProc lpTimerFunc );

[DllImport ( "user32", SetLastError = true )]
private static extern bool KillTimer ( IntPtr hWnd, uint nIDEvent );

[DllImport("user32", SetLastError=true)]
private static extern bool GetMessage (out MSG lpMsg, IntPtr hWnd, uint wMsgFilterMin, uint wMsgFilterMax);

[DllImport ( "user32", SetLastError = true )]
private static extern int DispatchMessage ( ref MSG lpMsg );

static void Main ( string[] args )
{
TimerProc lpTimerFunc = TimerFunc;
uint nIDEvent = SetTimer ( IntPtr.Zero, 0, 1500, lpTimerFunc );
if ( nIDEvent != 0 )
{
MSG msg;
while ( !done )
{
GetMessage ( out msg, IntPtr.Zero, 0, 0 );
DispatchMessage ( ref msg );
}

Console.WriteLine ( "Killing Timer" );
Console.WriteLine ( "Kill Result: {0}", KillTimer ( IntPtr.Zero, nIDEvent ) );
Console.ReadLine ();
}
else
{
Win32Exception ex = new Win32Exception ( Marshal.GetLastWin32Error () );
Console.WriteLine ( ex.Message );
}
}

static void TimerFunc ( IntPtr hWnd, uint uMsg, uint idEvent, uint dwTime )
{
Console.WriteLine ( "Tick: {0}", dwTime );
if ( --count == 0 )
done = true;
}
}
}

--
Tom Shelton
From: Peter Duniho on
Tom Shelton wrote:
> On 2010-02-26, Peter Duniho <no.peted.spam(a)no.nwlink.spam.com> wrote:
> [...]
>> Sure you do. You are allowed to pass NULL for the hWnd parameter, but
>> you still have to have a window somewhere, and you still need to be
>> dispatching messages for the window.
> [...]
>
> Actually Peter - you don't need to have a window handle or a window to use
> SetTimer. What you need to do is dispatch messages on the callign thread...

Hmmm´┐Żbeen doing too much Forms programming, where you practically never
are dispatching messages without a window. As you can see above, at
least I got the dispatching part right. :)

Thanks for the correction.

That said, I still don't understand what it is the OP wants that he
thinks doesn't exist in .NET.

Pete