应用程序在调试时不会崩溃,但在正常运行时会崩溃

2024-03-25

系统信息

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

    尝试调试:
  • [美国电话电报公司]Lumia 635(Windows 10 技术预览版,适用于版本 9941 的手机,带有 Lumia Cyan)
  • [美国电话电报公司]Lumia 1520(带有 Lumia Denim 和 PfD 的 Windows Phone 8.1)
  • [解锁]BLU Win Jr(带 PfD 的 Windows Phone 8.1)
  • [威瑞森]Lumia 图标(带有 Lumia Denim 和 PfD 的 Windows Phone 8.1)

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

这是代码:

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

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

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

整个应用程序在调试时可以正常工作,但在其他情况下几乎会立即崩溃。

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

提前致谢


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

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.");
    }
}

EDIT 2: I've 在 Twitter 上寻求帮助 https://twitter.com/devgregw/status/564804691695919104, and 收到回复 https://twitter.com/WinDevMatt/status/564872023139024897要求重现项目。我重新创建了原始应用程序的主要部分,但我无法收到错误。但是,您可能会遇到错误.


编辑 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: 

经检查this https://stackoverflow.com/questions/24747885/argumentexception-use-of-undefined-keyword-value-1-for-event-taskscheduled-in and this https://social.msdn.microsoft.com/Forums/windowsapps/en-US/3e505e04-7f30-4313-aa47-275eaef333dd/systemargumentexception-use-of-undefined-keyword-value-1-for-event-taskscheduled-in-async?forum=wpdevelop,我相信这是一个错误.NET async/awaitWinRT 的基础架构 http://blogs.msdn.com/b/windowsappdev/archive/2012/04/24/diving-deep-with-winrt-and-await.aspx。我无法重现它,但我鼓励您尝试以下解决方法,看看它是否适合您。

  • 分解出所有异步等待调用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();
    
        // ...
    }
    
  • Remove async修饰符来自OnNavigatedTo并打电话ContinueAsync from OnNavigatedTo像这样:

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

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


Updated显然,该错误存在于 TPL 日志记录提供程序中的某个位置,TplEtwProvider http://referencesource.microsoft.com/#mscorlib/system/threading/Tasks/TPLETWProvider.cs,e38cada0f78eafb7。如果添加以下代码,您可以看到它正在创建。到目前为止,我找不到禁用此事件源的方法(直接或通过反射):

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();
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

应用程序在调试时不会崩溃,但在正常运行时会崩溃 的相关文章

随机推荐