繁体   English   中英

如何引发跨线程事件

[英]How to raise cross thread event

如何从另一个线程中引发事件GeocodeAddressEventHandler?

    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; 
    }

实际上,事实证明,在委托的范围终止后,该线程正在终止。 这可能是一种愚蠢的方式,但我在该范围内放置了一段时间Queue.empty {sleep},因此它永远不会终止,然后我从另一个线程启动了LoadMap,这样它就会堵塞我的WCF服务等待无休止的队列终止。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM