示例#1
0
        /// <summary>
        /// Asynchronously executes the query.
        /// </summary>
        /// <typeparam name="TResult">The type of the elements in the returned collection.</typeparam>
        /// <param name="dataContext">The data context.</param>
        /// <param name="query">The query.</param>
        /// <param name="cancellationToken">The cancellation token.</param>
        /// <returns>A collection of objects returned by the query.</returns>
        public static async Task <IEnumerable <TResult> > ExecuteQueryAsync <TResult>(
            this DataContext dataContext,
            IQueryable <TResult> query,
            CancellationToken cancellationToken = default)
        {
            if (dataContext == null)
            {
                throw new ArgumentNullException(nameof(dataContext));
            }
            if (query == null)
            {
                throw new ArgumentNullException(nameof(query));
            }

            var command = dataContext.GetCommand(query);

            if (command is SqlCommand sqlCommand)
            {
                // Microsoft SQL Server data provider natively supports async operations.

                var connection = sqlCommand.Connection;
                if (connection.State == ConnectionState.Closed)
                {
                    await connection.OpenAsync(cancellationToken).ConfigureAwait(false);
                }

                var reader = await sqlCommand.ExecuteReaderAsync(cancellationToken).ConfigureAwait(false);

                // Execute Translate via TaskBridge because it uses synchronous operations
                // like Read() and NextResult() on a specified reader instance.
                // This allows to avoid possible thread pool pollution on a large number of concurrent operations.
                // In essence, this is an async polyfill but there is no better way without modifying the stock LINQ to SQL implementation.
                return(await
                       TaskBridge.ExecuteAsync(
                           () => dataContext.Translate <TResult>(reader),
                           cancellationToken)
                       .ConfigureAwait(false));
            }
            else
            {
                // Use TaskBridge-based async polyfill for other providers.

                return(await
                       TaskBridge.ExecuteAsync(
                           () =>
                {
                    var connection = command.Connection;
                    if (connection.State == ConnectionState.Closed)
                    {
                        connection.Open();
                    }

                    var reader = command.ExecuteReader();

                    return dataContext.Translate <TResult>(reader);
                },
                           cancellationToken)
                       .ConfigureAwait(false));
            }
        }
示例#2
0
        /// <summary>
        /// Asynchronously sends changes that were made to retrieved objects to the underlying database,
        /// and specifies the action to be taken if the submission fails.
        /// </summary>
        /// <param name="dataContext">The data context.</param>
        /// <param name="failureMode">The action to be taken if the submission fails.</param>
        /// <param name="cancellationToken">The cancellation token.</param>
        /// <returns>The task.</returns>
        public static Task SubmitChangesAsync(this DataContext dataContext, ConflictMode failureMode, CancellationToken cancellationToken)
        {
            if (dataContext == null)
            {
                throw new ArgumentNullException(nameof(dataContext));
            }

            // Using TaskBridge-based async polyfill as the best effort solution
            // (there is no better way without modifying the stock LINQ to SQL implementation).

            return(TaskBridge.ExecuteAsync(
                       () => dataContext.SubmitChanges(failureMode),
                       cancellationToken));
        }
示例#3
0
        static void Main(string[] args)
        {
            try
            {
                _Run();
                TaskBridge.Execute(_RunAsync);

                Console.WriteLine();
                Console.WriteLine("----------------------");
                Console.WriteLine("DONE");
            }
            catch (Exception e)
            {
                Console.Write("Error: ");
                Console.WriteLine(e);
            }
        }
示例#4
0
 /// <summary>
 /// Instructs the <see cref="Process"/> component to wait the specified number of milliseconds for the associated process to exit.
 /// </summary>
 /// <param name="process">The process.</param>
 /// <param name="milliseconds">The amount of time, in milliseconds, to wait for the associated process to exit.</param>
 /// <param name="cancellationToken">The cancellation token.</param>
 public static void WaitForExit(this Process process, int milliseconds, CancellationToken cancellationToken) =>
 TaskBridge.Execute(WaitForExitAsync(process, milliseconds, cancellationToken));
示例#5
0
 /// <summary>
 /// Ends a process according to a specified mode and timeout.
 /// </summary>
 /// <param name="process">The process to end.</param>
 /// <param name="mode">The mode of ending a process.</param>
 /// <param name="millisecondsTimeout">The amount of time, in milliseconds, to wait for the associated process to end.</param>
 /// <returns>The mode in which the process has been ended.</returns>
 public static ProcessEndMode End(this Process process, ProcessEndMode mode, int millisecondsTimeout) =>
 TaskBridge.Execute(ct => EndAsync(process, mode, millisecondsTimeout, ct));