Click here to Skip to main content
15,894,460 members
Home / Discussions / C#
   

C#

 
GeneralRe: logg/scan open windows - important question! Pin
pat27088129-Jul-04 7:45
pat27088129-Jul-04 7:45 
GeneralRe: logg/scan open windows - important question! Pin
Heath Stewart29-Jul-04 9:09
protectorHeath Stewart29-Jul-04 9:09 
GeneralRe: logg/scan open windows - important question! Pin
pat27088129-Jul-04 10:27
pat27088129-Jul-04 10:27 
GeneralRe: logg/scan open windows - important question! Pin
Heath Stewart29-Jul-04 10:48
protectorHeath Stewart29-Jul-04 10:48 
GeneralRe: logg/scan open windows - important question! Pin
pat27088130-Jul-04 0:39
pat27088130-Jul-04 0:39 
GeneralRe: logg/scan open windows - important question! Pin
Heath Stewart30-Jul-04 3:43
protectorHeath Stewart30-Jul-04 3:43 
GeneralRe: logg/scan open windows - important question! Pin
pat27088130-Jul-04 4:58
pat27088130-Jul-04 4:58 
GeneralRe: logg/scan open windows - important question! Pin
Heath Stewart30-Jul-04 5:33
protectorHeath Stewart30-Jul-04 5:33 
pat270881 wrote:
know that the AutoResetEvent handles the communication between the threads and the delegate wrap methods or objects and passes them to other methods but between theorie and implementation is a little difference.

Actually, an AutoResetEvent has little or nothing to do with threads. It's a wait handle - like a mutex - that helps synchronize threads (it can be used - though pointlessly - without threads).

A delegate is a "managed function pointer". It references only a method and allows you to synchronously or asynchronously invoke a method. This is like a CALLBACK in native C/C++, except that delegates can reference methods (functions declared on a class) unlike callbacks (has to do with the address in memory).

I'm really not sure what the other example you posted is trying to accomplish. It seems to do nothing just by looking at it. The example I posted is very much what you need.

Here's an example I threw together:
using System;
using System.Runtime.InteropServices;
using System.Threading;

class Test
{
  static AutoResetEvent evt;
  static int count = 1;
  static void Main(string[] args)
  {
    if (args.Length > 0)
    {
      try
      {
        count = int.Parse(args[0]);
      }
      catch {}
    }
    
    evt = new AutoResetEvent(false);
    Timer t = new Timer(new TimerCallback(TimerCallback), null, 0, 5000);
    evt.WaitOne();
  }

  static void TimerCallback(object state)
  {
    Console.WriteLine("Enumerating windows at {0:t}...", DateTime.Now);
    EnumWindows(new EnumWindowsProc(EnumWindowsCallback), IntPtr.Zero);

    if (--count == 0) evt.Set();
  }

  static bool EnumWindowsCallback(IntPtr hWnd, IntPtr lParam)
  {
    if (hWnd != IntPtr.Zero)
    {
      string title = new string('\0', 260);
      int ret = GetWindowText(hWnd, title, 260);
      if (ret != 0)
        Console.WriteLine("0x{0:x8}: {1}", hWnd.ToInt64(),
          title.Substring(0, ret));
    }

    return true;
  }

  [DllImport("user32.dll")]
  static extern bool EnumWindows(EnumWindowsProc proc, IntPtr lParam);

  delegate bool EnumWindowsProc(IntPtr hWnd, IntPtr lParam);

  [DllImport("user32.dll", CharSet=CharSet.Auto)]
  static extern int GetWindowText(IntPtr hWnd, [Out] string title,
    int maxCount);
}


 

Microsoft MVP, Visual C#
My Articles
GeneralRe: logg/scan open windows - important question! Pin
pat27088130-Jul-04 6:21
pat27088130-Jul-04 6:21 
GeneralRe: logg/scan open windows - important question! Pin
Heath Stewart30-Jul-04 7:30
protectorHeath Stewart30-Jul-04 7:30 
GeneralRe: logg/scan open windows - important question! Pin
pat27088130-Jul-04 7:47
pat27088130-Jul-04 7:47 
GeneralRe: logg/scan open windows - important question! Pin
Heath Stewart30-Jul-04 8:04
protectorHeath Stewart30-Jul-04 8:04 
GeneralRe: logg/scan open windows - important question! Pin
pat27088130-Jul-04 9:17
pat27088130-Jul-04 9:17 
GeneralRe: logg/scan open windows - important question! Pin
Heath Stewart30-Jul-04 9:29
protectorHeath Stewart30-Jul-04 9:29 
GeneralRe: logg/scan open windows - important question! Pin
pat27088130-Jul-04 9:52
pat27088130-Jul-04 9:52 
GeneralRe: logg/scan open windows - important question! Pin
pat2708812-Aug-04 4:28
pat2708812-Aug-04 4:28 
GeneralError while deploying with the emulator Pin
mathon16-Aug-04 7:53
mathon16-Aug-04 7:53 
GeneralRe: logg/scan open windows - important question! Pin
pat27088129-Jul-04 5:43
pat27088129-Jul-04 5:43 
GeneralRe: logg/scan open windows - important question! Pin
Heath Stewart29-Jul-04 6:21
protectorHeath Stewart29-Jul-04 6:21 
GeneralA Comprehensive Logging Package for .NET Pin
pat27088128-Jul-04 5:40
pat27088128-Jul-04 5:40 
GeneralRe: A Comprehensive Logging Package for .NET Pin
Heath Stewart28-Jul-04 6:39
protectorHeath Stewart28-Jul-04 6:39 
GeneralRe: A Comprehensive Logging Package for .NET Pin
pat27088128-Jul-04 6:53
pat27088128-Jul-04 6:53 
GeneralRe: A Comprehensive Logging Package for .NET Pin
Heath Stewart28-Jul-04 6:59
protectorHeath Stewart28-Jul-04 6:59 
GeneralRe: A Comprehensive Logging Package for .NET Pin
Giles28-Jul-04 21:00
Giles28-Jul-04 21:00 
GeneralRe: A Comprehensive Logging Package for .NET Pin
pat27088128-Jul-04 22:26
pat27088128-Jul-04 22:26 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.