C#控制台应用程序,退出缓慢 [英] C# Console App, slow to exit

查看:398
本文介绍了C#控制台应用程序,退出缓慢的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个非常基本的C#控制台应用程序,该应用程序连接到数据库,执行查询,关闭连接并退出该应用程序.

I have a very basic C# console app that connects to a db, executes a query, closes the connection, and exits out of the app.

问题在于,该应用需要花费近3秒钟的时间退出.

The problem is, the app takes almost 3 seconds to exit.

我已经显示了每个步骤的时间,以查看其运行缓慢的原因,而不是在任何处理过程中,只是它退出应用程序的时间.

I have displayed the time at each step to see why it is running slowly and it isn't during any of the processing, just when it exits out of the app.

有人知道如何加快速度吗?

Does anyone know how to speed this up?

打开连接:94ms
26:确定
闭合连接:356ms
闭合连接:357ms
退出:358ms
[退出前大约3秒钟的延迟]

Opening Connection:94ms
26:OK
Closing Connection:356ms
Closed Connection:357ms
Exiting:358ms
[Delay of about 3 seconds before it exits]

using System;
using System.Data;
using System.Data.SqlClient;
using System.Diagnostics;

namespace CheckSQL
{
    class Program
    {
        static Stopwatch watch = new Stopwatch();

        static void Main(string[] args)
        {
            if (args.Length == 0) return;
            watch.Start();

            string connstring = args[0];
            string sqlquery = args[1];

            ExecuteScalar(connstring, sqlquery);
            watch.Stop();
            Console.WriteLine(string.Format("Exiting:{0}ms", watch.ElapsedMilliseconds));
        }

        private static void ExecuteScalar(string connstring, string sqlquery)
        {
            SqlConnection sqlconn = new SqlConnection(connstring);
            SqlCommand sqlcmd = new SqlCommand(sqlquery, sqlconn);

            try
            {
                Console.WriteLine(string.Format("Opening Connection:{0}ms", watch.ElapsedMilliseconds));
                sqlconn.Open();
                Console.WriteLine(string.Format("{0}:OK", sqlcmd.ExecuteScalar()));
            }
            catch (Exception ex)
            {
                Console.WriteLine(string.Format("0:{0}", ex.Message));
            }
            finally
            {
                if (sqlconn.State == ConnectionState.Open)
                {
                    Console.WriteLine(string.Format("Closing Connection:{0}ms", watch.ElapsedMilliseconds));
                    sqlconn.Close();
                    Console.WriteLine(string.Format("Closed Connection:{0}ms", watch.ElapsedMilliseconds));
                }
            }
        }
    }
}

推荐答案

我在C#控制台应用程序中遇到了类似的问题,发现该问题与应用程序退出时清理连接池有关.通过池中的连接,我测量了退出的1.6秒延迟(由调用我的EXE的外部脚本计时).尽管我对解决方案并不完全满意,但是我发现在退出之前发出以下命令可以消除延迟:

I had a similar problem with a C# Console app, and found that the issue had something to do with the cleanup of the Connection Pool when the app exits. With connections in the pool, I measured a 1.6 second delay in exiting (Timed by an external script calling my EXE). Although I wasn't entirely happy with the solution, I found that issuing the following, before exiting removed the delay:

System.Data.SqlClient.SqlConnection.ClearAllPools();

System.Data.SqlClient.SqlConnection.ClearAllPools();

我猜想在连接字符串中使用"Pooling = False"也会达到目的...但是,只有在不需要池化的好处的情况下,您才可以这样做.

I would guess that using "Pooling=False", in your connection strings would also do the trick... But you would only do that if you didn't need the benefits of pooling.

这篇关于C#控制台应用程序,退出缓慢的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆