Exemplo n.º 1
0
            public Task GetOrDownloadAsync(string url)
            {
                //simulate image download
                IPendingOperation pendingOperation = null;

                Console.WriteLine($"Downloading {url}");
                Task.Delay(2000).ContinueWith(_ =>
                {
                    Console.WriteLine($"Downloaded {url}");
                    pendingOperation.Complete();
                });

                pendingOperation = ServiceRef.RegisterPendingOperation(this, actionOnCompletion: (res) =>
                {
                    //Accessing _images here would be risky, just wrap the call...
                    ServiceRef.Call(this, () =>
                    {
                        Console.WriteLine($"Adding to images {url}");
                        _images.Add(new ImageStuff()
                        {
                            Url = url, Data = new byte[] { 0x01 }
                        });
                        Console.WriteLine($"Added to images {url}");
                    });
                });

                return(Task.CompletedTask);
            }
Exemplo n.º 2
0
            public void BeginOperation()
            {
                _pendingOperation = ServiceRef.RegisterPendingOperation(this);

                Task.Factory.StartNew(() =>
                {
                    //simulate some work
                    Task.Delay(1000).Wait();
                    ServiceRef.Create <IPendingOpsService>(this)
                    .CompleteOperation();
                });
            }
Exemplo n.º 3
0
            public Task <bool> BeginOperationAsync()
            {
                _pendingOperation = ServiceRef.RegisterPendingOperation(this, () => OperationCompleted);

                Task.Run(async() =>
                {
                    //simulate some work
                    await Task.Delay(1000);
                    await ServiceRef.Create <IPendingOpsServiceAsync>(this)
                    .CompleteOperationAsync();
                });

                return(Task.FromResult(false));
            }
            public Task GetOrDownloadAsync(string url)
            {
                //simulate image download
                var downloadedEvent = new AutoResetEvent(false);

                Console.WriteLine($"Downloading {url}");
                Task.Delay(10000).ContinueWith(_ =>
                {
                    Console.WriteLine($"Downloaded {url}");
                    _images.Add(new ImageStuff()
                    {
                        Url = url, Data = new byte[] { 0x01 }
                    });
                    downloadedEvent.Set();
                });

                ServiceRef.RegisterPendingOperation(this, downloadedEvent);

                return(Task.CompletedTask);
            }