Roy

Roy

  • NA
  • 6
  • 0

Delegate throwing Null Exception

Jun 13 2006 1:28 PM

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

<code>

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.