4

How can I raise the the event GeocodeAddressEventHandler from another thread?

    System.Threading.Thread MapThread;
    WinformMap map ;

    public delegate void GeocodeAddressEventHandler(object sender, EventArgs e);
    public event GeocodeAddressEventHandler GeocodeAddressEvent;

    //How to Raise this Event from another thread??
    public void GeocodeAddress_Raised(object sender, EventArgs e)
    {
        MapLib.GeocodeAddress("12798 1ST ST", "", "", "");
    }

    public bool LoadMap(string restorepoint)
    {
        ////////////////////////Engine Controls////////////////////////////////////////////////////////
        try
        {
            System.ServiceModel.OperationContext context = System.ServiceModel.OperationContext.Current;

            //This is to instantiate a winform from a Console (will convert to service shortly) 
            MapThread = new System.Threading.Thread(new System.Threading.ThreadStart(delegate
            {
                using (System.ServiceModel.OperationContextScope scope = new System.ServiceModel.OperationContextScope(context))
                {
this.GeocodeAddressEvent += new GeocodeAddressEventHandler(GeocodeAddress_Raised);
                 }
            }));
            MapThread.SetApartmentState(System.Threading.ApartmentState.STA);
            MapThread.Start();
            MapThread.Join();
        }
        catch (Exception ex)
        {
            return false;
        }

        return true; 
    }

Actually it turned out that the thread was terminating after the scope of the delegate terminated. This might be a dumb way to do it, but I put a while Queue.empty { sleep } in that scope so it never terminated, then I launched the LoadMap from yet another thread, so that it jam up my WCF service waiting for the neverending queue to terminate.

patrick
  • 16,091
  • 29
  • 100
  • 164

2 Answers2

3

Take a look at the Invoke() method:

http://msdn.microsoft.com/library/zyzhdc6b.aspx

CodeZombie
  • 5,367
  • 3
  • 30
  • 37