{"id":6324,"date":"2022-06-17T10:20:09","date_gmt":"2022-06-17T09:20:09","guid":{"rendered":"https:\/\/solidt.eu\/site\/?p=6324"},"modified":"2023-09-16T09:55:56","modified_gmt":"2023-09-16T08:55:56","slug":"c-basic-throttle","status":"publish","type":"post","link":"https:\/\/solidt.eu\/site\/c-basic-throttle\/","title":{"rendered":"C# Basic Throttle"},"content":{"rendered":"\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 class Throttle\n{\n    public Throttler(TimeSpan throttleTime) { ThrottleTime = throttleTime; }\n    private DateTime? Last = null;\n    private readonly TimeSpan ThrottleTime;\n    private readonly object Locker = new();\n    public DateTime? LastUsed() => Last ?? DateTime.Now;\n    public void Update() => Last = DateTime.Now;\n    public bool CanFire()\n    {\n        lock(Locker) \n        { \n            bool canFire = Last == null || (DateTime.Now - Last).Value > ThrottleTime;\n            if (canFire) Update();\n            return canFire;\n        }\n    }\n}\n\n\n\/\/ Usage:\npublic Throttle PopupOpenThrottle = new Throttle(TimeSpan.FromSeconds(1));\n\nprivate void LeVariant_PopupClosing(object sender, ClosingPopupEventArgs e)\n{\n\tPopupOpenThrottle.Update();\n}\n\nprivate void LeVariant_GotFocus(object sender, RoutedEventArgs e)\n{\n\tif (sender is VariantEditor editor) {\n\t\tif (!editor.IsPopupOpen &amp;&amp; PopupOpenThrottle.CanFire())\n\t\t{ \n\t\t\teditor.ShowPopup();\n\t\t}\n\t}\n}\n\n\npublic class ThrottleDictionary\n{\n    public ThrottleDictionary(TimeSpan throttleTime) \n    {\n        this.throttleTime = throttleTime;\n    }\n    private readonly Dictionary&lt;string, Throttle> keyValuePairs = new Dictionary&lt;string, Throttle>();\n    private readonly TimeSpan throttleTime;\n\n    public Throttle Get(string key) => keyValuePairs.GetOrAdd(key, () => new Throttle(throttleTime));\n}\n<\/pre><\/div>\n","protected":false},"excerpt":{"rendered":"","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-6324","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\/6324","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=6324"}],"version-history":[{"count":4,"href":"https:\/\/solidt.eu\/site\/wp-json\/wp\/v2\/posts\/6324\/revisions"}],"predecessor-version":[{"id":8027,"href":"https:\/\/solidt.eu\/site\/wp-json\/wp\/v2\/posts\/6324\/revisions\/8027"}],"wp:attachment":[{"href":"https:\/\/solidt.eu\/site\/wp-json\/wp\/v2\/media?parent=6324"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/solidt.eu\/site\/wp-json\/wp\/v2\/categories?post=6324"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/solidt.eu\/site\/wp-json\/wp\/v2\/tags?post=6324"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}