{"id":323,"date":"2020-12-28T08:37:23","date_gmt":"2020-12-28T08:37:23","guid":{"rendered":"https:\/\/michaeljohnsteiner.com\/?p=323"},"modified":"2020-12-28T08:43:43","modified_gmt":"2020-12-28T08:43:43","slug":"monitoractionfuncwrapper-cs","status":"publish","type":"post","link":"https:\/\/michaeljohnsteiner.com\/index.php\/2020\/12\/28\/monitoractionfuncwrapper-cs\/","title":{"rendered":"MonitorActionFuncWrapper.cs"},"content":{"rendered":"\n<p>Monitor Lock Wrapper<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"csharp\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">using System;\nusing System.Threading;\npublic class MonitorActionFuncWrapper : ConcurrencyCheck\n{\n    public void Lock(object localObject, Action action)\n    {\n        if (action == null)\n            throw new Exception(\"Action argument cannot be null\");\n        if (CheckState())\n        {\n            var lockTaken = false;\n            try\n            {\n                Monitor.Enter(localObject, ref lockTaken);\n                action();\n            }\n            finally\n            {\n                if (lockTaken)\n                    Monitor.Exit(localObject);\n            }\n        }\n        else\n        {\n            action();\n        }\n    }\n    public TResult Lock&lt;TResult>(object localObject, Func&lt;TResult> func)\n    {\n        if (func == null)\n            throw new Exception(\"Func argument cannot be null\");\n        if (CheckState())\n        {\n            var lockTaken = false;\n            try\n            {\n                Monitor.Enter(localObject, ref lockTaken);\n                return func();\n            }\n            finally\n            {\n                if (lockTaken)\n                    Monitor.Exit(localObject);\n            }\n        }\n        return func();\n    }\n    public (TResult1, TResult2) Lock&lt;TResult1, TResult2>(object localObject, Func&lt;(TResult1, TResult2)> func)\n    {\n        if (func == null)\n            throw new Exception(\"Func argument cannot be null\");\n        if (CheckState())\n        {\n            var lockTaken = false;\n            try\n            {\n                Monitor.Enter(localObject, ref lockTaken);\n                return func();\n            }\n            finally\n            {\n                if (lockTaken)\n                    Monitor.Exit(localObject);\n            }\n        }\n        return func();\n    }\n    public (TResult1, TResult2) Lock&lt;TArg, TResult1, TResult2>(object localObject, Func&lt;TArg, (TResult1, TResult2)> func, TArg arg)\n    {\n        if (func == null)\n            throw new Exception(\"Func argument cannot be null\");\n        if (CheckState())\n        {\n            var lockTaken = false;\n            try\n            {\n                Monitor.Enter(localObject, ref lockTaken);\n                return func(arg);\n            }\n            finally\n            {\n                if (lockTaken)\n                    Monitor.Exit(localObject);\n            }\n        }\n        return func(arg);\n    }\n    public TResult Lock&lt;TArg, TResult>(object localObject, Func&lt;TArg, TResult> func, TArg arg)\n    {\n        if (func == null)\n            throw new Exception(\"Func argument cannot be null\");\n        if (CheckState())\n        {\n            var lockTaken = false;\n            try\n            {\n                Monitor.Enter(localObject, ref lockTaken);\n                return func(arg);\n            }\n            finally\n            {\n                if (lockTaken)\n                    Monitor.Exit(localObject);\n            }\n        }\n        return func(arg);\n    }\n    public void Lock&lt;TArg>(object localObject, Action&lt;TArg> action, TArg arg)\n    {\n        if (action == null)\n            throw new Exception(\"Action argument cannot be null\");\n        if (CheckState())\n        {\n            var lockTaken = false;\n            try\n            {\n                Monitor.Enter(localObject, ref lockTaken);\n                action(arg);\n            }\n            finally\n            {\n                if (lockTaken)\n                    Monitor.Exit(localObject);\n            }\n        }\n        else\n        {\n            action(arg);\n        }\n    }\n}<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Monitor Lock Wrapper<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[2],"tags":[79,80,158],"_links":{"self":[{"href":"https:\/\/michaeljohnsteiner.com\/index.php\/wp-json\/wp\/v2\/posts\/323"}],"collection":[{"href":"https:\/\/michaeljohnsteiner.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/michaeljohnsteiner.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/michaeljohnsteiner.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/michaeljohnsteiner.com\/index.php\/wp-json\/wp\/v2\/comments?post=323"}],"version-history":[{"count":2,"href":"https:\/\/michaeljohnsteiner.com\/index.php\/wp-json\/wp\/v2\/posts\/323\/revisions"}],"predecessor-version":[{"id":326,"href":"https:\/\/michaeljohnsteiner.com\/index.php\/wp-json\/wp\/v2\/posts\/323\/revisions\/326"}],"wp:attachment":[{"href":"https:\/\/michaeljohnsteiner.com\/index.php\/wp-json\/wp\/v2\/media?parent=323"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/michaeljohnsteiner.com\/index.php\/wp-json\/wp\/v2\/categories?post=323"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/michaeljohnsteiner.com\/index.php\/wp-json\/wp\/v2\/tags?post=323"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}