Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
chiekku opened this issue Oct 20, 2020 · 1 comment

Comments

@chiekku
Copy link

chiekku commented Oct 20, 2020

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!

@Syntaxrabbit
Copy link

Same problem in TapiLine.cs

private async Task HandleNewCall(TapiCall call, int callPrivileges)
{
    if (NewCall != null)
    {
        Privilege priv = callPrivileges == NativeMethods.LINECALLPRIVILEGE_NONE ? Privilege.None :
            callPrivileges == NativeMethods.LINECALLPRIVILEGE_MONITOR ? Privilege.Monitor : Privilege.Owner;
        foreach (EventHandler<NewCallEventArgs> nc in NewCall.GetInvocationList())
        {
            //nc.BeginInvoke(this, new NewCallEventArgs(call, priv),
            //    delegate (IAsyncResult ar)
            //    {
            //        try
            //        {
            //            var nce = (EventHandler<NewCallEventArgs>)ar.AsyncState;
            //            nce.EndInvoke(ar);
            //        }
            //        catch
            //        {
            //        }
            //    }, nc);
            await Task.Run(() =>
            {
                try
                {
                    nc.Invoke(this, new NewCallEventArgs(call, priv));
                }
                catch
                {
                }
            });
        }
    }
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants