【问题标题】:App Doesn't Crash While Debugging, but Does When Running Normally应用程序在调试时不会崩溃,但在正常运行时会崩溃
【发布时间】:2015-03-31 21:47:04
【问题描述】:

系统信息

  • Windows 10 技术预览版(内部版本 9926)
  • Visual Studio 社区 2013

    尝试调试:
  • [AT&T] Lumia 635(适用于手机的 Windows 10 Technical Preview 版本 9941 w/ Lumia Cyan)
  • [AT&T] Lumia 1520(带有 Lumia Denim 和 PfD 的 Windows Phone 8.1)
  • [解锁] BLU Win Jr(Windows Phone 8.1 with PfD)
  • [Verizon] Lumia 图标(带有 Lumia Denim 和 PfD 的 Windows Phone 8.1)

我试图让定位服务在我的应用中运行。以前,我让 Visual Studio 抛出错误。这是一个带有消息“Use of undefined keyword value 1 for event TaskScheduled in async”的ArgumentException。谷歌搜索没有找到任何解决方案。

代码如下:

Geolocator Locator = new Geolocator();
Geoposition Position = await Locator.GetGeopositionAsync();
Geocoordinate Coordinate = Position.Coordinate;

当我可以得到要抛出的错误时,异常是在上面示例中的第 2 行或第 3 行抛出的。 我简化了原始代码以尝试修复它,但这是原始代码:

Geolocator Locator = new Geolocator();
Geocoordinate Coordinate = (await Locator.GetGeopositionAsync()).Position.Coordinate;

整个应用程序在调试时工作正常,但在其他情况下几乎瞬间崩溃。

这是一个Windows 8.1 Universal项目,专注于手机项目。

提前致谢


编辑:根据要求,这是完整的方法:

private static bool CheckConnection()
{
    ConnectionProfile connections = NetworkInformation.GetInternetConnectionProfile();
    bool internet = connections != null && connections.GetNetworkConnectivityLevel() == NetworkConnectivityLevel.InternetAccess;
    return internet;
}
public static async Task<double> GetTemperature(bool Force)
{
    if (CheckConnection() || Force)
    {
        Geolocator Locator = new Geolocator();
        await Task.Yield(); //Error occurs here
        Geoposition Position = await Locator.GetGeopositionAsync();
        Geocoordinate Coordinate = Position.Coordinate;
        HttpClient Client = new HttpClient();
        double Temperature;
        Uri u = new Uri(string.Format("http://api.worldweatheronline.com/free/v1/weather.ashx?q={0},{1}&format=xml&num_of_days=1&date=today&cc=yes&key={2}",
                                      Coordinate.Point.Position.Latitude,
                                      Coordinate.Point.Position.Longitude,
                                      "API KEY"),
                                      UriKind.Absolute);
        string Raw = await Client.GetStringAsync(u);
        XElement main = XElement.Parse(Raw), current_condition, temp_c;
        current_condition = main.Element("current_condition");
        temp_c = current_condition.Element("temp_C");
        Temperature = Convert.ToDouble(temp_c.Value);
        switch (Memory.TempUnit)
        {
            case 0:
                Temperature = Convertions.Temperature.CelsiusToFahrenheit(Temperature);
                break;
            case 2:
                Temperature = Convertions.Temperature.CelsiusToKelvin(Temperature);
                break;
        }
        return Temperature;
    }
    else
    {
        throw new InvalidOperationException("Cannot connect to the weather server.");
    }
}

编辑 2: 我已经 asked for help on Twitterreceived a reply 要求进行复制项目。我重新创建了原始应用程序的主要部分,但我无法得到错误。但是,您可能会出现错误so here's the project


编辑 3:如果它有帮助,这里是异常详细信息:

System.ArgumentException occurred
  _HResult=-2147024809
  _message=Use of undefined keyword value 1 for event TaskScheduled.
  HResult=-2147024809
  IsTransient=false
  Message=Use of undefined keyword value 1 for event TaskScheduled.
  Source=mscorlib
  StackTrace:
       at System.Diagnostics.Tracing.ManifestBuilder.GetKeywords(UInt64 keywords, String eventName)
  InnerException: 

【问题讨论】:

  • 您并不孤单,请查看thisthis。作为一种解决方法,看看这是否有帮助:Geoposition Position = await Task.Run(() =&gt; Locator.GetGeopositionAsync());
  • @Noseratio 我已经在你链接的两个线程中尽我所能。另外,我尝试了您提供的解决方法。它在调试时仍然可以完美运行,但在不调试时仍然会崩溃。感谢您的回复
  • 显然,这是一个 WP/WRT 错误,我会在connect.microsoft.com 报告它。顺便说一句,也试试这个:await Task.Yeild(); Geocoordinate Coordinate = (await Locator.GetGeopositionAsync()).Position.Coordinate;Task.Yeild()await Locator.GetGeopositionAsync() 之后崩溃吗?
  • 顺便说一句,我很感激你在你这个年纪处理这些复杂的事情,干得好:)
  • @Noseratio 我使用在Task.Yield()await Locator.GetGeopositionAsync() 之后弹出的 MessageDialogs 进行设置。我的猜测是它在Task.Yield() 崩溃,因为没有弹出对话框。同样,它只在不调试的情况下执行此操作。谢谢顺便说一句,哈哈:P

标签: c# geolocation async-await win-universal-app argumentexception


【解决方案1】:

检查了thisthis,我相信这是.NET async/await infrastructure for WinRT 中的一个错误。我无法重现它,但我鼓励您尝试以下解决方法,看看它是否适合您。

  • 将来自OnNavigatedTo 的所有异步等待调用分解为单独的async Task 方法,例如ContinueAsync:

    async Task ContinueAsync()
    {
        Geolocator Locator = new Geolocator();
        Geoposition Position = await Locator.GetGeopositionAsync();
        Geocoordinate Coordinate = Position.Coordinate; 
    
        // ...
    
        var messageDialog = new Windows.UI.Popups.MessageDialog("Hello");
        await messageDialog.ShowAsync();
    
        // ...
    }
    
  • OnNavigatedTo 中删除async 修饰符并从OnNavigatedTo 中调用ContinueAsync,如下所示:

    var scheduler = TaskScheduler.FromCurrentSynchronizationContext();
    Task.Factory.StartNew(
        () => ContinueAsync(), 
        CancellationToken.None, TaskCreationOptions.None, scheduler).
        Unwrap().
        ContinueWith(t => 
        {
            try
            {
                t.GetAwaiter().GetResult();
            }
            catch (Exception ex)
            {
                Debug.WriteLine(ex);
                throw; // re-throw or handle somehow
            }
        }, 
        CancellationToken.None,            
        TaskContinuationOptions.NotOnRanToCompletion, 
        scheduler);
    

如果有帮助,请告诉我们:)


已更新,显然,该错误存在于 TPL 日志记录提供程序 TplEtwProvider 中。如果您添加以下代码,您可以看到它正在创建。到目前为止,我找不到禁用此事件源的方法(直接或通过反射):

internal class MyEventListener : EventListener
{
    protected override void OnEventSourceCreated(EventSource eventSource)
    {
        base.OnEventSourceCreated(eventSource);
        if (eventSource.Name == "System.Threading.Tasks.TplEventSource")
        {
            var enabled = eventSource.IsEnabled();

            // trying to disable - unsupported command :(
            System.Diagnostics.Tracing.EventSource.SendCommand(
                eventSource, EventCommand.Disable, new System.Collections.Generic.Dictionary<string, string>());
        }
    }
}

// ...
public sealed partial class App : Application
{
    static MyEventListener listener = new MyEventListener();
}

【讨论】:

  • 感谢您的帮助,但我仍然无法正常工作。我不知道为什么会发生这种情况,而且我看到很多其他人也有这种情况。
  • 嗨,格雷格 - 我认为这是一个错误。当我知道更多时,我会归档并更新您。我不能保证修复的速度有多快。请在 OneDrive 上发布一个复制项目的链接,我将有一些东西转发给 PG。
  • @MattSmall,请查看我关于TplEtwProvider 事件源的更新。是否可以为 Windows Phone/Windows Store 应用完全禁用它?
  • @Noseratio 我已经添加了您提供的代码并添加了一些 Debug.WriteLines 以查看输出:pastebin.com/7JxbvyeC
  • 我会检查一下,但要到下周才能更新,因为我要离开办公室。
猜你喜欢
  • 1970-01-01
  • 2020-02-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-09-17
  • 2014-05-25
  • 1970-01-01
相关资源
最近更新 更多