Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to judge that the WebBrowser browser web page has been loaded

2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article mainly introduces how to judge the completion of WebBrowser browser web page loading, has a certain reference value, interested friends can refer to, I hope you can learn a lot after reading this article, the following let Xiaobian take you to understand.

Many people think that the connection to SqlConnection is time-consuming, because the average time to loop through SqlConnection.Open is almost zero, but the time spent on each first open often ranges from a few milliseconds to several seconds.

First, let's take a look at what the authoritative documents on MSDN say.

Connecting to a database server typically consists of several time-consuming steps. A physical channel such as a socket or a named pipe must be established, the initial handshake with the server must occur, the connection string information must be parsed, the connection must be authenticated by the server, checks must be run for enlisting in the current transaction, and so on.

The above is extracted from http://msdn.microsoft.com/en-us/library/8xx3tyca%28VS.80%29.aspx

In other words, when a physical connection is established, you need to shake hands with the server, parse the connection string, authorize, check constraints, and so on, but after the physical connection is established, these operations will not be done. These operations take some time. So many people like to use a static object storage SqlConnection to always maintain the physical connection, but when using static objects, multithreaded access will bring some problems, in fact, we do not need to do this at all, because SqlConnection turns on the connection pooling function by default, and when the program executes SqlConnection.Close, the physical connection will not be released immediately, so when the loop executes the Open operation, the execution time is almost 0.

Let's first take a look at the time it takes to loop through SqlConnection.Open when connection pooling is not open

The copy code is as follows:

Public static void OpenWithoutPooling ()

{

String connectionString = "Data Source=192.168.10.2; Initial Catalog=News;Integrated Security=True;Pooling=False;"

Stopwatch sw = new Stopwatch ()

Sw.Start ()

Using (SqlConnection conn = new SqlConnection (connectionString))

{

Conn.Open ()

}

Sw.Stop ()

Console.WriteLine ("Without Pooling, first connection elapsed {0} ms", sw.ElapsedMilliseconds)

Sw.Reset ()

Sw.Start ()

For (int I = 0; I < 100; iTunes +)

{

Using (SqlConnection conn = new SqlConnection (connectionString))

{

Conn.Open ()

}

}

Sw.Stop ()

Console.WriteLine ("Without Pooling, average connection elapsed {0} ms", sw.ElapsedMilliseconds / 100)

}

SqlConnection turns on connection pooling by default. If you want to forcibly close it, you need to add Pooling=False to the connection string.

The calling procedure is as follows:

The copy code is as follows:

Test.SqlConnectionTest.OpenWithoutPooling ()

Console.WriteLine ("Waiting for 10s")

System.Threading.Thread.Sleep (10 * 1000)

Test.SqlConnectionTest.OpenWithoutPooling ()

Console.WriteLine ("Waiting for 600s")

System.Threading.Thread.Sleep (600 * 1000)

Test.SqlConnectionTest.OpenWithoutPooling ()

Here are the test results

Without Pooling, first connection elapsed 13 ms

Without Pooling, average connection elapsed 5 ms

Wating for 10s

Without Pooling, first connection elapsed 6 ms

Without Pooling, average connection elapsed 4 ms

Wating for 600s

Without Pooling, first connection elapsed 7 ms

Without Pooling, average connection elapsed 4 ms

From the test results, after closing the connection pool, the average time per connection is about 4 milliseconds, which is the average time it takes to establish a physical connection.

Let's take a look at the test code by default

The copy code is as follows:

Public static void OpenWithPooling ()

{

String connectionString = "Data Source=192.168.10.2; Initial Catalog=News; Integrated Security=True;"

Stopwatch sw = new Stopwatch ()

Sw.Start ()

Using (SqlConnection conn = new SqlConnection (connectionString))

{

Conn.Open ()

}

Sw.Stop ()

Console.WriteLine ("With Pooling, first connection elapsed {0} ms", sw.ElapsedMilliseconds)

Sw.Reset ()

Sw.Start ()

For (int I = 0; I < 100; iTunes +)

{

Using (SqlConnection conn = new SqlConnection (connectionString))

{

Conn.Open ()

}

}

Sw.Stop ()

Console.WriteLine ("With Pooling, average connection elapsed {0} ms", sw.ElapsedMilliseconds / 100)

}

Calling code

The copy code is as follows:

Test.SqlConnectionTest.OpenWithPooling ()

Console.WriteLine ("Waiting for 10s")

System.Threading.Thread.Sleep (10 * 1000)

Test.SqlConnectionTest.OpenWithPooling ()

Console.WriteLine ("Waiting for 600s")

System.Threading.Thread.Sleep (600 * 1000)

Test.SqlConnectionTest.OpenWithPooling ()

Test result

With Pooling, first connection elapsed 119 ms

With Pooling, average connection elapsed 0 ms

Waiting for 10s

With Pooling, first connection elapsed 0 ms

With Pooling, average connection elapsed 0 ms

Waiting for 600s

With Pooling, first connection elapsed 6 ms

With Pooling, average connection elapsed 0 ms

According to the test results, the first time is 119ms, this is because in the test code, I first run this test process, and 119ms is the first connection time when the program starts, which may include not only the time to connect to the database, but also the time it takes to initialize ado.net itself, so this time can be ignored. After 10 seconds, the time of the first execution becomes 0ms, which indicates that the connection pooling mechanism works. After SqlConnection Close, the physical connection is not closed, so the connection is executed 10 seconds later, and the connection takes almost no time.

But we found an interesting phenomenon, 10 minutes later, the first connection time became 6ms, which is almost the same as the previous test without opening the connection pool, that is, 10 minutes later, the physical connection was closed and a physical connection was reopened. This is because the connection pool has a timeout, which by default should be between 5 and 10 minutes. If there is no connection operation during this period, the physical connection will be closed. So is there any way for us to stay physically connected all the time? There are ways.

There is a minimum connection pool size in the connection pool setting, which is 0 by default, and we can set it to a value greater than 0 to keep several physical connections unreleased all the time. Look at the code.

The copy code is as follows:

Public static void OpenWithPooling (int minPoolSize)

{

String connectionString = string.Format ("Data Source=192.168.10.2; Initial Catalog=News; Integrated Security=True;Min PoolSize = {0}", minPoolSize)

Stopwatch sw = new Stopwatch ()

Sw.Start ()

Using (SqlConnection conn = new SqlConnection (connectionString))

{

Conn.Open ()

}

Sw.Stop ()

Console.WriteLine ("With Pooling Min PoolSize = {0}, first connection elapsed {1} ms", minPoolSize, sw.ElapsedMilliseconds)

Sw.Reset ()

Sw.Start ()

For (int I = 0; I < 100; iTunes +)

{

Using (SqlConnection conn = new SqlConnection (connectionString))

{

Conn.Open ()

}

}

Sw.Stop ()

Console.WriteLine ("With Pooling Min PoolSize = {0}, average connection elapsed {1} ms", minPoolSize, sw.ElapsedMilliseconds / 100)

}

In fact, all you have to do is add a Min Pool Size=n to the connection string.

Calling code

The copy code is as follows:

Test.SqlConnectionTest.OpenWithPooling (1)

Console.WriteLine ("Waiting for 10s")

System.Threading.Thread.Sleep (10 * 1000)

Test.SqlConnectionTest.OpenWithPooling (1)

Console.WriteLine ("Waiting for 600s")

System.Threading.Thread.Sleep (600 * 1000)

Test.SqlConnectionTest.OpenWithPooling (1)

With Pooling Min Pool Size=1, first connection elapsed 5 ms

With Pooling Min Pool Size=1, average connection elapsed 0 ms

Waiting for 10s

With Pooling Min Pool Size=1, first connection elapsed 0 ms

With Pooling Min Pool Size=1, average connection elapsed 0 ms

Waiting for 600s

With Pooling Min Pool Size=1, first connection elapsed 0 ms

With Pooling Min Pool Size=1, average connection elapsed 0 ms

We can see that when Min Pool Size = 1, except for the 5ms when the first connection is used, it is still 0ms even after 10 minutes, and the physical connection is not closed.

Multithreaded call problem

Multithreaded calls I have also done a test, here do not post the code, I will probably talk about the results. If it is multithreaded to access SqlConnection, note that it is accessed through new SqlConnection.

So there are two problems. If the latter thread invokes the Open operation before the previous thread Close, then Ado.net cannot reuse a physical connection, it will assign a new physical connection to the second thread. If the latter thread is Open and the previous thread is already Close, the new thread uses the physical connection of the previous thread. That is, if there are n threads connecting to the database at the same time, at most n physical connections will be created and at least 1. If you create n physical connections, the usage time is theoretically equal to n * t / cpu, where n is the number of threads, and t is the time it takes to create a physical connection. The result of the previous test is about 5-10ms, and cpu is the number of CPU of the current machine. In addition, the load of the network and the server also affects this time. In order to ensure that new physical connections are created as few as possible during large concurrency, we can appropriately increase the Min Pool Size, but not too much, because the number of TCP links on a single machine is limited.

Thank you for reading this article carefully. I hope the article "how to judge the completion of WebBrowser browser page loading" shared by the editor will be helpful to you. At the same time, I also hope you will support us and pay attention to the industry information channel. More related knowledge is waiting for you to learn!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report