Browse Source

Update Clipboard.ContainsText using a separate thread.

Fix/workaround for SD2-1466 - SharpDevelop freezes when debugged application sets clipboard text.

git-svn-id: svn://svn.sharpdevelop.net/sharpdevelop/branches/3.0@3605 1ccf3a8d-04fe-1044-b7c0-cef0b8235c61
shortcuts
Daniel Grunwald 18 years ago
parent
commit
2bffb7e184
  1. 27
      src/Libraries/ICSharpCode.TextEditor/Project/Src/Gui/TextAreaClipboardHandler.cs
  2. 1
      src/Main/Base/Project/ICSharpCode.SharpDevelop.csproj
  3. 3
      src/Main/Base/Project/Src/Commands/EditCommands.cs
  4. 67
      src/Main/Base/Project/Src/TextEditor/ClipboardHandling.cs
  5. 1
      src/Main/Base/Project/Src/TextEditor/Gui/Editor/TextEditorDisplayBinding.cs

27
src/Libraries/ICSharpCode.TextEditor/Project/Src/Gui/TextAreaClipboardHandler.cs

@ -32,14 +32,33 @@ namespace ICSharpCode.TextEditor @@ -32,14 +32,33 @@ namespace ICSharpCode.TextEditor
}
}
public delegate bool ClipboardContainsTextDelegate();
/// <summary>
/// Is called when CachedClipboardContainsText should be updated.
/// If this property is null (the default value), the text editor uses
/// System.Windows.Forms.Clipboard.ContainsText.
/// </summary>
/// <remarks>
/// This property is useful if you want to prevent the default Clipboard.ContainsText
/// behaviour that waits for the clipboard to be available - the clipboard might
/// never become available if it is owned by a process that is paused by the debugger.
/// </remarks>
public static ClipboardContainsTextDelegate GetClipboardContainsText;
public bool EnablePaste {
get {
if (!textArea.EnableCutOrPaste)
return false;
try {
return Clipboard.ContainsText();
} catch (ExternalException) {
return false;
ClipboardContainsTextDelegate d = GetClipboardContainsText;
if (d != null) {
return d();
} else {
try {
return Clipboard.ContainsText();
} catch (ExternalException) {
return false;
}
}
}
}

1
src/Main/Base/Project/ICSharpCode.SharpDevelop.csproj

@ -473,6 +473,7 @@ @@ -473,6 +473,7 @@
<Compile Include="Src\TextEditor\Bookmarks\Bookmark.cs" />
<Compile Include="Src\TextEditor\Bookmarks\BookmarkEventHandler.cs" />
<Compile Include="Src\TextEditor\Bookmarks\BookmarkManager.cs" />
<Compile Include="Src\TextEditor\ClipboardHandling.cs" />
<Compile Include="Src\TextEditor\Codons\AddInTreeSyntaxModeProvider.cs" />
<Compile Include="Src\TextEditor\Codons\EditActionDoozer.cs" />
<Compile Include="Src\TextEditor\Codons\SyntaxModeDoozer.cs" />

3
src/Main/Base/Project/Src/Commands/EditCommands.cs

@ -5,6 +5,7 @@ @@ -5,6 +5,7 @@
// <version>$Revision$</version>
// </file>
using ICSharpCode.SharpDevelop.DefaultEditor;
using System;
using System.Windows.Forms;
using ICSharpCode.Core;
@ -122,7 +123,7 @@ namespace ICSharpCode.SharpDevelop.Commands @@ -122,7 +123,7 @@ namespace ICSharpCode.SharpDevelop.Commands
get { return comboBox.SelectionLength > 0; }
}
public bool EnablePaste {
get { return ClipboardWrapper.ContainsText; }
get { return ClipboardHandling.GetClipboardContainsText(); }
}
public bool EnableDelete {
get { return true; }

67
src/Main/Base/Project/Src/TextEditor/ClipboardHandling.cs

@ -0,0 +1,67 @@ @@ -0,0 +1,67 @@
// <file>
// <copyright see="prj:///doc/copyright.txt"/>
// <license see="prj:///doc/license.txt"/>
// <author name="Daniel Grunwald"/>
// <version>$Revision$</version>
// </file>
using ICSharpCode.Core.WinForms;
using System;
using System.Threading;
using ICSharpCode.SharpDevelop.Gui;
namespace ICSharpCode.SharpDevelop.DefaultEditor
{
/// <summary>
/// This class fixes SD2-1466: SharpDevelop freezes when debugged application sets clipboard text.
/// The problem is that Clipboard.ContainsText may wait for the application owning the clipboard,
/// which in turn may currently wait for SharpDevelop (through the debugger)
/// </summary>
static class ClipboardHandling
{
public static void Initialize()
{
ICSharpCode.TextEditor.TextAreaClipboardHandler.GetClipboardContainsText = GetClipboardContainsText;
WorkbenchSingleton.MainForm.Activated += WorkbenchSingleton_MainForm_Activated;
}
static void WorkbenchSingleton_MainForm_Activated(object sender, EventArgs e)
{
UpdateClipboardContainsText();
}
static bool clipboardContainsText;
public static bool GetClipboardContainsText()
{
WorkbenchSingleton.DebugAssertMainThread();
if (WorkbenchSingleton.Workbench.IsActiveWindow) {
UpdateClipboardContainsText();
}
return clipboardContainsText;
}
static Thread updateThread;
static void UpdateClipboardContainsText()
{
if (updateThread != null)
return;
Thread t = new Thread(new ThreadStart(DoUpdate));
t.SetApartmentState(ApartmentState.STA);
t.IsBackground = true;
updateThread = t;
t.Start();
t.Join(50); // wait a few ms in case the clipboard can be accessed without problems
}
static void DoUpdate()
{
try {
clipboardContainsText = ClipboardWrapper.ContainsText;
} finally {
updateThread = null;
}
}
}
}

1
src/Main/Base/Project/Src/TextEditor/Gui/Editor/TextEditorDisplayBinding.cs

@ -33,6 +33,7 @@ namespace ICSharpCode.SharpDevelop.DefaultEditor.Gui.Editor @@ -33,6 +33,7 @@ namespace ICSharpCode.SharpDevelop.DefaultEditor.Gui.Editor
HighlightingManager.Manager.AddSyntaxModeFileProvider(new ICSharpCode.SharpDevelop.DefaultEditor.Codons.AddInTreeSyntaxModeProvider());
HighlightingManager.Manager.AddSyntaxModeFileProvider(new FileSyntaxModeProvider(Path.Combine(PropertyService.DataDirectory, "modes")));
HighlightingManager.Manager.AddSyntaxModeFileProvider(new FileSyntaxModeProvider(modeDir));
ClipboardHandling.Initialize();
}
/// <summary>

Loading…
Cancel
Save