예제 #1
0
        /// <summary>
        /// Test that the server is connected
        /// </summary>
        /// <returns>true if the connection is opened</returns>
        private static async Task <bool> CheckConnectionAsync()
        {
            var    flag       = false;
            string sqlMessage = null;

            try
            {
                var cts = new CancellationTokenSource(msTimeout);
                await NativeMethod.CursorWaitForAsync(() =>
                {
                    using (var dbContext = new TdbContext())
                    {
                        try
                        {
                            dbContext.Database.Connection.Open();
                            flag = true;
                        }
                        catch (SqlException e)// force timeout, since there is no way to catch exception inside Task.Run
                        {
                            sqlMessage = e.Message;
                            Task.Delay(msTimeout).Wait();
                        }
                    }
                }, cts.Token);
            }
            catch (TaskCanceledException)
            {
                Messenger.Default.Send(new Message <string>(sqlMessage ?? "Database connection timeout!"), errorToken);
            }
            return(flag);
        }