Re: Delegate.BeginInvoke and EndInvoke.

Fabian Schmied <[email protected]> Wed, 16 Jan 2008 10:12:22 +0100
Newsgroups gmane.comp.windows.devel.dotnet.winforms
Message-ID <[email protected]>
> Well then I don't see how this approach helps me out then. My motivation
> for doing this was to not get tied up when my call to
> SystemParametersInfo doesn't return as per my previous thread.

Amit, your actual problem is that of SystemParametersInfo not
returning. You should really look into that rather than trying to work
around it. As Peter said: Call product support to find out what's
happening there. SystemParametersInfo is not supposed to hang
infinitely. Terminating the thread that called it might cure the
symptom, but you definitely have a bigger problem.

(As a side note, I don't really know much about SystemParametersInfo,
but maybe you should ask at a Win32 forum as well - somebody might
have had this issue before. There's also this posting by Raymond Chen:
http://blogs.msdn.com/oldnewthing/archive/2005/03/10/392118.aspx - he
also talks about the WM_SETTINGCHANGE message; but it doesn't have to
be your own window that is blocking, it might be a different one.)

Regarding your workaround: yes, you need to call EndInvoke when
invoking delegates asynchronously. And if SystemParametersInfo blocks
infinitely, the approach with AsyncCallback calling EndInvoke won't do
that, either.

You could try to spawn your own thread and abort it after a certain
amount of time, but without knowing why/where SystemParametersInfo is
blocking, there's no way of telling whether Abort will actually be
able to terminate the thread. IMO, you should try to resolve the root
cause instead, otherwise you'll end up leaking background threads.

Best regards,
Fabian