System.Threading.Tasks.TaskCompletionSource.SetResult(Result)

Here are the examples of the csharp api System.Threading.Tasks.TaskCompletionSource.SetResult(Result) taken from open source projects. By voting up you can indicate which examples are most useful and appropriate.

6 Examples 7

19 Source : BarcodeScannerController.cs
with MIT License
from dansiegel

public Task<Result> ReadBarcodeResultAsync(params BarcodeFormat[] barcodeFormats)
        {
            var initialFormats = _scannerView.ScannerView.Options.PossibleFormats;
            _scannerView.ScannerView.Options.PossibleFormats = new List<BarcodeFormat>(barcodeFormats);
            var tcs = new TaskCompletionSource<Result>();
            ReadBarcodeInternal(r =>
            {
                _scannerView.ScannerView.Options.PossibleFormats = initialFormats;
                tcs.SetResult(r);
            }, e => tcs.SetException(e));
            return tcs.Task;
        }

19 Source : BarcodeScannerController.cs
with MIT License
from dansiegel

public Task<Result> ReadBarcodeResultAsync()
        {
            var tcs = new TaskCompletionSource<Result>();
            ReadBarcodeInternal(r => tcs.SetResult(r), e => tcs.SetException(e));
            return tcs.Task;
        }

19 Source : SocketMessenger.cs
with GNU Affero General Public License v3.0
from mileswatson

private Task<Result<int>> WriteFromBuffer(byte[] buffer, int offset)
		{
			try
			{
				var tcs = new TaskCompletionSource<Result<int>>();

				// Read data into fixed length buffer.
				socket.BeginSend(buffer, offset, buffer.Length - offset, SocketFlags.None, ar =>
				{
					try
					{
						var numBytesWritten = socket.EndSend(ar);
						tcs.SetResult(numBytesWritten);
					}
					catch (Exception e)
					{
						Debug.WriteLine(e);
						tcs.SetResult(Error());
					}
				}, null);

				return tcs.Task;
			}
			catch (Exception e)
			{
				Debug.WriteLine(e);
				return Task.FromResult<Result<int>>(Error());
			}
		}

19 Source : SocketServer.cs
with GNU Affero General Public License v3.0
from mileswatson

public async Task<Result<SocketMessenger, ConnectionError, DisposedError>> Accept()
		{
			if (disposed) return Error(new DisposedError());

			var preplaced = await acceptQueue.GetPreplaced().ConfigureAwait(false);
			if (preplaced.IsError) return Error(new DisposedError());

			var tcs = new TaskCompletionSource<Result<Socket, ConnectionError>>();
			try
			{
				listener.BeginAccept(
					new AsyncCallback(ar =>
					{
						try
						{
							// End the accept process.
							Socket connection = listener.EndAccept(ar);

							// Set the result.
							tcs.SetResult(connection);
						}
						catch (Exception e)
						{
							Debug.WriteLine(e);
							tcs.SetResult(Error(new ConnectionError()));
						}
					}),
					null
				);

				// Check periodically for disposal
				do
				{
					var timeout = Task.Delay(1000);
					await Task.WhenAny(tcs.Task, timeout).ConfigureAwait(false);
					if (tcs.Task.IsCompleted)
					{
						break;
					}
					ThrowIfDisposed();
				} while (true);

				// Attempt to return the result
				var result = tcs.Task.Result;
				if (result.IsError) return Error(new ConnectionError());
				return new SocketMessenger(result.Value);
			}
			finally
			{
				acceptQueue.ReturnPreplaced();
			}
		}

19 Source : SocketMessenger.cs
with GNU Affero General Public License v3.0
from mileswatson

private Task<Result<int>> ReadIntoBuffer(byte[] buffer, int offset)
		{
			try
			{
				var tcs = new TaskCompletionSource<Result<int>>();

				// Read data into fixed length buffer.
				socket.BeginReceive(buffer, offset, buffer.Length - offset, SocketFlags.None, ar =>
				{
					try
					{
						var numBytesRead = socket.EndReceive(ar);
						tcs.SetResult(numBytesRead);
					}
					catch (Exception e)
					{
						Debug.WriteLine(e);
						tcs.SetResult(Error());
					}
				}, null);
				return tcs.Task;
			}
			catch (Exception e)
			{
				Debug.WriteLine(e);
				return Task.FromResult<Result<int>>(Error());
			}
		}

19 Source : CompletionNotifier.cs
with MIT License
from tana

public void Notify(Key key, Result result)
    {
        TaskCompletionSource<Result> tcs;
        lock (dict)
        {
            tcs = dict[key];
            dict.Remove(key);
        }
        tcs.SetResult(result);
    }