How to raise a cross thread event

How can I raise a GeocodeAddressEventHandler event 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; } 

In fact, it turned out that the flow stopped after the delegation area was terminated. This might be a dumb way to do this, but I put Queue.empty {sleep} for a while in this area so that it never ends, then I started LoadMap from another thread, so it started my WCF service, waiting for an endless queue for completion.

+4
source share

Source: https://habr.com/ru/post/1385356/


All Articles