Skip to content

Conversation

@CyrusNajmabadi
Copy link
Member

Winforms exposes this (and we make use of it) already for setting clipboard data. This just exposes the same for reading clipboard data.

Noticed in local development where cutting code could take up to 1 second due to other apps on my system locking the clipboard.

do
{
hr = OleGetClipboard(ref dataObject);
if (!ErrorHandler.Succeeded(hr))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why Succeeded check here but loop terminates on S_OK check?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Doesn't ErrorHandler.Succeeded check that it's >= 0, not == 0? The concern I have is if OleGetClipboard returns S_FALSE, then this code could loop forever.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch. That was unintended. HAve switched this to a straight 0 check. thanks!

if (!ErrorHandler.Succeeded(hr))
{
if (retry == 0)
return null;
Copy link
Member

@JoeRobich JoeRobich Nov 19, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In Clipboard this threw. How will the behavior change affect the user experience?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We caught the throw and returned null. So this has the same effect. It means we don't support rich copy/paste semantics. But that's better than timing out for 1 second :D

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, I should have looked further down.

if (!ErrorHandler.Succeeded(hr))
{
if (retry == 0)
return null;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, I should have looked further down.

Copy link
Contributor

@ToddGrun ToddGrun left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

:shipit:

Copy link
Member

@jasonmalinowski jasonmalinowski left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

:shipit:

/// <summary>
/// Copied from https://github.com/dotnet/winforms/blob/0f76e65878b1a0958175f17c4360b8198f8b36ba/src/System.Windows.Forms/src/System/Windows/Forms/Clipboard.cs#L139
/// </summary>
private static IDataObject? GetDataObject(int retryTimes, int retryDelay)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

private static string GetFormat(string key)
=> $"{RoslynFormat}-{key}";

public bool TrySetClipboardData(string key, string data)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This PR is fine, but if I recall at one point we also had clipboard issues with the stack trace explorer that might have been similar. I wonder what this code would look like if we moved the clipboard manipulation to a different thread (or move it to the OOP process) entirely. I'm guessing there's some COM/STA thread semantics so it's not a trivial change, but just something to think about if we still see issues.

@CyrusNajmabadi CyrusNajmabadi merged commit 19911c9 into dotnet:main Nov 19, 2025
25 of 26 checks passed
@CyrusNajmabadi CyrusNajmabadi deleted the clipboardTimeout branch November 19, 2025 20:28
@dotnet-policy-service dotnet-policy-service bot added this to the Next milestone Nov 19, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants