Quantcast
Channel: Xamarin.Mac — Xamarin Community Forums
Viewing all articles
Browse latest Browse all 1429

Repeating Task.Delay causes a large delay

$
0
0

I'm developing Xamarin.Mac(Cocoa) using C#. I want to develop application waiting seconds sometimes, so I developed waiting function using Task.Delay.
But repeating Task.Delay causes a large delay.

This is a test code for Cocoa.

using System;
using System.Threading.Tasks;
using System.Threading;
using AppKit;
using Foundation;

namespace TaskDelayTest
{
    public partial class ViewController : NSViewController
    {
        public ViewController(IntPtr handle) : base(handle)
        {
        }

        public override void ViewDidLoad()
        {
            base.ViewDidLoad();
        }

        public override NSObject RepresentedObject
        {
            get
            {
                return base.RepresentedObject;
            }
            set
            {
                base.RepresentedObject = value;
            }
        }

        async partial void Execute(AppKit.NSButton sender)
        {
            while (true)
            {
                int millisecond = 1000;
                Console.WriteLine($"WaitTime : {millisecond}");
                var startDate = DateTime.Now;
                await Task.Delay(millisecond).ConfigureAwait(false);

                var endDate = DateTime.Now;
                var diff = (endDate - startDate);
                Console.WriteLine($"WaitEnd   : {endDate.ToString("yyyy/MM/dd HH:mm:ss.fff")}");
                Console.WriteLine($"DiffTime : {diff.TotalMilliseconds}");
            }
        }
    }
}

Console result:
.
.
.
(Repeating a lot of times)
.
.
.
WaitTime : 1000 WaitEnd : 2018/02/01 01:00:25.784 DiffTime : 1000.192
WaitTime : 1000 WaitEnd : 2018/02/01 01:00:26.784 DiffTime : 1000.193
WaitTime : 1000 WaitEnd : 2018/02/01 01:00:27.785 DiffTime : 1000.232
WaitTime : 1000 WaitEnd : 2018/02/01 01:00:28.785 DiffTime : 1000.462
WaitTime : 1000 WaitEnd : 2018/02/01 01:00:39.785 DiffTime : 10999.643
WaitTime : 1000 WaitEnd : 2018/02/01 01:00:42.259 DiffTime : 2473.116
WaitTime : 1000 WaitEnd : 2018/02/01 01:00:43.582 DiffTime : 1322.505
WaitTime : 1000 WaitEnd : 2018/02/01 01:00:44.582 DiffTime : 1000.173
WaitTime : 1000 WaitEnd : 2018/02/01 01:00:45.582 DiffTime : 1000.147
WaitTime : 1000 WaitEnd : 2018/02/01 01:00:48.973 DiffTime : 3389.941
WaitTime : 1000 WaitEnd : 2018/02/01 01:00:49.973 DiffTime : 1000.186
WaitTime : 1000 WaitEnd : 2018/02/01 01:00:50.973 DiffTime : 1000.186
WaitTime : 1000 WaitEnd : 2018/02/01 01:00:52.083 DiffTime : 1108.889
.
.
.

Sometimes very very delayed. This did not happen on Windows. It only occurred with Xamarin.Mac.
And I have to develop as asynchronous function because it's cocoa application. If I develop as not async function, it locks UI.


Viewing all articles
Browse latest Browse all 1429

Trending Articles