{"id":579,"date":"2014-02-16T23:58:24","date_gmt":"2014-02-17T03:58:24","guid":{"rendered":"http:\/\/www.peteonsoftware.com\/?p=579"},"modified":"2024-03-02T16:24:26","modified_gmt":"2024-03-02T21:24:26","slug":"executing-on-the-background-thread-with-postsharp","status":"publish","type":"post","link":"https:\/\/www.peteonsoftware.com\/index.php\/2014\/02\/16\/executing-on-the-background-thread-with-postsharp\/","title":{"rendered":"Executing on the Background Thread with PostSharp"},"content":{"rendered":"<p><img decoding=\"async\" src=\"https:\/\/www.peteonsoftware.com\/images\/201402\/BackgroundThread.jpg\" alt=\"Background Thread\" title=\"Background Thread\" style=\"float:left; margin: .5em\" \/>In my <a href=\"https:\/\/www.peteonsoftware.com\/index.php\/2014\/02\/08\/parameter-checking-with-postsharp\/\">last<\/a> <a href=\"https:\/\/www.peteonsoftware.com\/index.php\/2014\/02\/03\/my-intro-to-aop-with-postsharp\/\">two<\/a> PostSharp blog posts, I looked at creating some custom attributes that you could use to get custom behavior out of PostSharp.  Of course, I was implementing simple functionality and in fact, PostSharp had most of if not all of those features already built in.<\/p>\n<p>Today, I want to dig in a little more to some of the built in power of PostSharp.  To do this, I had to sign up for the 45 day evaluation license for PostSharp Ultimate.  I had been putting that off to do as much &#8220;free stuff&#8221; as I could, but I really wanted to see what PostSharp could do, so I fired up the evaluation.<\/p>\n<p>I created a very simple UI that looks like this:<br \/>\n<img decoding=\"async\" src=\"https:\/\/www.peteonsoftware.com\/images\/201402\/Aop3LongRunningUi.png\" alt=\"Simple &quot;Long Running Process&quot; UI\" title=\"Simple &quot;Long Running Process&quot; UI\" \/><\/p>\n<p>When you click the buttons, I call a class that just sleeps for three seconds and returns to simulate some slow process.<\/p>\n<pre>\r\npublic void LongRunningOperationBlocking()\r\n{\r\n    Thread.Sleep(3000);\r\n    return;\r\n}\r\n<\/pre>\n<p>I created code that will run this blocking process and then when the UI thread is free again, it displays a message box with whatever was in the textbox.  Here is the UI in action.  Note that I cannot select the text after clicking the button because the UI is locked up for 3 seconds.<br \/>\n<img decoding=\"async\" src=\"https:\/\/www.peteonsoftware.com\/images\/201402\/BlockingFail.gif\" alt=\"Blocking Fail\" title=\"Blocking Fail\" \/><\/p>\n<p>However, with PostSharp, we can easily execute these long running methods on a background thread.  As long as the method returns void, the context menu will reveal the &#8220;Execute method in the background&#8221; option like this:<br \/>\n<img decoding=\"async\" src=\"https:\/\/www.peteonsoftware.com\/images\/201402\/PostSharpBackgroundContextMenu.png\" alt=\"The PostSharp Context Menu Showing Background Thread Option\" title=\"The PostSharp Context Menu Showing Background Thread Option\" \/><\/p>\n<p>After selecting that option, you get this wizard:<br \/>\n<img decoding=\"async\" src=\"https:\/\/www.peteonsoftware.com\/images\/201402\/PostSharpBackgroundWizard.png\" alt=\"The PostSharp Add a Feature Wizard\" title=\"The PostSharp Add a Feature Wizard\" \/><\/p>\n<p>The sum total of the &#8220;visible&#8221; changes is that our other method got a new attribute, &#8220;[Background]&#8221;<\/p>\n<pre>\r\n[Background]\r\npublic void LongRunningOperationBackground()\r\n{\r\n    Thread.Sleep(3000);\r\n    return;\r\n}\r\n<\/pre>\n<p>But, the win is that now when I click the button, the UI returns immediately and we see the message box right away, even while the long running process is going.<br \/>\n<img decoding=\"async\" src=\"https:\/\/www.peteonsoftware.com\/images\/201402\/NonBlockingWin.gif\" alt=\"Blocking Fail\" title=\"Blocking Fail\" \/><\/p>\n<p>That is a gigantic amount of threading code that we don&#8217;t have to write.  It is only probably 4-6 lines per method, but you have to write the same thing over and over again for every method you want to have do work on a background thread.  Of course, any time you can just cut out &#8220;boilerplate code&#8221;, you make your code not only easier to write, but easier to read and discern intent.  And that is &#8220;A Good Thing&#8482;&#8221;!<\/p>\n<p>As always, you can download the code for this series from my Github repository for it.  The path to this post&#8217;s code is <a href=\"https:\/\/github.com\/PeteShearer\/IntroToAopWithPostSharp\/tree\/master\/IntroToAop3\">here<\/a>.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>In my last two PostSharp blog posts, I looked at creating some custom attributes that you could use to get custom behavior out of PostSharp. Of course, I was implementing simple functionality and in fact, &hellip;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[70,71,72],"tags":[124,126],"class_list":["post-579","post","type-post","status-publish","format-standard","hentry","category-aop","category-aspect-oriented-programming","category-postsharp","tag-aop","tag-postsharp"],"_links":{"self":[{"href":"https:\/\/www.peteonsoftware.com\/index.php\/wp-json\/wp\/v2\/posts\/579","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.peteonsoftware.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.peteonsoftware.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.peteonsoftware.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.peteonsoftware.com\/index.php\/wp-json\/wp\/v2\/comments?post=579"}],"version-history":[{"count":0,"href":"https:\/\/www.peteonsoftware.com\/index.php\/wp-json\/wp\/v2\/posts\/579\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.peteonsoftware.com\/index.php\/wp-json\/wp\/v2\/media?parent=579"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.peteonsoftware.com\/index.php\/wp-json\/wp\/v2\/categories?post=579"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.peteonsoftware.com\/index.php\/wp-json\/wp\/v2\/tags?post=579"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}