You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
46 lines
1.3 KiB
46 lines
1.3 KiB
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Text;
|
|
using System.Threading.Tasks;
|
|
using System.Windows;
|
|
|
|
namespace AIK.Views.Utils
|
|
{
|
|
public static class SafeClipboard
|
|
{
|
|
public static async Task<bool> SetTextAsync(string text, int maxRetries = 3, int delayMs = 10)
|
|
{
|
|
if (string.IsNullOrEmpty(text))
|
|
return false;
|
|
|
|
if (Application.Current?.Dispatcher.CheckAccess() == false)
|
|
{
|
|
return await SetTextInternal(text, maxRetries, delayMs);
|
|
}
|
|
else
|
|
{
|
|
return await SetTextInternal(text, maxRetries, delayMs);
|
|
}
|
|
}
|
|
|
|
private static async Task<bool> SetTextInternal(string text, int maxRetries, int delayMs)
|
|
{
|
|
for (int i = 0; i < maxRetries; i++)
|
|
{
|
|
try
|
|
{
|
|
Clipboard.SetText(text);
|
|
return true;
|
|
}
|
|
catch (System.Runtime.InteropServices.COMException ex) when (ex.ErrorCode == -2147221040)
|
|
{
|
|
if (i == maxRetries - 1)
|
|
return false;
|
|
await Task.Delay(delayMs);
|
|
}
|
|
}
|
|
return false;
|
|
}
|
|
}
|
|
}
|
|
|