I am using the invoke() to call a delegate in order to communicate with another form in a different thread. The code is below.

You can see at the bottom (before the stars) where the thread is started. The method below that is being called by the delegate in the code below the stars. Each set of code is in a diff thread.

public class MainForm : System.Windows.Forms.Form
	{
          private void search()
          {

              Search ArtifactSearch = new ArtifactSearch(this);

              ArtifactSearch.SearchComplete += new  Search.ProcessingCompleteDelegate(Search_ProcessingComplete);
            
                Thread StegSearch = new Thread(new ThreadStart(ArtifactSearch.Start));

                StegSearch.Start();
}

private void Search_ProcessingComplete()
{
    //this method is being called by the delegate in the StegSearch thread above.
}

*********************************************************************************************

public class Search
	{
                MainForm Parent;

	        public event ProcessingCompleteDelegate SearchComplete;
		
		public Search(MainForm parent)
		{
			Parent = parent;
		}

		public void Start()
		{
                   /// Search would take place here

                   Parent.Invoke(SearchComplete,null);
                }
        }

My problem is that when i invoke the SearchComplete delegate it tells me that "SearchComplete" is not set to an instance of an object. But as you can see it clearly is set to a new instance before the thread is started. How do I fix this. I have used delegates in this manner in other areas of my code and it worked. I'm stumped.

Dani AI

Generated

The NullReferenceException indicates the event field is null at the moment it is being invoked — not that Invoke itself is broken. is right to point out the delegate declaration, but the more common causes here are a subscription that never happened on the same Search instance, an unsubscribe or race, or invoking the event directly from the worker thread without the usual safety pattern.

Troubleshooting checklist:

  • Confirm the same Search instance is used for subscription and for Start (log instance ids or GetHashCode from both places).
  • Make sure the subscription executes before the thread is started and that no code later removes the handler.
  • Verify the Parent reference is not null and that the delegate type matches the handler signature (as suggested).
  • Add short tracing inside the constructor, the subscription site, and at the top of Start to see the event field state.

A safer raising pattern avoids the null race and marshals the call to the UI thread. Copy the event to a local variable, check it, then use the control marshal to call it on the UI thread (BeginInvoke is nonblocking and preferred for simple notifications):

var handler = SearchComplete;
if (handler != null)
{
    Parent.BeginInvoke((MethodInvoker)delegate { handler(); });
}

This uses a local snapshot so the invocation list cannot become null between the check and the call, and it explicitly runs the handlers on the UI thread.

Longer-term alternatives: use BackgroundWorker, Task with TaskScheduler.FromCurrentSynchronizationContext, or post back via SynchronizationContext to avoid manual marshaling. For Windows Forms thread-safety guidance see the Microsoft docs on making thread-safe calls to controls and on .NET events: How to make thread-safe calls to Windows Forms controls and Events in .NET.

I don't see where you actually declared your delegate ?

somewhere in the namespace put this:

public delegate void ProcessingCompleteDelegate();

also to fire an event you usually do it like this:

if (SearchComplete != null) SearchComplete()

that code would go in your Start() method

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.