Hydrogen Framework  1.3.1
 All Classes Namespaces Functions Variables Enumerations Enumerator Properties Events
Hydrogen.Threading.Forker Class Reference

Provides a caller-friendly wrapper around parallel actions. More...

Classes

class  ParallelEventArgs
 Event arguments representing the completion of a parallel action. More...
 

Public Member Functions

int CountRunning ()
 Indicates the number of incomplete operations. More...
 
Forker Fork (ThreadStart action)
 Enqueues an operation. More...
 
Forker Fork (ThreadStart action, object state)
 Enqueues an operation. More...
 
void Join ()
 Waits for all operations to complete. More...
 
bool Join (int millisecondsTimeout)
 Waits (with timeout) for all operations to complete. More...
 
Forker OnItemComplete (EventHandler< ParallelEventArgs > handler)
 Adds a callback to invoke when each operation completes. More...
 
Forker OnAllComplete (EventHandler handler)
 Adds a callback to invoke when all operations are complete. More...
 

Events

EventHandler AllComplete [add, remove]
 Raised when all operations have completed. More...
 
EventHandler< ParallelEventArgsItemComplete [add, remove]
 Raised when each operation completes. More...
 

Detailed Description

Provides a caller-friendly wrapper around parallel actions.

Definition at line 38 of file Forker.cs.

Member Function Documentation

int Hydrogen.Threading.Forker.CountRunning ( )

Indicates the number of incomplete operations.

Returns
The number of incomplete operations.

Definition at line 97 of file Forker.cs.

98  {
99  return Interlocked.CompareExchange (ref _runningCount, 0, 0);
100  }
Forker Hydrogen.Threading.Forker.Fork ( ThreadStart  action)

Enqueues an operation.

Parameters
actionThe operation to perform.
Returns
The current instance (for fluent API).

Definition at line 107 of file Forker.cs.

108  {
109  return Fork (action, null);
110  }
Forker Fork(ThreadStart action)
Enqueues an operation.
Definition: Forker.cs:107
Forker Hydrogen.Threading.Forker.Fork ( ThreadStart  action,
object  state 
)

Enqueues an operation.

Parameters
actionThe operation to perform.
stateAn opaque object, allowing the caller to identify operations.
Returns
The current instance (for fluent API).

Definition at line 118 of file Forker.cs.

119  {
120  if (action == null)
121  throw new ArgumentNullException ("action");
122  Interlocked.Increment (ref _runningCount);
123  ThreadPool.QueueUserWorkItem (delegate {
124  Exception exception = null;
125  try {
126  action ();
127  } catch (Exception ex) {
128  exception = ex;
129  }
130  OnItemComplete (state, exception);
131  });
132  return this;
133  }
Forker OnItemComplete(EventHandler< ParallelEventArgs > handler)
Adds a callback to invoke when each operation completes.
Definition: Forker.cs:162
void Hydrogen.Threading.Forker.Join ( )

Waits for all operations to complete.

Definition at line 138 of file Forker.cs.

139  {
140  Join (-1);
141  }
void Join()
Waits for all operations to complete.
Definition: Forker.cs:138
bool Hydrogen.Threading.Forker.Join ( int  millisecondsTimeout)

Waits (with timeout) for all operations to complete.

Returns
Whether all operations had completed before the timeout.

Definition at line 147 of file Forker.cs.

148  {
149  lock (_joinLock) {
150  if (CountRunning () == 0)
151  return true;
152  Thread.SpinWait (1); // try our luck...
153  return (CountRunning () == 0) ||
154  Monitor.Wait (_joinLock, millisecondsTimeout);
155  }
156  }
int CountRunning()
Indicates the number of incomplete operations.
Definition: Forker.cs:97
Forker Hydrogen.Threading.Forker.OnAllComplete ( EventHandler  handler)

Adds a callback to invoke when all operations are complete.

Returns
Current instance (for fluent API).

Definition at line 174 of file Forker.cs.

175  {
176  if (handler == null)
177  throw new ArgumentNullException ("handler");
178  AllComplete += handler;
179  return this;
180  }
EventHandler AllComplete
Raised when all operations have completed.
Definition: Forker.cs:64
Forker Hydrogen.Threading.Forker.OnItemComplete ( EventHandler< ParallelEventArgs handler)

Adds a callback to invoke when each operation completes.

Returns
Current instance (for fluent API).

Definition at line 162 of file Forker.cs.

163  {
164  if (handler == null)
165  throw new ArgumentNullException ("handler");
166  ItemComplete += handler;
167  return this;
168  }
EventHandler< ParallelEventArgs > ItemComplete
Raised when each operation completes.
Definition: Forker.cs:80

Event Documentation

EventHandler Hydrogen.Threading.Forker.AllComplete
addremove

Raised when all operations have completed.

Definition at line 64 of file Forker.cs.

EventHandler< ParallelEventArgs> Hydrogen.Threading.Forker.ItemComplete
addremove

Raised when each operation completes.

Definition at line 80 of file Forker.cs.