Comment augmenter la croix fil des événements
Comment puis-je augmenter l'événement GeocodeAddressEventHandler partir d'un autre 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;
}
En fait il s'est avéré que le thread a été appelé à disparaître après le champ d'application de la délégué résilié. Ce pourrait être un idiot de le faire, mais j'ai mis un certain temps d'Attente.vide { sommeil } que dans le champ d'application il n'a jamais mis fin, puis j'ai lancé le LoadMap à partir d'un autre thread, afin qu'il la confiture de mon service WCF en attente pour l'interminable file d'attente à la fin.
OriginalL'auteur bluepoisondartfrog | 2011-12-08
Vous devez vous connecter pour publier un commentaire.
Prendre un coup d'oeil à
http://www.codeproject.com/KB/cs/Cross_thread_Events.aspx
Voir aussi classe BackgroundWorker : http://msdn.microsoft.com/en-us/library/system.componentmodel.backgroundworker.aspx
OriginalL'auteur Bogdan M.
Prendre un coup d'oeil à la
Invoke()
méthode:http://msdn.microsoft.com/library/zyzhdc6b.aspx
Control.Invoke()
ne fonctionne que si vous voulez que le thread d'INTERFACE utilisateur pour exécuter la méthode.OriginalL'auteur CodeZombie