Re: a few issues with events

Tech Tip: Click here to run a free scan for Windows Errors and optimize PC performance



Thanks for the response, Control.Invoke very definitely locks on
"this." If you open it with the reflector you'll see this:

public object Invoke(Delegate method, params object[] args)
{
using (MultithreadSafeCallScope scope = new
MultithreadSafeCallScope())
{
return this.FindMarshalingControl().MarshaledInvoke(this,
method, args, true);
}
}

private Control FindMarshalingControl()
{
lock (this)
{
Control parentInternal = this;
while ((parentInternal != null) && !
parentInternal.IsHandleCreated)
{
parentInternal = parentInternal.ParentInternal;
}
if (parentInternal == null)
{
parentInternal = this;
}
return parentInternal;
}
}

private object MarshaledInvoke(Control caller, Delegate method,
object[] args, bool synchronous)
{
int num;
if (!this.IsHandleCreated)
{
throw new
InvalidOperationException(SR.GetString("ErrorNoMarshalingThread"));
}
if (((ActiveXImpl) this.Properties.GetObject(PropActiveXImpl)) !=
null)
{
IntSecurity.UnmanagedCode.Demand();
}
bool flag = false;
if ((SafeNativeMethods.GetWindowThreadProcessId(new
HandleRef(this, this.Handle), out num) ==
SafeNativeMethods.GetCurrentThreadId()) && synchronous)
{
flag = true;
}
ExecutionContext executionContext = null;
if (!flag)
{
executionContext = ExecutionContext.Capture();
}
ThreadMethodEntry entry = new ThreadMethodEntry(caller, method,
args, synchronous, executionContext);
lock (this)
{
if (this.threadCallbackList == null)
{
this.threadCallbackList = new Queue();
}
}
lock (this.threadCallbackList)
{
if (threadCallbackMessage == 0)
{
threadCallbackMessage =
SafeNativeMethods.RegisterWindowMessage(Application.WindowMessagesVersion
+ "_ThreadCallbackMessage");
}
this.threadCallbackList.Enqueue(entry);
}
if (flag)
{
this.InvokeMarshaledCallbacks();
}
else
{
UnsafeNativeMethods.PostMessage(new HandleRef(this,
this.Handle), threadCallbackMessage, IntPtr.Zero, IntPtr.Zero);
}
if (!synchronous)
{
return entry;
}
if (!entry.IsCompleted)
{
this.WaitForWaitHandle(entry.AsyncWaitHandle);
}
if (entry.exception != null)
{
throw entry.exception;
}
return entry.retVal;
}





.



Relevant Pages

  • Re: How to do proper locking
    ... >> actually free the object until the refcount reaches zero. ... >> callback function, either directly, or like a pointer to this structure. ... file-descriptor of the current thread while holding some global lock. ... "dev" should get a copy of "args" while holding a global lock. ...
    (freebsd-hackers)
  • Re: Safely Firing an Event
    ... I would recommend releasing the lock before invoking the event - ... private void FireMyEvent(MyEventArgs args) ... EventHandler handler; ...
    (microsoft.public.dotnet.languages.csharp)