diff options
Diffstat (limited to 'main/src/core/MonoDevelop.Ide/MonoDevelop.Ide.ProgressMonitoring/BaseProgressMonitor.cs')
-rw-r--r-- | main/src/core/MonoDevelop.Ide/MonoDevelop.Ide.ProgressMonitoring/BaseProgressMonitor.cs | 316 |
1 files changed, 13 insertions, 303 deletions
diff --git a/main/src/core/MonoDevelop.Ide/MonoDevelop.Ide.ProgressMonitoring/BaseProgressMonitor.cs b/main/src/core/MonoDevelop.Ide/MonoDevelop.Ide.ProgressMonitoring/BaseProgressMonitor.cs index a7359939a2..348ce2aa23 100644 --- a/main/src/core/MonoDevelop.Ide/MonoDevelop.Ide.ProgressMonitoring/BaseProgressMonitor.cs +++ b/main/src/core/MonoDevelop.Ide/MonoDevelop.Ide.ProgressMonitoring/BaseProgressMonitor.cs @@ -38,317 +38,27 @@ using MonoDevelop.Ide.Gui.Dialogs; namespace MonoDevelop.Ide.ProgressMonitoring { - public class BaseProgressMonitor: GuiSyncObject, IProgressMonitor, IAsyncOperation + public static class ProgressHelper { - class MbrWrapper { - public ManualResetEvent waitEvent; - - //workaround for "** ERROR **: file icall.c: line 2419 (ves_icall_InternalExecute): assertion failed" bug when - //handling the CancelRequested event - public event MonitorHandler cancelRequestedEvent; - - public void RaiseEvent (IProgressMonitor monitor) { - if (cancelRequestedEvent != null) - cancelRequestedEvent (monitor); - } - } - - MbrWrapper c = new MbrWrapper (); - ProgressTracker progressTracker; - LogTextWriter logger; - bool canceled; - bool runningPendingEvents; - - event OperationHandler completedEvent; - - StringCollection errorsMessages = new StringCollection (); - StringCollection successMessages = new StringCollection (); - StringCollection warningMessages = new StringCollection (); - Exception errorException; - - public BaseProgressMonitor () - { - progressTracker = new ProgressTracker (); - logger = new LogTextWriter (); - logger.TextWritten += new LogTextEventHandler (WriteLogInternal); - } - - [FreeDispatch] - public object SyncRoot { - [FreeDispatch] - get { - // Dont return 'this'. Locking on proxies doesn't look like a good idea. - return progressTracker; - } - } - - [AsyncDispatch] - public virtual void BeginTask (string name, int totalWork) - { - progressTracker.BeginTask (name, totalWork); - OnProgressChanged (); - } - - [AsyncDispatch] - public virtual void BeginStepTask (string name, int totalWork, int stepSize) - { - progressTracker.BeginStepTask (name, totalWork, stepSize); - OnProgressChanged (); - } - - [AsyncDispatch] - public virtual void EndTask () - { - progressTracker.EndTask (); - } - - [AsyncDispatch] - public virtual void Step (int work) - { - progressTracker.Step (work); - OnProgressChanged (); - } - - public TextWriter Log { - [FreeDispatch] - get { - return logger; - } - } - - [FreeDispatch] - public virtual void ReportSuccess (string message) - { - successMessages.Add (message); - } - - [FreeDispatch] - public virtual void ReportWarning (string message) - { - warningMessages.Add (message); - } - - [FreeDispatch] - public virtual void ReportError (string message, Exception ex) - { - if (message == null && ex != null) { - var userEx = ex as UserException; - if (userEx != null) - message = string.Format ("{0}{1}{1}{2}", userEx.Message, Environment.NewLine, userEx.Details); - else - message = ex.Message; - } else if (message != null && ex != null) { - if (!message.EndsWith (".")) message += "."; - message += " " + ex.Message; - } - - errorsMessages.Add (message); - if (ex != null) { - LoggingService.LogError (ex.ToString ()); - errorException = ex; - } - } - - [FreeDispatch] - public virtual bool IsCancelRequested { - [FreeDispatch] - get { return canceled; } - } - - [AsyncDispatch] - public virtual void Dispose() - { - DispatchService.RunPendingEvents (); - - lock (progressTracker) { - progressTracker.Done (); - if (c.waitEvent != null) - c.waitEvent.Set (); - } - try { - OnCompleted (); - } catch (Exception ex) { - string msg = "Unhandled exception in monitor cancel event handler"; - LoggingService.LogError (msg, ex); - MessageService.ShowException (ex, msg); - } - } - - [FreeDispatch] - public IAsyncOperation AsyncOperation - { - [FreeDispatch] - get { return this; } - } - - [FreeDispatch] - void IAsyncOperation.Cancel () - { - OnCancelRequested (); - } - - [FreeDispatch] - void IAsyncOperation.WaitForCompleted () - { - if (IsCompleted) return; - - if (DispatchService.IsGuiThread) { - while (!IsCompleted) { - DispatchService.RunPendingEvents (); - Thread.Sleep (100); - } - } else { - lock (progressTracker) { - if (!progressTracker.InProgress) return; - if (c.waitEvent == null) - c.waitEvent = new ManualResetEvent (false); - } - c.waitEvent.WaitOne (); - } - } - - [FreeDispatch] - bool IAsyncOperation.Success { - [FreeDispatch] - get { return errorsMessages.Count == 0; } - } - - [FreeDispatch] - bool IAsyncOperation.SuccessWithWarnings { - [FreeDispatch] - get { return errorsMessages.Count == 0 && warningMessages.Count > 0; } - } - - public bool IsCompleted { - [FreeDispatch] - get { return !progressTracker.InProgress; } - } - - public event OperationHandler Completed { - add { - bool alreadyCompleted = false; - lock (progressTracker) { - completedEvent += value; - alreadyCompleted = !progressTracker.InProgress; - } - if (alreadyCompleted) value (this); - } - remove { - lock (progressTracker) { - completedEvent -= value; - } - } - } - - public event MonitorHandler CancelRequested { - [FreeDispatch] - add { - bool alreadyCanceled = false; - lock (progressTracker) { - c.cancelRequestedEvent += value; - alreadyCanceled = canceled; - } - if (alreadyCanceled) value (this); - } - [FreeDispatch] - remove { - lock (progressTracker) { - c.cancelRequestedEvent -= value; - } - } - } - - public Exception ErrorException { - get { return errorException; } - } - - public StringCollection Errors { - get { return errorsMessages; } - } - - public StringCollection SuccessMessages { - get { return successMessages; } - } - - public StringCollection Warnings { - get { return warningMessages; } - } - - protected string CurrentTask { - get { return progressTracker.CurrentTask; } - } - - protected double CurrentTaskWork { - get { return progressTracker.CurrentTaskWork; } - } - - protected double GlobalWork { - get { return progressTracker.GlobalWork; } - } - - protected bool UnknownWork { - get { return progressTracker.UnknownWork; } - } - - protected virtual void OnCompleted () - { - if (completedEvent != null) - completedEvent (AsyncOperation); - } - - protected virtual void OnCancelRequested () - { - lock (progressTracker) { - canceled = true; - } - - c.RaiseEvent(this); - } - - [AsyncDispatch] - void WriteLogInternal (string text) - { - OnWriteLog (text); - } - - protected virtual void OnWriteLog (string text) - { - } - - protected virtual void OnProgressChanged () - { - } - - protected void RunPendingEvents () - { - if (!runningPendingEvents) { - try { - runningPendingEvents = true; - DispatchService.RunPendingEvents (); - } finally { - runningPendingEvents = false; - } - } - } - - protected void ShowResultDialog () + public static void ShowResultDialog (this ProgressMonitor monitor) { - if (Errors.Count == 1 && Warnings.Count == 0) { - if (ErrorException != null) - MessageService.ShowException (ErrorException, Errors[0]); + if (monitor.Errors.Length == 1 && !monitor.HasWarnings) { + var error = monitor.Errors [0]; + if (error.Exception != null) + MessageService.ShowException (error.Exception, error.Message); else - MessageService.ShowError (Errors[0]); + MessageService.ShowError (error.Message); } - else if (Errors.Count == 0 && Warnings.Count == 1) { - MessageService.ShowWarning (Warnings[0]); + else if (!monitor.HasErrors && monitor.Warnings.Length == 1) { + MessageService.ShowWarning (monitor.Warnings[0]); } - else if (Errors.Count > 0 || Warnings.Count > 0) { + else if (monitor.HasErrors || monitor.HasWarnings) { var resultDialog = new MultiMessageDialog () { Modal = true, }; - foreach (string m in Errors) - resultDialog.AddError (m); - foreach (string m in Warnings) + foreach (var m in monitor.Errors) + resultDialog.AddError (m.Message); + foreach (var m in monitor.Warnings) resultDialog.AddWarning (m); MessageService.ShowCustomDialog (resultDialog); } |