Skip to content

.NET Core support: "System.PlatformNotSupportedException" for delegate BeginInvoke #34

@chiekku

Description

@chiekku

I can make a call...BUT I get an exception:

Note: the call continues in the background while this exception has stopped execution of the program

Looks like if I want to use .NET Core, I have to change how the async calls are done:

https://devblogs.microsoft.com/dotnet/migrating-delegate-begininvoke-calls-for-net-core/

The fix appears to be relatively simple. Here are the necessary changes:

async public Task<bool> Initialize()
{
    ...
    
    /*
    _workerThread = new Thread(ProcessTapiMessages)
    {
        Name = "Tapi Message Processor",
        IsBackground = true,
    };
    _workerThread.Start();
    */

    var _workerTask = new Task(async () =>
    { 
        await ProcessTapiMessages();
    });
    
    _workerTask.Start();
    ...
}

async private Task ProcessTapiMessages()
{
    ...

    /*
    ptmCb.BeginInvoke(msg, ar =>
                        {
                            try
                            {
                                ptmCb.EndInvoke(ar);
                            }
                            catch (Exception ex)
                            {
                                Trace.WriteLine("TAPI message exception: " + ex.Message);
                            }
                        }, null);
    */

     // invoke and await delegate
        await Task.Run(() =>
        {
            try
            {
                ptmCb.Invoke(msg);
            }
            catch (Exception ex)
            {
                Trace.WriteLine("TAPI message exception: {0}", ex.Message);
            }
        });
    ...
}
                 

in TapiManager.cs

Now I no longer get the exception, and the call continues normally. Both the async changes were required, or else the UI would hang.

This fix should be compatible with existing applications, and now it supports .NET Core!

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions