ArgumentException - Use of undefined keyword value 1 for event TaskScheduled in async
Asked Answered
C

3

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();
    }
Chladek answered 14/7, 2014 at 23:54 Comment(5)
I have removed, and reinstall, Windows Phone SDKs, Emulators and Visual Studio. After re installing everything the problem still persists !! please helpChladek
I've encountered the same problem - did you work it out?Comforter
There is a work around in the MSDN forum: Hack workaround until Microsoft addresses itChladek
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.Constrictive
@CarlosAlvarez, you may want to keep an eye on this, which is trying to deal with the same problem.Sweet
P
5

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

enter image description here

Pepin answered 9/8, 2014 at 20:28 Comment(3)
Why does it occur in the first place? What does it signify?Fredkin
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.Pepin
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.Comforter
G
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.

Godless answered 1/9, 2015 at 0:48 Comment(0)
C
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.

Carbylamine answered 21/6, 2015 at 2:27 Comment(3)
Where should I add this?Revere
IIRC, where you're using the Task, or just in main (where the exception bubbles through).Carbylamine
This doesn't seem to do anything.Brevier

© 2022 - 2024 — McMap. All rights reserved.