/// <summary> /// Perform operation in a worker thread with wait dialog in main thread. /// </summary> /// <param name="op">Delegate of operation to be performed.</param> /// <param name="parameter">Parameter to be passed into the operation.</param> /// <returns>Result of performing the operation.</returns> public static object PerformOperation(Operation op, object parameter) { // Create a modal window for showing feedback using (ModalWaitDialog wait = new ModalWaitDialog()) { // Create the object that runs the operation in a separate thread OperationThread opThread = new OperationThread(op, parameter); // Create the actual thread and provide thread entry point Thread thread = new Thread(new ThreadStart(opThread.Run)); // Kick off the thread action thread.Start(); // Keep looping until the thread is finished while (opThread.State == 0) { // Sleep to allow thread to perform more work Thread.Sleep(25); // Give the feedback dialog a chance to update wait.UpdateDialog(); } // Process operation result switch (opThread.State) { case 1: return opThread.Result; case 2: throw opThread.Exception; default: // Should never happen! Debug.Assert(false); return null; } } }