After a nice 4 days trek on the Marlborough Sounds (pictures and short report to come soon), I'm back at work.

We are close to the release of the next version of our main product, so we are setting up some VirtualPCs to test it. And we found a problem on a portion of application that never caused problems: a simple Clipboard.SetDataObject.

Looking at the documentation on MSDN I found that NET 2.0 added a new overload to the SetDataObject method:

public static void SetDataObject (
    Object data,
    bool copy,
    int retryTimes,
    int retryDelay
)

Why should a method have these retry options?

Adding data to the Clipboard can occasionally fail if the Clipboard is busy with another thread or application. This method is useful to work around this issue in environments with heavy Clipboard use.

But these retry options doesn't seem to be enough on Virtual PC, so I wrote a small helper method that seems to work around the problem most of the times.

private static void CopyDataToClipboardSafe(object data, bool copy)
{
  try
  {
      Clipboard.SetDataObject(data, copy, 10, 50);
  }
  catch (ExternalException)
  {
      Application.DoEvents();
      try
      {
          Clipboard.SetDataObject(data, copy, 10, 50);
      }
      catch (ExternalException)
      {
          throw;
      }
  }
}

Not 100% effective, but since the problem happens mainly on Virtual PCs, and probably 95% of the users of our product never heard about it, I think I can safely announce that I fixed that bug smile_wink.
And probably better to use VMware as virtualization software for our test environment.

kick it on DotNetKicks.com