コード例 #1
0
        private async Task UpdateLoopAsync()
        {
            int UpdateStatus(LinkProfile profile)
            {
                var span = DateTime.Now - profile.LastOnlineDateTime;

                if (span < TimeSpan.Zero || span > environment.ProfileOnlineTimeout)
                {
                    profile.ProfileStatus = LinkProfileStatus.Offline;
                }
                var imageHash = profile.RemoteImageHash;

                if (imageHash.IsNullOrEmpty() == false && imageHash != profile.ImageHash && pending.Add(profile.Id))
                {
                    Task.Run(() => UpdateImageAsync(profile));
                }
                return(1);
            }

            var token = client.CancellationToken;

            while (true)
            {
                token.ThrowIfCancellationRequested();
                await client.UpdateUIAsync(() => Op.Lock(locker, () => profiles.Values.Sum(UpdateStatus)));

                await Task.Delay(environment.BackgroundTaskDelay);
            }
        }
コード例 #2
0
        internal async Task <Task> StartAsync()
        {
            var profile     = client.Profile;
            var udpEndPoint = environment.UdpEndPoint;
            var tcpEndPoint = environment.TcpEndPoint;

            udpClient = new UdpClient(udpEndPoint)
            {
                EnableBroadcast = true
            };
            tcpListener = new TcpListener(tcpEndPoint);
            tcpListener.Start();
            await client.UpdateUIAsync(() =>
            {
                profile.UdpPort = udpEndPoint.Port;
                profile.TcpPort = tcpEndPoint.Port;
            });

            var tasks = new Task[]
            {
                Task.Run(UdpLoopAsync),
                Task.Run(TcpLoopAsync),
            };

            return(Task.WhenAll(tasks));
        }