private static void Exec() { if (top != null) { // determine amount of jobs BackgroundFb next = top.next; int amount = 1; while (next != null) { amount++; next = next.next; } // execute this amount of jobs // if jobs are added during execution of this jobs then // these jobs are executed in a next cycle while (top != null && amount != 0) { BackgroundFb current = top; lock (worker) { BackgroundFb temp = top; // remember top object to set top.next to null top = top.next; // next job temp.next = null; } amount--; current.Exec(); // execute job } } }
internal static bool AddJob(BackgroundFb fb) { bool result = false; if (Resource.Running) { if (fb != null) { fb.Exec(); result = true; } } else { lock (worker) { if (top == null) { top = fb; } else { bool bBreak = false; BackgroundFb iter = top; while (iter.next != null) { if (iter.next == fb) { // an object is allowed to exist only once in the queue bBreak = true; break; } iter = iter.next; } if (!bBreak) { if (iter != fb) // avoid recursion in chain (important if top.next == null) { iter.next = fb; result = true; } } } } } return(result); }