0

I'm developing Xamarin.Mac(Cocoa) using C#. I want to wait seconds, so I developed waiting function using Task.Delay.

But repeating Task.Delay causes a large delay. Source code↓

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.WriteLine($"----------------------------------");

            }
        }
    }
}

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.

[Edit] Thanks comment! I have to develop as async function because it's cocoa application. If I develop as not async function, it locks UI.

hirossyi
  • 31
  • 6
  • Interesting. How is it if you replace `Task.Delay` by a timer: `var timer = new System.Threading.Timer(x => delayedAction(), null, 1000, Timeout.Infinite);` – Dirk Vollmar Jan 31 '18 at 16:16
  • Just tried your code, I saw a few hundred milliseconds max deviation... Try `NSThread.SleepFor(1.000);` instead of Task.Delay as an experiment. If it varies greatly also, I would say something else on your system is getting priority (cpu, paging memory/swap, disk) – SushiHangover Jan 31 '18 at 21:45
  • Thanks comment! But sorry, I have to develop as async function because it's cocoa application. If I develop as not async function, it locks UI. – hirossyi Feb 01 '18 at 02:12

0 Answers0