This resource pool is used to manage the valuable resources of the program.
The main class is resourcepool<t>.
Users can request a resource by calling the GetResource method, and then return it to the resource pool through Returnresource. The resource pool determines when redundant resources are freed.
Interface iresourceprovider<t> is used to obtain resources.
Class resourcetag<t> is used to flag whether a resource is in use.
The concrete is not much to say, please see the code. Welcome to the discussion.
Test the code First:
1 using System;
2 using Nunit.framework;
3 using System.Data.SqlClient;
4 public class Sqlconnectionprovider:iresourceprovider<sqlconnection>
5 {
6 Public SqlConnection Request ()
7 {
8 SqlConnection con= New SqlConnection ();
9//Open the database connection here because Resourcepool requires managing those resources that are available.
Ten//con. Open ();
One return con;
12}
The public void Dispose (SqlConnection con)
14 {
15//Destroy objects Here
Con. Dispose ();
17}
18}
[Testfixture]
public class Test
21 {
[Test]
public void Testpool ()
24 {
25//Here initializes the resource pool, parameters: A resource provider class and the maximum number of resources in one maximum resource pool
-Resourcepool<sqlconnection> Pool=resourcepool<sqlconnection> Instance (New Sqlconnectionprovider (), 10);
27
Long ResourceID;
SqlConnection Con=pool. GetResource (out ResourceID);
30//Use Con object here
31
32//After use, return
Pool. Returnresource (ref Con,resourceid);
34
35
36}
37}
(The above test is just a simple demo function, the detailed test code is related to other classes of the project, it is more complicated to paste)