File read and write conflict resolution: ReaderWriterLock

2011-04-07  来源:本站原创  分类:DotNet  人气:149 

The project ran into static pages of documents to read and write conflicts (simultaneous read and write if they reported the Yellow Pages, although the writing is the "moment", but still possible under high concurrency.)

public class ReaderWriterLockHelper  

{  

    static ReaderWriterLock rwl = new ReaderWriterLock();  

    public static void ReadFromResource(int timeOut, Action readAction)  

    {  

        try 

        {  

            rwl.AcquireReaderLock(timeOut);  

            try 

            {  

                readAction();  

            }  

            finally 

            {  

                rwl.ReleaseReaderLock();  

            }  

        }  

        catch (ApplicationException)  

        {  

        }  

    }  

    public static void WriteToResource(int timeOut, Action writeAction)  

    {  

        try 

        {  

            rwl.AcquireWriterLock(timeOut);  

            try 

            {  

                writeAction();  

            }  

            finally 

            {  

                rwl.ReleaseWriterLock();  

            }  

        }  

        catch (ApplicationException)  

        {  

        }  

    }  

}

Write:

var thread = new Thread(context =>  

{  

    HttpContext.Current = (HttpContext)context;  

    var content = TemplateFactory.ReplaceContent(template);  

    ReaderWriterLockHelper.WriteToResource(10, () =>  

    {  

        using (var sw = fileInfo.CreateText())  

        {  

            sw.WriteLine(content);  

            sw.Close();  

        }  

    });  

});  

thread.Start(HttpContext.Current);

Read:

ReaderWriterLockHelper.ReadFromResource (10, () => context.Response.WriteFile (url));

相关文章
  • File read and write conflict resolution: ReaderWriterLock 2011-04-07

    The project ran into static pages of documents to read and write conflicts (simultaneous read and write if they reported the Yellow Pages, although the writing is the "moment", but still possible under high concurrency.) public class ReaderWrite

  • Detailed SVN file tree conflict and conflict resolution (r) 2011-06-28

    When you update from the repository, merge files, or switch your working copy to a different URL when you get a conflict. There are two conflicts: SVN file conflict when two (or more) developers to modify the same file in the same row adjacent or fil

  • SVN conflict resolution 2010-04-06

    Order to resolve version conflicts. In conflict resolution, the need to use the svn resolved to tell subversion conflict resolution, in order to submit updates. Conflict occurs, subversion will be saved in the Work Copy all of the object file version

  • TortoiseCVS to TortoiseSVN files using a similar conflict resolution 2011-05-23

    Problems arise: For the use of Copy-Modified-Merge of version control system approach, conflict management and resolution is very important. The company's version of a project to upgrade the system, the system's source code during the development pro

  • svn command-line conflict resolution 2011-03-28

    svn command-line conflict resolution if you do not accidentally committed some files do not need the dynamic changes to the svn server, how to resolve the conflict, the basic idea is to first use the svn resolved filename to give up on the file chang

  • WAS application package to deploy Jmx the process of conflict resolution 2010-10-19

    WAS application package to deploy Jmx the process of conflict resolution Recently, the was6.0 above a JMX-based application deployment, the emergence of a JMX class JMXConnectorFactory.resolveClassLoader () of the MethodNotFound the error, but clearl

  • JBoss Log4j conflict resolution methods 2010-03-29

    Boss built Log4j, so if in their own projects to use Log4j, configured in the log4j.xml within the project will be conflict. Because in the JBoss unified management concept is the log, so all of the logs are output to the system log inside. JBoss pro

  • Eclipse plug-in conflict resolution 2010-07-24

    Open the Eclipse projects under construction today, edit, every step of the operation prompt will pop up an error box that prompts: An error has occurred.see error log for more details. Internet search has found out it was because the installed plug-

  • SVN in conflict resolution 2010-08-30

    - I use the SVN is not very long time, just prior to use are too shallow to understand the software. Starting from August this year, as a project using Eclipse 3.1, cross-regional development, in order to adapt to different developers in different ge

  • jQuery with other libraries conflict resolution 2010-12-06

    Developed using jQuery, it may also use to other JS libraries such as Prototype, but the coexistence of many libraries may conflict; if a conflict, they can be resolved through the following programs: A, jQuery library before the import of other libr

  • About $ jquery dwr at conflict resolution methods 2009-03-28

    The following order include. Js library <script type="text/javascript" src="../js/jquery-1.3.2.min.js"></script> <script language="javascript"> var j = jQuery.noConflict(); </script> <script type='t

  • jquery and prototype conflict resolution 2010-08-02

    Background problem: want to use jQuery to do AJAX deal to do with form-based Prototype of EasyValidation validation, but the two can not coexist, because both use the same method name: $ (). Address: <script type="text/javascript"> jQuery.

  • sql server2005 (INSERT statement with COLUMN FOREIGN KEY constraint conflict resolution) 2010-11-01

    Recently. In a project with sql server2005, when inserting data Report: INSERT statement with COLUMN FOREIGN KEY constraint errors. This error, the reason is that the main table has been added to the database data, and then add in its child table dat

  • TortoiseCVS to TortoiseSVN uses a similar file conflict solution 2011-05-23

    Problems arise: For use Copy-Modified-Merge of version control system approach, conflict management and resolution is very important. The company's version of a project due to system upgrades, system source code in the development process a large num

  • <GotGit> Appendix A Git command index 2011-05-26

    "GotGit" command index in Appendix A Git Appendix A. Git command index Git commands for each child, and a specific directory called git-<cmd> corresponding document, which is there in this particular directory called git-<cmd> execut

  • (Graphic) SVN version of the conflict Settlement Understanding 2010-09-20

    Version of the causes of conflict: Suppose A, B are two users when the 100 version, updated kingtuns.txt this document, A user submitted after editing kingtuns.txt to the server, this time successfully submitted, this time kingtuns.txt version of the

  • (Transfer) svn tree conflict 2010-12-21

    SVN conflicts and directory tree loss problem (1) Temporary work, and an old friend (after the code name used yzw) MSN, call me. SVN said he encountered a problem: • The executive branch merge, a directory tree of the conflict took place • Direct to

  • (R) svn tree conflict 2010-12-21

    SVN directory tree conflict and loss issues (1) Temporary work, and an old friend (followed by the yzw said on behalf of) call me on MSN. The SVN says he encountered a problem: • The Executive branch merge, a directory tree conflict occurred • Direct

  • How to achieve more than multi-line sharing of ERP changes and save excel file 2011-09-29

    Company monthly sales statistics office staff is the most headaches, usually by the sales department to sales of entry into an Excel worksheet, and then back the company from the office, copy them to a work sheet book, the final focus. As the company

  • SWC file description 2010-03-09

    SWC SWC files are similar to zip the file (package and archive format by PKZIP to expand), it generated by the Flash authoring tool. SWC files generated from the Flash environment, you can choose to SWC files contain other files. For example, you may