コード例 #1
0
        private async Task ReceiveFileAsync(IProgress <double> progressIndicator, CancellationToken token)
        {
            await Task.Run(async() =>  // async put so that the exception is thrown to the caller
            {
                try
                {
                    token.ThrowIfCancellationRequested();

                    updateEstimation = 1;
                    oldValue         = 0;
                    timestamp        = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds(); // initializing the timestamp at the beginning of the transfer

                    while (this.sft.FileLength > 0)
                    {
                        client.Receive(this.sft);
                        token.ThrowIfCancellationRequested();

                        //Thread.Sleep(500); // HACK: Waiting for testing purposes

                        progressIndicator.Report(100 - ((float)this.sft.FileLength / originalLength * 100));
                    }

                    client.EndReceiving(this.sft);
                }
                catch (OperationCanceledException)
                {
                    client.CancelReceiving(this.sft);
                    throw;
                }
                catch (Exception e)
                {
                    if (e is SocketException || e is IOException)
                    {
                        client.CancelReceiving(this.sft);
                        MessageBox.Show("There was an error in receiving the file.", "Mthis.sft", MessageBoxButton.OK,
                                        MessageBoxImage.Stop, MessageBoxResult.OK, MessageBoxOptions.DefaultDesktopOnly);
                        throw new OperationCanceledException();
                    }

                    throw;
                }
            }, token);
        }