C# Console App, slow to exit

瘦欲@ 提交于 2019-12-11 03:03:08

问题


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.

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?

Here is the output:

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

And here is the code:

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));
                }
            }
        }
    }
}

回答1:


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();

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.




回答2:


Closing a connection (calling sqlconn.Close() ) only means returning it to the ConnectionPool.

So there still is some housekeeping to be done on exit.

3 seconds seems a bit long, but there are several components (CLR, Database) in play here.




回答3:


I think its impossible to do in correct way. How can you seepd up job which takes some time? The only possible way in this case - to optimize algorythm, but you cant do this. As I understand you should return control immediately after checking some database information. You can workaround this by creating two process system. First process would startup second and the second in turn would check infomation in database and send results to the first process which would communicate with user. So you alway would return control after retrieving results. The second process would take some time to terminate but this fact should not worry you because you would have control by that moment.



来源:https://stackoverflow.com/questions/5881171/c-sharp-console-app-slow-to-exit

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!