Is it preferable to register multiple SetWinEventHook() functions with the same WinEventProc() Callback function and handle each event type separately inside the function's code or as many as I want.
EDIT : I posted three different scenarios and I want to know which one is the best and why ?
Case 1 : Single callback, single delegate, multiple hooks
static WinEventDelegate SingleCallbackDelegate = new WinEventDelegate(SingleCallback);
public static void SingleCallback(IntPtr hWinEventHook, uint eventType, IntPtr hwnd, int idObject, int idChild, uint dwEventThread, uint dwmsEventTime)
{
switch(evenType) :
case eventId1 : // do work related to event 1
case eventId2 : // do work related to event 2
// etc.
}
void SetHooks()
{
SetWinEventHook(eventId1, eventId1, IntPtr.Zero, SingleCallbackDelegate, 0,
0, flags);
SetWinEventHook(eventId2, eventId2, IntPtr.Zero, SingleCallbackDelegate, 0,
0, flags);
}
Case 2 : Single callback, multiple delegates, multiple hooks
static WinEventDelegate CallbackDelegate1 = new WinEventDelegate(SingleCallback);
static WinEventDelegate CallbackDelegate2 = new WinEventDelegate(SingleCallback);
public static void SingleCallback(IntPtr hWinEventHook, uint eventType, IntPtr hwnd, int idObject, int idChild, uint dwEventThread, uint dwmsEventTime)
{
switch(evenType) :
case eventId1 : // do work related to event 1
case eventId2 : // do work related to event 2
// etc.
}
void SetHooks()
{
SetWinEventHook(eventId1, eventId1, IntPtr.Zero, CallbackDelegate1, 0,
0, flags);
SetWinEventHook(eventId2, eventId2, IntPtr.Zero, CallbackDelegate2, 0,
0, flags);
}
Case 3 : Multiple callbacks, multiple delegates, multiple hooks
static WinEventDelegate CallbackDelegate1 = new WinEventDelegate(Callback1);
public static void Callback1(IntPtr hWinEventHook, uint eventType, IntPtr hwnd, int idObject, int idChild, uint dwEventThread, uint dwmsEventTime)
{
// do work related to event 1
}
static WinEventDelegate CallbackDelegate2 = new WinEventDelegate(Callback2);
public static void Callback1(IntPtr hWinEventHook, uint eventType, IntPtr hwnd, int idObject, int idChild, uint dwEventThread, uint dwmsEventTime)
{
// do work related to event 2
}
void SetHooks()
{
SetWinEventHook(eventId1, eventId1, IntPtr.Zero, CallbackDelegate1, 0,
0, flags);
SetWinEventHook(eventId2, eventId2, IntPtr.Zero, CallbackDelegate2, 0,
0, flags);
}