none
c# - unexpected time error for GlobalSystemMediaTransportControlsSessionManager RRS feed

  • Question

  • I'm trying to get the current playing song with GlobalSystemMediaTransportControlsSessionManager base on https://stackoverflow.com/questions/57580053/using-systemmediatransportcontrols-to-get-current-playing-song-info-from-other-a/63099881#answer-63099881

    but when i run this i get this error:

    Unhandled Exception: System.InvalidOperationException: A method was called at an unexpected time.
    A method was called at an unexpected time.
    at Windows.Foundation.IAsyncOperation`1.GetResults() at ConsoleApp1.Program.Main(String[] args) in C:\Users\hamid\source\repos\ConsoleApp1\Program.cs:line 10

    I have also removed GetAwaiter() because it could't find it.

    this is my code:

    using System;
    using Windows.Media.Control;

    namespace ConsoleApp1
    {
        class Program
        {
            static void Main(string[] args)
            {
                var gsmtcsm = GlobalSystemMediaTransportControlsSessionManager.RequestAsync().GetResults().GetCurrentSession();
                var mediaProperties = gsmtcsm.TryGetMediaPropertiesAsync().GetResults();
                Console.WriteLine("{0} - {1}", mediaProperties.Artist, mediaProperties.Title);
            }
        }
    }
    What is the problem?

    Thursday, November 19, 2020 5:24 PM

All replies

  • Hi hamidreza-B,

    Thank you for posting here.

    This error is because the asynchronous task has not been completed, but you are already trying to get its result.

    In order to wait for the completion of the asynchronous task, we generally use the await keyword, or use the GetAwaiter() extension method as in that link.

    The code in the link is located in a UWP program, so it can be compiled, but you seem to use UWP API in the Console App, so there may be some lack of references that cause exceptions.

    If you need a UWP application, please ask for help in Microsoft Q&A.

    If not, please describe your problem in detail. Maybe someone can provide you with a new idea.

    Best Regards,

    Timon


    "Visual c#" forum will be migrating to a new home on Microsoft Q&A ! We invite you to post new questions in the "Developing Universal Windows apps" forum’s new home on Microsoft Q&A ! For more information, please refer to the sticky post.

    Friday, November 20, 2020 5:12 AM
  • Hi Timon!

    thank you for your reply.

    I asked this question in stackoverflow, github and here and you are the only person who answered, so i really appreciate your response.

    I'm working on an electron project and in that project i have made my own taskbar.

    For that taskbar i want to implement a feature that shows the current playing media(title, artist and etc).

    I'm making a library for the taskbar in c#, if i can make it work in console application i can also do that for a dll file.

    Is there anyway to do this in console application?


    • Edited by Hamidreza-B Sunday, November 22, 2020 2:11 PM
    Sunday, November 22, 2020 4:47 AM
  • Hi,

    I added three references to the console app:

    C:\Program Files (x86)\Windows Kits\10\UnionMetadata\10.0.18362.0\Windows.winmd

    C:\Program Files (x86)\Windows Kits\10\References\10.0.18362.0\Windows.Foundation.FoundationContract\3.0.0.0\Windows.Foundation.FoundationContract.winmd

    C:\Program Files (x86)\Windows Kits\10\References\10.0.18362.0\Windows.Foundation.UniversalApiContract\8.0.0.0\Windows.Foundation.UniversalApiContract.winmd

    Then the code passed the compilation, but I haven't actually applied the code, so I don't know if it is enough, you need to try it yourself.

    In addition, you need to install the UWP workload in Visual Studio to find these packages.

    Best Regards,

    Timon


    "Visual C#" forum will be migrating to a new home on Microsoft Q&A ! We invite you to post new questions in the "Developing Universal Windows apps" forum’s new home on Microsoft Q&A ! For more information, please refer to the sticky post.

    8 hours 43 minutes ago