{"id":5395,"date":"2021-09-28T13:41:31","date_gmt":"2021-09-28T12:41:31","guid":{"rendered":"https:\/\/solidt.eu\/site\/?p=5395"},"modified":"2022-07-25T08:31:28","modified_gmt":"2022-07-25T07:31:28","slug":"c-tryagain","status":"publish","type":"post","link":"https:\/\/solidt.eu\/site\/c-tryagain\/","title":{"rendered":"C# TryAgain"},"content":{"rendered":"\n<p>For the methods that at random do not work<\/p>\n\n\n\n<div style=\"height: 250px; position:relative; margin-bottom: 50px;\" class=\"wp-block-simple-code-block-ace\"><pre class=\"wp-block-simple-code-block-ace\" style=\"position:absolute;top:0;right:0;bottom:0;left:0\" data-mode=\"csharp\" data-theme=\"monokai\" data-fontsize=\"14\" data-lines=\"Infinity\" data-showlines=\"true\" data-copy=\"false\">public static T RunSync&lt;T>(Func&lt;Task&lt;T>> fn) => Task.Run(fn).GetAwaiter().GetResult();\n\npublic static T TryAgain&lt;T>(Func&lt;Task&lt;T>> action, int count = 3)\n{\n\tException lastException = null;\n\twhile(count > 0)\n\t{\n\t\ttry\n\t\t{\n\t\t\tvar result = RunSync(async () => await action());\n\t\t\treturn result;\n\t\t}\n\t\tcatch (Exception ex) \n\t\t{\n\t\t\tlastException = ex;\n\t\t}\n\t\tThread.Sleep(200); \/\/ This can be adjusted at will\n\t\tcount -= 1;\n\t}\n\tthrow lastException;\n}\n\n\/\/ Example usage:\n\nvar prodOrder = TryAgain(async () =>\n{\n\t\/\/ next line works like a traffic light\n\tprodOrder = await api.Query&lt;ProductionOrder>(x => x.id == id);\n\tif (prodOrder == null)\n\t\tthrow new Exception(\"Production order not found!\");\n\treturn prodOrder;\n});<\/pre><\/div>\n","protected":false},"excerpt":{"rendered":"<p>For the methods that at random do not work<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"inline_featured_image":false,"footnotes":""},"categories":[6,4,1],"tags":[],"class_list":["post-5395","post","type-post","status-publish","format-standard","hentry","category-dotnet","category-programming","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/solidt.eu\/site\/wp-json\/wp\/v2\/posts\/5395","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/solidt.eu\/site\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/solidt.eu\/site\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/solidt.eu\/site\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/solidt.eu\/site\/wp-json\/wp\/v2\/comments?post=5395"}],"version-history":[{"count":5,"href":"https:\/\/solidt.eu\/site\/wp-json\/wp\/v2\/posts\/5395\/revisions"}],"predecessor-version":[{"id":5466,"href":"https:\/\/solidt.eu\/site\/wp-json\/wp\/v2\/posts\/5395\/revisions\/5466"}],"wp:attachment":[{"href":"https:\/\/solidt.eu\/site\/wp-json\/wp\/v2\/media?parent=5395"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/solidt.eu\/site\/wp-json\/wp\/v2\/categories?post=5395"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/solidt.eu\/site\/wp-json\/wp\/v2\/tags?post=5395"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}