Forum Xamarin.Mac

Announcement:

The Xamarin Forums have officially moved to the new Microsoft Q&A experience. Microsoft Q&A is the home for technical questions and answers at across all products at Microsoft now including Xamarin!

To create new threads and ask questions head over to Microsoft Q&A for .NET and get involved today.

Why does NSPasteboard run so slowly in TcpListener's async task?

BycRealBycReal Member ✭✭
edited July 2020 in Xamarin.Mac

I have a app(webview + xamarin) which support websocket server.
But when I use chrome to debug js, the server side is so slow. I simplify the code as follows:

            Task.Run(async () =>
            {
                var tcpListener = new TcpListener(IPAddress.Any, 50505);
                tcpListener.Start();
                while (true)
                {
                    var tcpClient = await tcpListener.AcceptTcpClientAsync();
                    NSRunLoop.Main.BeginInvokeOnMainThread(() =>
                    {
                        var sw = new Stopwatch();
                        sw.Start();
                        var text = NSPasteboard.GeneralPasteboard.GetStringForType(NSPasteboard.NSStringType);
                        sw.Stop();
                        Console.WriteLine("ElapsedMs: " + sw.ElapsedMilliseconds);
                    });
                }
            }).ConfigureAwait(false);

Then I create a test html file contains websocket client js code.

  1. From terminal, run TcpListenerTest app.
  2. Open chrome, maximum it( let TcpListenerTest app to background).
  3. In chrome, open a web page, and copy some words(abount 100 words).
  4. Wait about 5 minutes.
  5. In chrome, open the test html file.
  6. Wait about 10 seconds and turn to terminal to see the output.

In most case, ElapsedMs is about 10,000( 10 seconds).

Only the first time after restart computer, it will occur.

Best Answer

Answers

  • BycRealBycReal Member ✭✭

    I write a xcode+objc project, it has same problem.
    So it should not be the bug of xamarin.mac.
    And it may be priority of background ui process.

  • BycRealBycReal Member ✭✭
    Accepted Answer

    It is because of NAP of macOS.
    See Disable App Nap for Mono Mac application

  • ChrisHamonsChrisHamons USForum Administrator, Xamarin Team Xamurai

    Great idea in testing against ObjC version - that's one of my favorite tactics.

    Glad you were able to pin this down.

Sign In or Register to comment.