Implementation of a C # resource pool for Project common algorithms

Source: Internet
Author: User
Tags resource

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)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.