Note that there are some explanatory texts on larger screens.

plurals
  1. PONeed help debugging why property is not set/null
    primarykey
    data
    text
    <p>I have a class <code>WorkQueue</code>, just take note about the <code>DoNext()</code> for now, the rest are mainly helpers. Basically, <code>WorkQueue</code> is just a Queue of <code>WorkItems</code>. <code>DoNext()</code> is responsible for "Starting a Pending WorkItem with a Free Background Worker". Also note that it will set the <code>WorkItem.Worker</code> property. </p> <pre><code>public class WorkQueue&lt;Tin, Tout&gt; : INotifyCollectionChanged, IEnumerable&lt;WorkItem&lt;Tin, Tout&gt;&gt; { public bool DoNext() { // check if any work left in queue WorkItem&lt;Tin, Tout&gt; item = GetWork(); if (item != null) { // check if any free workers BackgroundWorker worker = GetWorker(); Debug.WriteLine( "[WorkQueue.DoNext] Setting Worker to WorkItem: " + worker); item.Worker = worker; if (worker != null) { worker.RunWorkerAsync(item); return true; } } return false; } public void AddWork(WorkItem&lt;Tin, Tout&gt; item) { _queue.Add(item); RaiseCollectionChanged( new NotifyCollectionChangedEventArgs( NotifyCollectionChangedAction.Add, item)); } public WorkItem&lt;Tin, Tout&gt; GetWork() { return (from i in _queue where i.Status == WorkStatus.Pending select i).FirstOrDefault();; } public BackgroundWorker GetWorker() { return (from worker in _workers where worker.IsBusy == false select worker).FirstOrDefault(); } } </code></pre> <p>The problem I am having is when I do something like below, </p> <pre><code>foreach (string filename in fileNames) { UploadQueue.AddWork(new WorkItem&lt;string, UploadedImage&gt;(filename)); UploadQueue.DoNext(); } </code></pre> <p>Where <code>UploadQueue</code> is a <code>WorkQueue&lt;string, UploadedImage&gt;</code>. On the 1st (first only) <code>DoNext()</code>, the <code>WorkItem.Worker</code> is null. I know that because my Cancel button bound to <code>WorkItem.CancelCommand</code> is disabled. When debugging, I discovered the reason was because worker is null. </p> <pre><code>_cancelCommand = new RelayCommand(... () =&gt; { // Returns true if WorkItem is being processed with a worker that supports // cancellation or if the WorkItem is still Pending // False if otherwise, eg. already completed, cancelled etc if (Status == WorkStatus.Processing) { if (_worker != null &amp;&amp; _worker.WorkerSupportsCancellation) return true; } else if (Status == WorkStatus.Pending) { return true; } return false; }); </code></pre> <p>The solution is to move the <code>DoNext()</code> out of the loop, </p> <pre><code>foreach (string filename in fileNames) UploadQueue.AddWork(new WorkItem&lt;string, UploadedImage&gt;(filename)); UploadQueue.DoNext(); </code></pre> <p>but whats the problem with it inside, why is worker set to null? If its null, from the if clause, the <code>BackgroundWorker</code> should not start? </p> <pre><code>if (worker != null) worker.RunWorkerAsync(item); </code></pre> <p><a href="http://screenr.com/akF" rel="nofollow">Video Demonstrating the Problem</a></p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload