线程 - 确保线程完成 [英] Threading - make sure the thread finishes

查看:27
本文介绍了线程 - 确保线程完成的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

已解决:http://msdn.microsoft.com/en-us/library/ff431782(v=VS.92).aspx

我有以下课程,它将为我提供 WP7 中的当前位置:

I have the following class that will give me the current location in WP7:

public class Position
{
    private GeoCoordinateWatcher watcher = null;
    public GeoCoordinate CurrentLocation { get; set; }

    public Position()
    {
        ObtainCurrentLocation();
    }

    private void ObtainCurrentLocation()
    {
        watcher = new GeoCoordinateWatcher(GeoPositionAccuracy.Default);
        watcher.PositionChanged += new EventHandler<GeoPositionChangedEventArgs<GeoCoordinate>>(watcher_PositionChanged);
        watcher.Start();
    }

    void watcher_PositionChanged(object sender, GeoPositionChangedEventArgs<GeoCoordinate> e)
    {
        //stop & clean up, we don't need it anymore
        watcher.Stop();
        watcher.Dispose();
        watcher = null;

        CurrentLocation = e.Position.Location;
    }

}

我想用它来获取位置.所以我要做的是实例化它.当我调用 CurrentLocation 属性时,如何确保已获取位置?

I want to use it to get the location. So what I do is to instantiate it. How can I make sure that, when I call the CurrentLocation property, the location whould have been acquired?

推荐答案

你可以让你的 Position 类实现 INotifyPropertyChanged 并引发 PropertyChanged 当 CurrentLocation 属性的值发生变化时.

You can make your Position class implement INotifyPropertyChanged and raise PropertyChanged when the value of the CurrentLocation property has changed.

依赖 CurrentLocation 的代码将监听 Position.PropertyChanged 并在引发事件时采取适当的行动.

Code that depends on CurrentLocation will then listen for Position.PropertyChanged and act appropriately when the event is raised.

这篇关于线程 - 确保线程完成的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆