{"id":85,"date":"2019-02-10T19:01:05","date_gmt":"2019-02-10T11:01:05","guid":{"rendered":"http:\/\/jrblog.mooo.com\/john\/2019\/02\/10\/avoid-fileio-locking\/"},"modified":"2019-02-10T19:01:05","modified_gmt":"2019-02-10T11:01:05","slug":"avoid-fileio-locking","status":"publish","type":"post","link":"https:\/\/john.pentaidea.com\/?p=85","title":{"rendered":"Avoid fileIO locking"},"content":{"rendered":"<pre><code>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;private static ReaderWriterLockSlim _readWriteLock;\n\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;public void WriteToFileThreadSafe(string text, string path)\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\/\/ Set Status to Locked\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;_readWriteLock.EnterWriteLock();\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;try\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\/\/ Append text to the file\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;using (StreamWriter sw = File.AppendText(path))\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;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-excerpt\"><p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;private static ReaderWriterLockSlim _readWriteLock;<\/p>\n<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;public void WriteToFileThreadSafe(string text, string path)<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\/\/ Set Status to Locked<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;_readWriteLock.<\/p>\n<\/div>","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[4],"tags":[17],"class_list":["post-85","post","type-post","status-publish","format-standard","hentry","category-tech","tag-posts_y"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v22.8 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Avoid fileIO locking - John&#039;s Blog<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/john.pentaidea.com\/?p=85\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Avoid fileIO locking - John&#039;s Blog\" \/>\n<meta property=\"og:description\" content=\"&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;private static ReaderWriterLockSlim _readWriteLock; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;public void WriteToFileThreadSafe(string text, string path) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\/\/ Set Status to Locked &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;_readWriteLock.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/john.pentaidea.com\/?p=85\" \/>\n<meta property=\"og:site_name\" content=\"John&#039;s Blog\" \/>\n<meta property=\"article:published_time\" content=\"2019-02-10T11:01:05+00:00\" \/>\n<meta name=\"author\" content=\"jj\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"jj\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"2 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\/\/john.pentaidea.com\/?p=85\",\"url\":\"https:\/\/john.pentaidea.com\/?p=85\",\"name\":\"Avoid fileIO locking - John&#039;s Blog\",\"isPartOf\":{\"@id\":\"https:\/\/john.pentaidea.com\/#website\"},\"datePublished\":\"2019-02-10T11:01:05+00:00\",\"dateModified\":\"2019-02-10T11:01:05+00:00\",\"author\":{\"@id\":\"https:\/\/john.pentaidea.com\/#\/schema\/person\/3d2e5980a7d3023e93b91d668d2a4a4f\"},\"breadcrumb\":{\"@id\":\"https:\/\/john.pentaidea.com\/?p=85#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/john.pentaidea.com\/?p=85\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/john.pentaidea.com\/?p=85#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/john.pentaidea.com\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Avoid fileIO locking\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/john.pentaidea.com\/#website\",\"url\":\"https:\/\/john.pentaidea.com\/\",\"name\":\"John's Blog\",\"description\":\"\u4e16\u754c\u56e0\u601d\u7dd2\u800c\u8907\u96dc, \u4eba\u56e0\u5922\u60f3\u800c\u5049\u5927\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/john.pentaidea.com\/?s={search_term_string}\"},\"query-input\":\"required name=search_term_string\"}],\"inLanguage\":\"en-US\"},{\"@type\":\"Person\",\"@id\":\"https:\/\/john.pentaidea.com\/#\/schema\/person\/3d2e5980a7d3023e93b91d668d2a4a4f\",\"name\":\"jj\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/john.pentaidea.com\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/a685414cd92056415bc823b5cfbbf95a44a98df88d8c040b18613724c1c0724b?s=96&d=mm&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/a685414cd92056415bc823b5cfbbf95a44a98df88d8c040b18613724c1c0724b?s=96&d=mm&r=g\",\"caption\":\"jj\"},\"sameAs\":[\"https:\/\/john.pentaidea.com\"],\"url\":\"https:\/\/john.pentaidea.com\/?author=1\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Avoid fileIO locking - John&#039;s Blog","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/john.pentaidea.com\/?p=85","og_locale":"en_US","og_type":"article","og_title":"Avoid fileIO locking - John&#039;s Blog","og_description":"&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;private static ReaderWriterLockSlim _readWriteLock; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;public void WriteToFileThreadSafe(string text, string path) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\/\/ Set Status to Locked &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;_readWriteLock.","og_url":"https:\/\/john.pentaidea.com\/?p=85","og_site_name":"John&#039;s Blog","article_published_time":"2019-02-10T11:01:05+00:00","author":"jj","twitter_card":"summary_large_image","twitter_misc":{"Written by":"jj","Est. reading time":"2 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/john.pentaidea.com\/?p=85","url":"https:\/\/john.pentaidea.com\/?p=85","name":"Avoid fileIO locking - John&#039;s Blog","isPartOf":{"@id":"https:\/\/john.pentaidea.com\/#website"},"datePublished":"2019-02-10T11:01:05+00:00","dateModified":"2019-02-10T11:01:05+00:00","author":{"@id":"https:\/\/john.pentaidea.com\/#\/schema\/person\/3d2e5980a7d3023e93b91d668d2a4a4f"},"breadcrumb":{"@id":"https:\/\/john.pentaidea.com\/?p=85#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/john.pentaidea.com\/?p=85"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/john.pentaidea.com\/?p=85#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/john.pentaidea.com\/"},{"@type":"ListItem","position":2,"name":"Avoid fileIO locking"}]},{"@type":"WebSite","@id":"https:\/\/john.pentaidea.com\/#website","url":"https:\/\/john.pentaidea.com\/","name":"John's Blog","description":"\u4e16\u754c\u56e0\u601d\u7dd2\u800c\u8907\u96dc, \u4eba\u56e0\u5922\u60f3\u800c\u5049\u5927","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/john.pentaidea.com\/?s={search_term_string}"},"query-input":"required name=search_term_string"}],"inLanguage":"en-US"},{"@type":"Person","@id":"https:\/\/john.pentaidea.com\/#\/schema\/person\/3d2e5980a7d3023e93b91d668d2a4a4f","name":"jj","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/john.pentaidea.com\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/a685414cd92056415bc823b5cfbbf95a44a98df88d8c040b18613724c1c0724b?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/a685414cd92056415bc823b5cfbbf95a44a98df88d8c040b18613724c1c0724b?s=96&d=mm&r=g","caption":"jj"},"sameAs":["https:\/\/john.pentaidea.com"],"url":"https:\/\/john.pentaidea.com\/?author=1"}]}},"jetpack_sharing_enabled":true,"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/john.pentaidea.com\/index.php?rest_route=\/wp\/v2\/posts\/85","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/john.pentaidea.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/john.pentaidea.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/john.pentaidea.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/john.pentaidea.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=85"}],"version-history":[{"count":0,"href":"https:\/\/john.pentaidea.com\/index.php?rest_route=\/wp\/v2\/posts\/85\/revisions"}],"wp:attachment":[{"href":"https:\/\/john.pentaidea.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=85"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/john.pentaidea.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=85"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/john.pentaidea.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=85"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}