GeoCoordinateWatcher.TryStart(Boolean, TimeSpan) 方法

定义

开始从当前位置提供程序获取数据。Initiates the acquisition of data from the current location provider. 此方法同步返回。This method returns synchronously.

public:
 virtual bool TryStart(bool suppressPermissionPrompt, TimeSpan timeout);
public bool TryStart (bool suppressPermissionPrompt, TimeSpan timeout);
abstract member TryStart : bool * TimeSpan -> bool
override this.TryStart : bool * TimeSpan -> bool
Public Function TryStart (suppressPermissionPrompt As Boolean, timeout As TimeSpan) As Boolean

参数

suppressPermissionPrompt
Boolean

如果为 true,则禁止显示权限对话框;如果为 false,则显示权限对话框。true to suppress the permission dialog box; false to display the permission dialog box.

timeout
TimeSpan

超时前等待位置提供程序启动的时间(以毫秒为单位)。Time in milliseconds to wait for the location provider to start before timing out.

返回

Boolean

如果在 timeout 指定的时间内开始数据获取,则为 true ;否则为 falsetrue if data acquisition is started within the time period specified by timeout; otherwise, false.

实现

示例

下面的示例演示如何调用 TryStartThe following example demonstrates how to call TryStart.

using System;
using System.Device.Location;

namespace GetLocationProperty
{
    class Program
    {
        static void Main(string[] args)
        {
            GetLocationProperty();
        }

        static void GetLocationProperty()
        {
            GeoCoordinateWatcher watcher = new GeoCoordinateWatcher();

            // Do not suppress prompt, and wait 1000 milliseconds to start.
            watcher.TryStart(false, TimeSpan.FromMilliseconds(1000));

            GeoCoordinate coord = watcher.Position.Location;

            if (coord.IsUnknown != true)
            {
                Console.WriteLine("Lat: {0}, Long: {1}",
                    coord.Latitude,
                    coord.Longitude);
            }
            else
            {
                Console.WriteLine("Unknown latitude and longitude.");
            }
        }
    }
}
Imports System.Device.Location

Module GetLocationProperty
    Public Sub GetLocationProperty()
        Dim watcher As New System.Device.Location.GeoCoordinateWatcher()
        watcher.TryStart(False, TimeSpan.FromMilliseconds(1000))

        Dim coord As GeoCoordinate = watcher.Position.Location

        If coord.IsUnknown <> True Then
            Console.WriteLine("Lat: {0}, Long: {1}", coord.Latitude, coord.Longitude)
        Else
            Console.WriteLine("Unknown latitude and longitude.")
        End If
    End Sub

    Public Sub Main()
        GetLocationProperty()
        Console.ReadLine()
    End Sub

End Module

注解

此方法在指定的时间段内阻止执行调用线程 timeoutThis method blocks execution of the calling thread during the time period specified by timeout. TryStart从应用程序的用户界面线程调用时请务必小心。Use caution when calling TryStart from the user interface thread of your application.

适用于