Skip to content

Commit

Permalink
Catch clipboard exceptions
Browse files Browse the repository at this point in the history
  • Loading branch information
Tkael committed May 5, 2024
1 parent a68231c commit 31ada4c
Showing 1 changed file with 17 additions and 7 deletions.
24 changes: 17 additions & 7 deletions SpeechResponder/CustomFunctions/SetClipboard.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using System;
using System.Threading;
using System.Windows;
using Utilities;

namespace EddiSpeechResponder.CustomFunctions
{
Expand All @@ -14,22 +15,31 @@ public class SetClipboard : ICustomFunction
public FunctionCategory Category => FunctionCategory.Utility;
public string description => Properties.CustomFunctions_Untranslated.SetClipboard;
public Type ReturnType => typeof( string );
public NativeFunction function => new NativeFunction((values) =>
public NativeFunction function => new NativeFunction( ( values ) =>
{
var text = values[0].AsString;
if (!string.IsNullOrEmpty(text))
if ( !string.IsNullOrEmpty( text ) )
{
var clipboardThread = new Thread(() => { Clipboard.SetData(DataFormats.Text, text); });
clipboardThread.SetApartmentState(ApartmentState.STA);
var clipboardThread = new Thread( () =>
{
try
{
Clipboard.SetData(DataFormats.Text, text);
}
catch ( Exception e )
{
Logging.Error( "Failed to set clipboard", e );
}
});
clipboardThread.SetApartmentState( ApartmentState.STA );
clipboardThread.Start();
clipboardThread.Join();
return "";
}
else
{
return
"The SetClipboard function is used improperly. Please review the documentation for correct usage.";
return "The SetClipboard function is used improperly. Please review the documentation for correct usage.";
}
}, 1);
}, 1 );
}
}

0 comments on commit 31ada4c

Please sign in to comment.