Skip to content

Unhandled exception occurs when the application attempts to access the clipboard #102

@Juris-S

Description

@Juris-S

Outline

An unhandled exception occurs when the application attempts to access the clipboard, likely due to a race condition or the clipboard being locked by another process.

Reproduction Steps:

  1. Set PasteIntoFile to launch automatically on Windows startup.
  2. Restart the computer or log out and log back in.
  3. The exception dialog appears as soon as the desktop environment loads.

Error message

System.Runtime.InteropServices.ExternalException (0x800401D0): Requested Clipboard operation did not succeed.
   at System.Windows.Forms.Clipboard.ThrowIfFailed(Int32 hr)
   at System.Windows.Forms.Clipboard.GetDataObject(Int32 retryTimes, Int32 retryDelay)
   at System.Windows.Forms.Clipboard.GetDataObject()
   at WK.Libraries.SharpClipboardNS.Views.ClipboardHandle.WndProc(Message& m)
   at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
   at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
   at System.Windows.Forms.NativeWindow.Callback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)

Environment:

  • Application: PasteIntoFile v5.6.0.0
  • OS: Windows 11 Pro 64-bit
  • Runtime: .NET Framework 4.8
  • Library involved: SharpClipboard v3.5.2.0

Loaded Assemblies (Summary):

  • mscorlib (4.0.0.0)
  • System.Windows.Forms (4.0.0.0)
  • SharpClipboard (3.5.2.0)
  • CommandLine (2.9.1.0)

The stack trace shows the crash happens at WK.Libraries.SharpClipboardNS.Views.ClipboardHandle.WndProc. This is a classic race condition between apps probably. Try wrap the clipboard access in a try-catch block with a small "retry" loop (e.g., try 3 times with a 100ms delay between attempts). Since SharpClipboard uses a "Clipboard Viewer" or "Clipboard Monitor" hook. When it receives a message that the clipboard has changed, it immediately tries to read it, often before the application that put the data there has finished closing the clipboard handle.

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions