Browse Source

Implemented AsynchronousWaitDialog.ShowWaitDialogForAsyncOperation() API.

pull/326/merge
Andreas Weizel 12 years ago
parent
commit
6c6a55d0b7
  1. 9
      src/AddIns/BackendBindings/CSharpBinding/Project/Src/FormsDesigner/CSharpDesignerLoader.cs
  2. 8
      src/AddIns/DisplayBindings/WpfDesign/WpfDesign.AddIn/Src/WpfViewContent.cs
  3. 10
      src/Main/Base/Project/Src/Editor/Commands/FindReferencesCommand.cs
  4. 101
      src/Main/Base/Project/Src/Gui/Dialogs/AsynchronousWaitDialog.cs

9
src/AddIns/BackendBindings/CSharpBinding/Project/Src/FormsDesigner/CSharpDesignerLoader.cs

@ -30,6 +30,7 @@ using ICSharpCode.NRefactory.CSharp.Resolver;
using ICSharpCode.NRefactory.Editor; using ICSharpCode.NRefactory.Editor;
using ICSharpCode.NRefactory.TypeSystem; using ICSharpCode.NRefactory.TypeSystem;
using ICSharpCode.SharpDevelop; using ICSharpCode.SharpDevelop;
using ICSharpCode.SharpDevelop.Gui;
using ICSharpCode.SharpDevelop.Refactoring; using ICSharpCode.SharpDevelop.Refactoring;
using Microsoft.CSharp; using Microsoft.CSharp;
using CSharpBinding.Parser; using CSharpBinding.Parser;
@ -239,14 +240,18 @@ namespace CSharpBinding.FormsDesigner
controlSymbol = designerClass.GetFields(f => f.Name == oldName, GetMemberOptions.IgnoreInheritedMembers) controlSymbol = designerClass.GetFields(f => f.Name == oldName, GetMemberOptions.IgnoreInheritedMembers)
.SingleOrDefault(); .SingleOrDefault();
if (controlSymbol != null) { if (controlSymbol != null) {
FindReferenceService.RenameSymbol(controlSymbol, newName, new DummyProgressMonitor()) AsynchronousWaitDialog.ShowWaitDialogForAsyncOperation(
"${res:SharpDevelop.Refactoring.Rename}",
progressMonitor =>
FindReferenceService.RenameSymbol(controlSymbol, newName, progressMonitor)
.ObserveOnUIThread() .ObserveOnUIThread()
.Subscribe(error => SD.MessageService.ShowError(error.Message), // onNext .Subscribe(error => SD.MessageService.ShowError(error.Message), // onNext
ex => SD.MessageService.ShowException(ex), // onError ex => SD.MessageService.ShowException(ex), // onError
// onCompleted - force refresh of the DesignerCodeFile's parse info, because the code generator // onCompleted - force refresh of the DesignerCodeFile's parse info, because the code generator
// seems to work with an outdated version, when the document is saved. // seems to work with an outdated version, when the document is saved.
() => SD.ParserService.Parse(new FileName(context.DesignerCodeFileDocument.FileName), context.DesignerCodeFileDocument) () => SD.ParserService.Parse(new FileName(context.DesignerCodeFileDocument.FileName), context.DesignerCodeFileDocument)
); )
);
} }
} }

8
src/AddIns/DisplayBindings/WpfDesign/WpfDesign.AddIn/Src/WpfViewContent.cs

@ -234,7 +234,10 @@ namespace ICSharpCode.WpfDesign.AddIn
ISymbol controlSymbol = designerClass.GetFields(f => f.Name == propertyGridView.PropertyGrid.OldName, GetMemberOptions.IgnoreInheritedMembers) ISymbol controlSymbol = designerClass.GetFields(f => f.Name == propertyGridView.PropertyGrid.OldName, GetMemberOptions.IgnoreInheritedMembers)
.SingleOrDefault(); .SingleOrDefault();
if (controlSymbol != null) { if (controlSymbol != null) {
FindReferenceService.RenameSymbol(controlSymbol, propertyGridView.PropertyGrid.Name, new DummyProgressMonitor()) AsynchronousWaitDialog.ShowWaitDialogForAsyncOperation(
"${res:SharpDevelop.Refactoring.Rename}",
progressMonitor =>
FindReferenceService.RenameSymbol(controlSymbol, propertyGridView.PropertyGrid.Name, progressMonitor)
.ObserveOnUIThread() .ObserveOnUIThread()
.Subscribe(error => SD.MessageService.ShowError(error.Message), // onNext .Subscribe(error => SD.MessageService.ShowError(error.Message), // onNext
ex => SD.MessageService.ShowException(ex), // onError ex => SD.MessageService.ShowException(ex), // onError
@ -244,7 +247,8 @@ namespace ICSharpCode.WpfDesign.AddIn
SD.ParserService.ParseAsync(fileName).FireAndForget(); SD.ParserService.ParseAsync(fileName).FireAndForget();
} }
} }
); )
);
} }
} }

10
src/Main/Base/Project/Src/Editor/Commands/FindReferencesCommand.cs

@ -65,10 +65,14 @@ namespace ICSharpCode.SharpDevelop.Editor.Commands
NewSymbolName = entity.Name NewSymbolName = entity.Name
}; };
if ((bool) renameDialog.ShowDialog()) { if ((bool) renameDialog.ShowDialog()) {
using (IProgressMonitor progressMonitor = AsynchronousWaitDialog.ShowWaitDialog("${res:SharpDevelop.Refactoring.Rename}")) // using (IProgressMonitor progressMonitor = AsynchronousWaitDialog.ShowWaitDialog("${res:SharpDevelop.Refactoring.Rename}"))
AsynchronousWaitDialog.ShowWaitDialogForAsyncOperation(
"${res:SharpDevelop.Refactoring.Rename}",
progressMonitor =>
FindReferenceService.RenameSymbol(entity, renameDialog.NewSymbolName, progressMonitor) FindReferenceService.RenameSymbol(entity, renameDialog.NewSymbolName, progressMonitor)
.ObserveOnUIThread() .ObserveOnUIThread()
.Subscribe(error => SD.MessageService.ShowError(error.Message), ex => SD.MessageService.ShowException(ex), () => {}); .Subscribe(error => SD.MessageService.ShowError(error.Message), ex => SD.MessageService.ShowException(ex), () => {}));
} }
} }
} }

101
src/Main/Base/Project/Src/Gui/Dialogs/AsynchronousWaitDialog.cs

@ -66,6 +66,8 @@ namespace ICSharpCode.SharpDevelop.Gui
readonly string defaultTaskName; readonly string defaultTaskName;
readonly CancellationTokenSource cancellation; readonly CancellationTokenSource cancellation;
readonly ProgressCollector collector; readonly ProgressCollector collector;
readonly bool runningInOwnThread;
readonly Action<IProgressMonitor> asyncOperation;
readonly SynchronizationHelper synchronizationHelper = new SynchronizationHelper(); readonly SynchronizationHelper synchronizationHelper = new SynchronizationHelper();
AsynchronousWaitDialogForm dlg; AsynchronousWaitDialogForm dlg;
@ -106,8 +108,8 @@ namespace ICSharpCode.SharpDevelop.Gui
{ {
if (titleName == null) if (titleName == null)
throw new ArgumentNullException("titleName"); throw new ArgumentNullException("titleName");
AsynchronousWaitDialog h = new AsynchronousWaitDialog(titleName, defaultTaskName, allowCancel); AsynchronousWaitDialog h = new AsynchronousWaitDialog(titleName, defaultTaskName, allowCancel, null);
h.Start(); h.StartInThread();
return h; return h;
} }
@ -121,8 +123,8 @@ namespace ICSharpCode.SharpDevelop.Gui
{ {
if (titleName == null) if (titleName == null)
throw new ArgumentNullException("titleName"); throw new ArgumentNullException("titleName");
using (AsynchronousWaitDialog h = new AsynchronousWaitDialog(titleName, defaultTaskName, true)) { using (AsynchronousWaitDialog h = new AsynchronousWaitDialog(titleName, defaultTaskName, true, null)) {
h.Start(); h.StartInThread();
try { try {
action(h); action(h);
} catch (OperationCanceledException ex) { } catch (OperationCanceledException ex) {
@ -133,12 +135,41 @@ namespace ICSharpCode.SharpDevelop.Gui
} }
} }
private AsynchronousWaitDialog(string titleName, string defaultTaskName, bool allowCancel) /// <summary>
/// Shows a wait dialog that does not support cancelling and waits for an asynchronous operation to end.
/// </summary>
/// <param name="titleName">Title of the wait dialog</param>
/// <param name="asyncOperation">Asynchronous operation to be executed.</param>
public static void ShowWaitDialogForAsyncOperation(string titleName, Action<IProgressMonitor> asyncOperation)
{
ShowWaitDialogForAsyncOperation(titleName, null, asyncOperation);
}
/// <summary>
/// Shows a wait dialog that does not support cancelling and waits for an asynchronous operation to end.
/// </summary>
/// <param name="titleName">Title of the wait dialog</param>
/// <param name="allowCancel">Specifies whether a cancel button should be shown.</param>
/// <param name="defaultTaskName">The default description text, if no named task is active.</param>
/// <param name="asyncOperation">Asynchronous operation to be executed.</param>
/// <returns>AsynchronousWaitDialog object - you can use it to access the wait dialog's properties.
/// To close the wait dialog, call Dispose() on the AsynchronousWaitDialog object</returns>
public static void ShowWaitDialogForAsyncOperation(string titleName, string defaultTaskName, Action<IProgressMonitor> asyncOperation)
{
if (titleName == null)
throw new ArgumentNullException("titleName");
AsynchronousWaitDialog h = new AsynchronousWaitDialog(titleName, defaultTaskName, false, asyncOperation);
h.StartWithAsyncOperation();
}
private AsynchronousWaitDialog(string titleName, string defaultTaskName, bool allowCancel, Action<IProgressMonitor> asyncOperation)
{ {
this.titleName = StringParser.Parse(titleName); this.titleName = StringParser.Parse(titleName);
this.defaultTaskName = StringParser.Parse(defaultTaskName ?? "${res:Global.PleaseWait}"); this.defaultTaskName = StringParser.Parse(defaultTaskName ?? "${res:Global.PleaseWait}");
if (allowCancel) if (allowCancel)
this.cancellation = new CancellationTokenSource(); this.cancellation = new CancellationTokenSource();
this.asyncOperation = asyncOperation;
this.runningInOwnThread = (asyncOperation == null);
this.collector = new ProgressCollector(synchronizationHelper, allowCancel ? cancellation.Token : CancellationToken.None); this.collector = new ProgressCollector(synchronizationHelper, allowCancel ? cancellation.Token : CancellationToken.None);
} }
#endregion #endregion
@ -200,7 +231,7 @@ namespace ICSharpCode.SharpDevelop.Gui
/// <summary> /// <summary>
/// Start waiting thread /// Start waiting thread
/// </summary> /// </summary>
internal void Start() internal void StartInThread()
{ {
Thread newThread = new Thread(Run); Thread newThread = new Thread(Run);
newThread.Name = "AsynchronousWaitDialog thread"; newThread.Name = "AsynchronousWaitDialog thread";
@ -213,8 +244,43 @@ namespace ICSharpCode.SharpDevelop.Gui
void Run() void Run()
{ {
Thread.Sleep(ShowWaitDialogDelay); Thread.Sleep(ShowWaitDialogDelay);
if (collector.ProgressMonitorIsDisposed)
if (CreateDialogForm()) {
if (collector.ShowingDialog) {
Application.Run();
} else {
Application.Run(dlg);
}
}
}
#endregion
internal void StartWithAsyncOperation()
{
// Start asynchronous operation
if (asyncOperation == null)
return; return;
asyncOperation(collector.ProgressMonitor);
// Wait delay before showing dialog
var timer = new System.Windows.Forms.Timer();
timer.Interval = ShowWaitDialogDelay;
timer.Tick += (sender, e) =>
{
timer.Stop();
timer.Dispose();
// Create and show dialog
if (CreateDialogForm())
dlg.ShowDialog();
};
timer.Start();
}
bool CreateDialogForm()
{
if (collector.ProgressMonitorIsDisposed)
return false;
dlg = new AsynchronousWaitDialogForm(cancellation != null); dlg = new AsynchronousWaitDialogForm(cancellation != null);
dlg.Text = titleName; dlg.Text = titleName;
@ -230,18 +296,13 @@ namespace ICSharpCode.SharpDevelop.Gui
// check IsDisposed once again (we might have missed an event while we initialized the dialog): // check IsDisposed once again (we might have missed an event while we initialized the dialog):
if (collector.ProgressMonitorIsDisposed) { if (collector.ProgressMonitorIsDisposed) {
dlg.Dispose(); dlg.Dispose();
return; return false;
} }
progress_PropertyChanged(null, new System.ComponentModel.PropertyChangedEventArgs("TaskName")); progress_PropertyChanged(null, new System.ComponentModel.PropertyChangedEventArgs("TaskName"));
if (collector.ShowingDialog) { return true;
Application.Run();
} else {
Application.Run(dlg);
}
} }
#endregion
/// <summary> /// <summary>
/// Closes the wait dialog. /// Closes the wait dialog.
@ -249,7 +310,8 @@ namespace ICSharpCode.SharpDevelop.Gui
void progress_ProgressMonitorDisposed(object sender, EventArgs e) void progress_ProgressMonitorDisposed(object sender, EventArgs e)
{ {
dlg.Dispose(); dlg.Dispose();
Application.ExitThread(); if (runningInOwnThread)
Application.ExitThread();
} }
bool reshowTimerRunning = false; bool reshowTimerRunning = false;
@ -267,8 +329,13 @@ namespace ICSharpCode.SharpDevelop.Gui
timer.Tick += delegate { timer.Tick += delegate {
timer.Dispose(); timer.Dispose();
reshowTimerRunning = false; reshowTimerRunning = false;
if (!collector.ShowingDialog) if (!collector.ShowingDialog) {
dlg.Show(); if (runningInOwnThread)
dlg.Show();
else
if (!dlg.Visible)
dlg.ShowDialog();
}
}; };
timer.Start(); timer.Start();
} }

Loading…
Cancel
Save