17 changed files with 211 additions and 12 deletions
@ -0,0 +1,63 @@ |
|||||||
|
// Copyright (c) AlphaSierraPapa for the SharpDevelop Team (for details please see \doc\copyright.txt)
|
||||||
|
// This code is distributed under the GNU LGPL (for details please see \doc\license.txt)
|
||||||
|
|
||||||
|
using System; |
||||||
|
using System.Threading; |
||||||
|
using System.Threading.Tasks; |
||||||
|
|
||||||
|
namespace ICSharpCode.SharpDevelop |
||||||
|
{ |
||||||
|
/// <summary>
|
||||||
|
/// Service that manages the IDE shutdown, and any questions.
|
||||||
|
/// </summary>
|
||||||
|
public interface IShutdownService |
||||||
|
{ |
||||||
|
/// <summary>
|
||||||
|
/// Attemps to close the IDE.
|
||||||
|
/// </summary>
|
||||||
|
/// <remarks>
|
||||||
|
/// This method will
|
||||||
|
/// - Check if <see cref="PreventShutdown"/> was called and abort the shutdown if it was.
|
||||||
|
/// - Prompt the user to save the open files. The user has the option to cancel the shutdown at that point.
|
||||||
|
/// - Closes the solution.
|
||||||
|
/// - Signals the <see cref="ShutdownToken"/>.
|
||||||
|
/// - Disposes pads
|
||||||
|
/// - Wait for background tasks (<see cref="AddBackgroundTask"/>) to finish.
|
||||||
|
/// - Disposes services
|
||||||
|
/// - Saves the PropertyService
|
||||||
|
///
|
||||||
|
/// This method must be called on the main thread.
|
||||||
|
/// </remarks>
|
||||||
|
bool Shutdown(); |
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Prevents shutdown with the following reason.
|
||||||
|
/// Dispose the returned value to allow shutdown again.
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="reason">The reason. This parameter will be passed through the StringParser when the reason is displayed to the user.</param>
|
||||||
|
/// <exception cref="InvalidOperationException">Shutdown is already in progress</exception>
|
||||||
|
/// <remarks>This method is thread-safe.</remarks>
|
||||||
|
IDisposable PreventShutdown(string reason); |
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Gets the current reason that prevents shutdown.
|
||||||
|
/// If there isn't any reason, returns null.
|
||||||
|
/// If there are multiple reasons, this returns one of them.
|
||||||
|
/// </summary>
|
||||||
|
/// <remarks>This method is thread-safe.</remarks>
|
||||||
|
string CurrentReasonPreventingShutdown { get; } |
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Gets a cancellation token that gets signalled when SharpDevelop is shutting down.
|
||||||
|
/// </summary>
|
||||||
|
CancellationToken ShutdownToken { get; } |
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Adds a background task on which SharpDevelop should wait on shutdown.
|
||||||
|
///
|
||||||
|
/// Use this method for tasks that asynchronously write state to disk and should not be
|
||||||
|
/// interrupted by SharpDevelop closing down.
|
||||||
|
/// </summary>
|
||||||
|
void AddBackgroundTask(Task task); |
||||||
|
} |
||||||
|
} |
@ -0,0 +1,20 @@ |
|||||||
|
// Copyright (c) AlphaSierraPapa for the SharpDevelop Team (for details please see \doc\copyright.txt)
|
||||||
|
// This code is distributed under the GNU LGPL (for details please see \doc\license.txt)
|
||||||
|
|
||||||
|
using System; |
||||||
|
using System.Collections.Generic; |
||||||
|
using System.Threading.Tasks; |
||||||
|
|
||||||
|
namespace ICSharpCode.SharpDevelop.Util |
||||||
|
{ |
||||||
|
/// <summary>
|
||||||
|
/// Scheduler for IO-intensive tasks.
|
||||||
|
/// </summary>
|
||||||
|
public class IOTaskScheduler |
||||||
|
{ |
||||||
|
// TODO: use a limited-concurrency scheduler instead
|
||||||
|
public static TaskFactory Factory { |
||||||
|
get { return Task.Factory; } |
||||||
|
} |
||||||
|
} |
||||||
|
} |
@ -0,0 +1,84 @@ |
|||||||
|
// Copyright (c) AlphaSierraPapa for the SharpDevelop Team (for details please see \doc\copyright.txt)
|
||||||
|
// This code is distributed under the GNU LGPL (for details please see \doc\license.txt)
|
||||||
|
|
||||||
|
using System; |
||||||
|
using System.Collections.Generic; |
||||||
|
using System.Linq; |
||||||
|
using System.Threading; |
||||||
|
using System.Threading.Tasks; |
||||||
|
|
||||||
|
using ICSharpCode.NRefactory.Utils; |
||||||
|
|
||||||
|
namespace ICSharpCode.SharpDevelop.Workbench |
||||||
|
{ |
||||||
|
sealed class ShutdownService : IShutdownService |
||||||
|
{ |
||||||
|
CancellationTokenSource cts = new CancellationTokenSource(); |
||||||
|
|
||||||
|
public CancellationToken ShutdownToken { |
||||||
|
get { return cts.Token; } |
||||||
|
} |
||||||
|
|
||||||
|
internal void SignalShutdownToken() |
||||||
|
{ |
||||||
|
cts.Cancel(); |
||||||
|
} |
||||||
|
|
||||||
|
public bool Shutdown() |
||||||
|
{ |
||||||
|
SD.Workbench.MainWindow.Close(); |
||||||
|
return SD.Workbench.WorkbenchLayout == null; |
||||||
|
} |
||||||
|
|
||||||
|
#region PreventShutdown
|
||||||
|
List<string> reasonsPreventingShutdown = new List<string>(); |
||||||
|
|
||||||
|
public IDisposable PreventShutdown(string reason) |
||||||
|
{ |
||||||
|
lock (reasonsPreventingShutdown) { |
||||||
|
reasonsPreventingShutdown.Add(reason); |
||||||
|
} |
||||||
|
return new CallbackOnDispose( |
||||||
|
delegate { |
||||||
|
lock (reasonsPreventingShutdown) { |
||||||
|
reasonsPreventingShutdown.Remove(reason); |
||||||
|
} |
||||||
|
}); |
||||||
|
} |
||||||
|
|
||||||
|
public string CurrentReasonPreventingShutdown { |
||||||
|
get { |
||||||
|
lock (reasonsPreventingShutdown) { |
||||||
|
return reasonsPreventingShutdown.FirstOrDefault(); |
||||||
|
} |
||||||
|
} |
||||||
|
} |
||||||
|
#endregion
|
||||||
|
|
||||||
|
#region Background Tasks
|
||||||
|
int outstandingBackgroundTasks; |
||||||
|
ManualResetEventSlim backgroundTaskEvent = new ManualResetEventSlim(true); |
||||||
|
|
||||||
|
public void AddBackgroundTask(Task task) |
||||||
|
{ |
||||||
|
backgroundTaskEvent.Reset(); |
||||||
|
Interlocked.Increment(ref outstandingBackgroundTasks); |
||||||
|
task.ContinueWith( |
||||||
|
delegate { |
||||||
|
if (Interlocked.Decrement(ref outstandingBackgroundTasks) == 0) { |
||||||
|
backgroundTaskEvent.Set(); |
||||||
|
} |
||||||
|
}); |
||||||
|
} |
||||||
|
|
||||||
|
internal void WaitForBackgroundTasks() |
||||||
|
{ |
||||||
|
if (!backgroundTaskEvent.IsSet) { |
||||||
|
SD.LoggingService.Info("Waiting for background tasks to finish..."); |
||||||
|
backgroundTaskEvent.Wait(); |
||||||
|
SD.LoggingService.Info("Background tasks have finished."); |
||||||
|
} |
||||||
|
} |
||||||
|
#endregion
|
||||||
|
} |
||||||
|
} |
Loading…
Reference in new issue