ArgumentException - Use of undefined keyword value

2019-04-18 04:49发布

Getting System.ArgumentException - Use of undefined keyword value 1 for event TaskScheduled in async apis.

There is something wrong when running the first await statement in an Universal app with Visual Studio 2013 Update 3.

I am using WP8.1 Universal and silverlight apps after I installed Visual Studio 2013 Update 3.

The exceptions happens in Emulator/Device modes. I have spent a couple of days researching this issue without any resolution.

I have a sibling article at Windows Dev center forum but I have not heard any answers from Microsoft.

The code is straight forward. Once the internal exception is thrown, the await never returns.

Is anyone else having these issues with async ?? resolution ?

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

Full exception:

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: 

I have a sample project available. Creating a shell Universal App and adding some await statement makes the problem reocur.

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 error

Here is the full OnLaunched code with calls to 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();
    }

3条回答
神经病院院长
2楼-- · 2019-04-18 05:03

The exception can be ignored. Just hit play. Alternatively you can disable break on exceptions in debug -> exceptions menu.

enter image description here

查看更多
Emotional °昔
3楼-- · 2019-04-18 05:11

The issue still exists, and the exception itself can be ignored as other answer said. But if the async operation is enclosed by try/catch, the exception would be caught and other operation in same try/catch brace would not be executed, that's the problem.

Just putting this before async operation would get better this.

try
{
    await Task.Delay(1);
}
catch
{
    // do nothing
}

Exception still occurs on Task.Delay(1), but after that it wouldn't occur on following async operation.

查看更多
别忘想泡老子
4楼-- · 2019-04-18 05:26

I solved this by adding

using System.Runtime.InteropServices.WindowsRuntime;

Do not remove this line. The automatic "sort and remove usings" will (in my case) remove it, causing this cryptic issue. No, I don't know why. But I know it's the cause.

查看更多
登录 后发表回答