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.

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.