tag:blogger.com,1999:blog-29331675.post5272006056636953580..comments2024-03-05T17:37:00.995+01:00Comments on The Delphi Geek: Background Workergabr42http://www.blogger.com/profile/06903558857617342477noreply@blogger.comBlogger6125tag:blogger.com,1999:blog-29331675.post-63251595722586153712014-03-08T19:05:45.829+01:002014-03-08T19:05:45.829+01:00Main thread can always maintain a simple counter c...Main thread can always maintain a simple counter containing number of items that were sent to the worker but are yet unprocessed.<br /><br />As for the second problem, I'll check the code and report the findings here.gabr42https://www.blogger.com/profile/06903558857617342477noreply@blogger.comtag:blogger.com,1999:blog-29331675.post-75732925782114599932014-03-08T14:58:56.362+01:002014-03-08T14:58:56.362+01:00The Worker doesn't need to know but the main t...The Worker doesn't need to know but the main thread that creates the worker does so that it can check for new unprocessed orders and then schedule more tasks. For now, I have a workaround that captures workitem.UniqueID and the done callback uses it to know when to check for the next batch of unprocessed orders. This is not guaranteed to work in all cases as the last task might complete quicker than previous ones.<br /><br />Also, the WaitFor method of BackgroundWorker is not working as I understand described on page 53 of your book. It seems to return before the done method for each task is called and returns. This is leading to a memory leak in my code as I use the done method to free allocated objects that were passed into the tasks' Create method. Is there a better place to cleanup after completed tasks?<br /><br />To reproduce the error, modify test_52_BackgroundWorker.pas as follows:<br />- Create new Terminate button with below code<br />- Click on Work x3! button<br />- Immediately click on Terminate button<br />- Message from the three scheduled tasks HandleRequestDone2 method is never written to the log<br /><br />procedure TForm16.btnTerminateClick(Sender: TObject);<br />begin<br /> lbLog.ItemIndex := lbLog.Items.Add('Starting WaitFor / Terminate test');<br /> FBackgroundWorker2.WaitFor(INFINITE);<br /> lbLog.ItemIndex := lbLog.Items.Add('After WaitFor');<br /> FBackgroundWorker2 := nil;<br />end;<br />Timothy Vogelhttps://www.blogger.com/profile/14588482114269155962noreply@blogger.comtag:blogger.com,1999:blog-29331675.post-70021498515211661302014-03-07T09:30:15.413+01:002014-03-07T09:30:15.413+01:00Why would the worker need to know where there are ...Why would the worker need to know where there are more scheduled items? I don't see any good reason for that.gabr42https://www.blogger.com/profile/06903558857617342477noreply@blogger.comtag:blogger.com,1999:blog-29331675.post-83471806332176778182014-03-04T06:39:14.667+01:002014-03-04T06:39:14.667+01:00I am using the BackgroundWorker abstraction to pro...I am using the BackgroundWorker abstraction to process "orders". I get the orders that have been submitted and schedule a WorkItem for each order. When all the orders have been processed, I need to go back and check for new orders. I need a way to know when there are no more scheduled items. I have looked in the code, your book and webinars but don't see where this is a capability.<br />How would you suggest I handle this use case?Timothy Vogelhttps://www.blogger.com/profile/14588482114269155962noreply@blogger.comtag:blogger.com,1999:blog-29331675.post-88396082997929463832012-01-12T14:24:34.259+01:002012-01-12T14:24:34.259+01:00Yes, there will be an example.
This pattern solve...Yes, there will be an example.<br /><br />This pattern solves the problem of a background worker accepting work units which can be canceled at any time. In a way it is very similar to a single-stage pipeline but with added cancellation support.gabr42https://www.blogger.com/profile/06903558857617342477noreply@blogger.comtag:blogger.com,1999:blog-29331675.post-23271455640886310292012-01-11T19:53:15.866+01:002012-01-11T19:53:15.866+01:00Neat implementation, Primoz! Thanks!
Will there b...Neat implementation, Primoz! Thanks! <br />Will there be any real life examples? What tasks is this pattern intended to solve?Anton Alisovhttps://www.blogger.com/profile/08259023172071661864noreply@blogger.com