{"id":71,"date":"2008-05-01T11:29:38","date_gmt":"2008-05-01T15:29:38","guid":{"rendered":"http:\/\/jianmingli.com\/wp\/?p=71"},"modified":"2008-05-20T10:32:31","modified_gmt":"2008-05-20T14:32:31","slug":"net-backgroundworker-component","status":"publish","type":"post","link":"https:\/\/jianmingli.com\/wp\/?p=71","title":{"rendered":".Net BackgroundWorker Component"},"content":{"rendered":"<p><strong>Overview<\/strong><\/p>\n<ul>\n<li>Is a helper class in System.ComponentModel namespace<\/li>\n<li>Manages a worker thread<\/li>\n<li>Handles automatic marshaling\/invoking: can update Windows forms <\/li>\n<\/ul>\n<p><strong>Use BackgroundWorder<\/strong><\/p>\n<ul>\n<li>Add a BackgroundWorker component to the form.<\/li>\n<li>Run it:\n<pre lang=\"csharp\">backgroundWorker1.RunWorkerAsync();\r\n\/\/ Parameter can be pass to worker as well\r\nbackgroundWorker1.RunWorkerAsync(Object param);<\/pre>\n<\/li>\n<li>Implement actual work in the DoWork event:\n<pre lang=\"csharp\">private void backgroundWorker1_DoWork(\r\nobject sender, DoWorkEventArgs e){ \r\n\/\/ long task... \r\n}<\/pre>\n<\/li>\n<li>Cleanup when done:\n<pre lang=\"csharp\">private void backgroundWorker1_RunWorkerCompleted(\r\nobject sender, RunWorkerCompletedEventArgs e){ \r\n\/\/ Clean up... \r\n}<\/pre>\n<\/li>\n<\/ul>\n<p><strong>To be able to cancle background work<\/strong><\/p>\n<ul>\n<li>Enable worker cancellation support:\n<pre lang=\"csharp\">backgroundWorker1\r\n  .WorkerSupportsCancellation = true;<\/pre>\n<\/li>\n<li>Cancel worker:\n<pre lang=\"csharp\">backgroundWorker1.CancelAsync();<\/pre>\n<\/li>\n<li>Check cancel event in DoWork event\n<pre lang=\"csharp\">private void backgroundWorker1_DoWork(\r\nobject sender, DoWorkEventArgs e){ \r\n  \/\/ long task... \r\n  BackgroundWorker worker= (BackgroundWorker)sender;\r\n  If (worker.CancellationPending == true) {\r\n    e.Cancel = True\r\n    \/\/ Cancel long task\r\n  }\r\n}<\/pre>\n<\/li>\n<li>Check cancel event in RunWorkerCompleted event\n<pre lang=\"csharp\">private void backgroundWorker1_RunWorkerCompleted(\r\nobject sender, RunWorkerCompletedEventArgs e){ \r\n  \/\/ Clean up... \r\n  if (e.Cancelled){\r\n    MessageBox.Show(\"Cancelled\");\r\n  }\r\n}<\/pre>\n<\/li>\n<\/ul>\n<p><strong>If need to report progress<\/strong><\/p>\n<ul>\n<li>Enable backgrounWorker progress report:\n<pre lang=\"csharp\">backgroundWorker1.WorkerReportsProgress = true;<\/pre>\n<\/li>\n<li>Add a ProgressBar component to the form<\/li>\n<li>Update progress bar in ProgressChanged event:\n<pre lang=\"csharp\">private void backgroundWorker1_ProgressChanged(\r\nobject sender, ProgressChangedEventArgs e){\r\n  BackgroundWorker worker = \r\n    (BackgroundWorker)sender;\r\n  worker.ReportProgress(10);\r\n}<\/pre>\n<\/li>\n<\/ul>\n<p><strong>Reference:<\/strong><\/p>\n<ul>\n<li><a href=\"http:\/\/www.ondotnet.com\/pub\/a\/dotnet\/2005\/07\/25\/backgroundworker.html\">Understanding the BackgroundWorker Component<\/a><\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>Overview Is a helper class in System.ComponentModel namespace Manages a worker thread Handles automatic marshaling\/invoking: can update Windows forms Use BackgroundWorder Add a BackgroundWorker component to the form. Run it: backgroundWorker1.RunWorkerAsync(); \/\/ Parameter can be pass to worker as well &hellip; <a href=\"https:\/\/jianmingli.com\/wp\/?p=71\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_exactmetrics_skip_tracking":false,"_exactmetrics_sitenote_active":false,"_exactmetrics_sitenote_note":"","_exactmetrics_sitenote_category":0,"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":false,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2}},"categories":[5],"tags":[],"class_list":["post-71","post","type-post","status-publish","format-standard","hentry","category-c"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/p8cRUO-19","_links":{"self":[{"href":"https:\/\/jianmingli.com\/wp\/index.php?rest_route=\/wp\/v2\/posts\/71","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/jianmingli.com\/wp\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/jianmingli.com\/wp\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/jianmingli.com\/wp\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/jianmingli.com\/wp\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=71"}],"version-history":[{"count":0,"href":"https:\/\/jianmingli.com\/wp\/index.php?rest_route=\/wp\/v2\/posts\/71\/revisions"}],"wp:attachment":[{"href":"https:\/\/jianmingli.com\/wp\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=71"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/jianmingli.com\/wp\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=71"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/jianmingli.com\/wp\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=71"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}