20

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();
    }
Carlos A
  • 231
  • 2
  • 6
  • I have removed, and reinstall, Windows Phone SDKs, Emulators and Visual Studio. After re installing everything the problem still persists !! please help – Carlos A Jul 18 '14 at 10:13
  • I've encountered the same problem - did you work it out? – Reuben Bond Jul 25 '14 at 04:29
  • 1
    There is a work around in the MSDN forum: [Hack workaround until Microsoft addresses it](http://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) – Carlos A Jul 25 '14 at 10:58
  • 1
    I don't know if this is related, but I started seeing this issue as soon as I turned on catching of all exceptions from Debug -> Exceptions. Once I hit continue a few times after the ArgumentException was caught, everything worked fine. After unchecking the catching of all exceptions from Debug -> Exceptions, this problem has gone away. – sohum Aug 05 '14 at 03:08
  • @CarlosAlvarez, you may want to keep an eye on [this](http://stackoverflow.com/q/28472872), which is trying to deal with the same problem. – noseratio Feb 12 '15 at 15:24

3 Answers3

5

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

enter image description here

Andras Csehi
  • 4,305
  • 1
  • 28
  • 36
  • Why does it occur in the first place? What does it signify? – Factor Mystic Sep 11 '14 at 01:08
  • The framework throws exceptions internally. As long as it is not surfaced to the user code you should be not concerned why it is happening. Visual Studio should have a debug option where it only breaks on user code thrown exception. – Andras Csehi Sep 14 '14 at 11:00
  • 8
    This is not acceptable. It wastes an annoying amount of time every time VS has to halt execution to display this exception. Don't be so throw happy in the framework. – Reuben Bond Sep 30 '14 at 03:47
1

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.

Takumi
  • 11
  • 2
0

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.

Andy
  • 255
  • 2
  • 9