Tworze zdarzenie WaitEventHandler, którego uchwyt przekazuje funkcji NotifyAddrChange. Zdarzenie jest sygnalizowane gdy w systemie zmieni się adres IP.
const UInt32 ERROR_IO_PENDING = 997;
[StructLayout(LayoutKind.Explicit, Size = 20)]
public struct OVERLAPPED
{
[FieldOffset(0)] public uint Internal;
[FieldOffset(4)] public uint InternalHigh;
[FieldOffset(8)] public uint Offset;
[FieldOffset(12)] public uint OffsetHigh;
[FieldOffset(8)] public IntPtr Pointer;
[FieldOffset(16)] public IntPtr hEvent;
}
[DllImport("Iphlpapi.dll")]
static extern UInt32 NotifyAddrChange(out IntPtr handle, ref OVERLAPPED overlapped);
//...
OVERLAPPED overlap = new OVERLAPPED();
UInt32 ret;
IntPtr hand = IntPtr.Zero;
EventWaitHandle eventWaitHandle = new EventWaitHandle(false, EventResetMode.AutoReset);
overlap.hEvent = eventWaitHandle.SafeWaitHandle.DangerousGetHandle();
ret = NotifyAddrChange(out hand, ref overlap);
if (ret != ERROR_IO_PENDING)
MessageBox.Show(String.Format("Error {0}", ret));
else
{
eventWaitHandle.WaitOne();
MessageBox.Show("IP Changed");
}
Metoda WaitOne zablokuje wątek (główny). A ja chciałbym nie blokować wątku głównego i żeby po zajściu tego zdarzenia został asynchronicznie wywołany callback w kontekście wątku głównego. I nie wiem czego użyć za bardzo :/