GeoCoordinateWatcher.TryStart(Boolean, TimeSpan) 方法

定義

啟動從目前位置提供者取得資料的作業。 這個方法會以同步方式傳回。

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 表示要顯示使用權限對話方塊。

timeout
TimeSpan

在位置提供者啟動逾時之前的等候時間 (以毫秒為單位)。

傳回

如果資料取得作業在 true 指定的時間週期內啟動,則為 false,否則為 timeout

實作

範例

下列範例示範如何呼叫 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

備註

這個方法會封鎖在 所 timeout指定的期間內執行呼叫線程。 從應用程式的使用者介面線程呼叫 TryStart 時,請小心。

適用於