参数异常 - 在异步操作中,使用了未定义的关键字值1来描述TaskScheduled事件。

20

出现System.ArgumentException错误 - 在异步API中针对TaskScheduled事件使用未定义的关键字值1。

在使用Visual Studio 2013 Update 3运行通用应用程序的第一个await语句时出现问题。

我在安装Visual Studio 2013 Update 3后,使用WP8.1 Universal和Silverlight应用程序。异常情况发生在模拟器/设备模式下。我花了几天时间研究这个问题,但没有得到任何解决方案。

我在Windows Dev Center论坛上有一篇相似文章,但我没有从微软那里听到任何答复。

代码很简单。一旦抛出内部异常,await就永远不会返回。是否还有其他人遇到异步相关的问题?有什么解决办法吗?

public async Task<StorageFolder> FolderExists(StorageFolder parent, string folderName)
{
    StorageFolder result = null;
    try
    {
        // Exception happens here. The code never returns so the thread hangs
        result = await parent.GetFolderAsync(folderName);
    }
    catch (Exception ex)
    {
        if (FeishLogger.Logger.IsDebug)
            ex.LogException(() => string.Format("FolderExists File: {0}\\{1}", parent.Path, folderName));
    }

    return result;
}

完整异常信息:

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: 

我有一个示例项目可用。创建一个 shell 通用应用程序并添加一些等待语句会使问题再次出现。

private async Task AsyncMethod()
{
    Debug.WriteLine("({0:0000} - Sync Debug)", Environment.CurrentManagedThreadId);

    // Uncomment this line to make it work
    //await Task.Delay(1);

    // Fails only if the line above is commented
    await Task.Run(() => Debug.WriteLine("({0:0000} - Async Debug)", Environment.CurrentManagedThreadId));
}

VS错误

以下是完整的OnLaunched代码,其中调用了AsyncMethod

   protected override async void OnLaunched(LaunchActivatedEventArgs e)
    {
      #if DEBUG
        if (System.Diagnostics.Debugger.IsAttached)
        {
            this.DebugSettings.EnableFrameRateCounter = true;
        }
      #endif

        Frame rootFrame = Window.Current.Content as Frame;

        // Do not repeat app initialization when the Window already has content,
        // just ensure that the window is active
        if (rootFrame == null)
        {
            // Create a Frame to act as the navigation context and navigate to the first page
            rootFrame = new Frame();

            // TODO: change this value to a cache size that is appropriate for your application
            rootFrame.CacheSize = 1;

            if (e.PreviousExecutionState == ApplicationExecutionState.Terminated)
            {
                // TODO: Load state from previously suspended application
            }

            // Place the frame in the current Window
            Window.Current.Content = rootFrame;
        }

        if (rootFrame.Content == null)
        {
         #if WINDOWS_PHONE_APP
            // Removes the turnstile navigation for startup.
            if (rootFrame.ContentTransitions != null)
            {
                this.transitions = new TransitionCollection();
                foreach (var c in rootFrame.ContentTransitions)
                {
                    this.transitions.Add(c);
                }
            }

            rootFrame.ContentTransitions = null;
            rootFrame.Navigated += this.RootFrame_FirstNavigated;
         #endif

            await AsyncMethod();

            await AsyncMethods();
            await AsyncMethods();
            await AsyncMethods();


            // When the navigation stack isn't restored navigate to the first page,
            // configuring the new page by passing required information as a navigation
            // parameter
            if (!rootFrame.Navigate(typeof(MainPage), e.Arguments))
            {
                throw new Exception("Failed to create initial page");
            }
        }

        // Ensure the current window is active
        Window.Current.Activate();
    }

我已经卸载并重新安装了Windows Phone SDK、模拟器和Visual Studio。重新安装后,问题仍然存在!!请帮忙。 - Carlos A
我遇到了同样的问题 - 你解决了吗? - Reuben Bond
1
在 MSDN 论坛中有一个解决方法:Hack workaround until Microsoft addresses it - Carlos A
1
我不知道这是否相关,但是当我从Debug -> Exceptions开始捕获所有异常时,我立即开始看到了这个问题。一旦ArgumentException被捕获后,我点击继续几次,一切都正常了。在从Debug -> Exceptions取消勾选捕获所有异常之后,这个问题就消失了。 - sohum
@CarlosAlvarez,你可能想关注一下这个问题:https://dev59.com/SYfca4cB1Zd3GeqPnssr,它试图解决相同的问题。 - noseratio - open to work
3个回答

5
异常可以忽略。只需点击播放按钮即可。或者您可以在“调试”->“异常”菜单中禁用异常断点。

为什么首先会发生这种情况?它代表了什么意义? - Factor Mystic
框架在内部抛出异常。只要它没有暴露给用户代码,你就不必关心它为什么会发生。Visual Studio 应该有一个调试选项,只在用户代码抛出异常时中断。 - Andras Csehi
8
这是不可接受的。每次 Visual Studio 停止执行以显示此异常时,都会浪费很多烦人的时间。在框架中不要太随意地抛出异常。 - Reuben Bond

1
问题仍然存在,并且异常本身可以被忽略,就像其他答案所说的那样。但是,如果异步操作被try/catch包围,则异常将被捕获,并且同一try/catch括号中的其他操作将不会被执行,这就是问题所在。
只需在异步操作之前放置此代码即可改善情况。
try
{
    await Task.Delay(1);
}
catch
{
    // do nothing
}

在Task.Delay(1)上仍然会发生异常,但在此之后,随后的异步操作不会再出现异常。


0

我通过添加解决了这个问题

using System.Runtime.InteropServices.WindowsRuntime;

请勿删除此行。自动“排序并删除使用”将(在我的情况下)删除它,导致这个神秘的问题。不,我不知道为什么。但我知道这是原因。


我应该把这个添加到哪里? - seb
如果我没记错的话,你是在使用 Task,还是在主函数中(异常从这里冒出来)。 - Andy
这似乎没有做任何事情。 - Mihai Damian

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接