This repository has been archived by the owner on May 18, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
21 changed files
with
1,296 additions
and
245 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Text; | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
|
||
namespace PacketViewerLogViewer.ClipboardHelper | ||
{ | ||
// Source: https://stackoverflow.com/questions/899350/how-do-i-copy-the-contents-of-a-string-to-the-clipboard-in-c | ||
/// <summary> | ||
/// Usage: new SetClipboardHelper( DataFormats.Text, "See, I'm on the clipboard" ).Go(); | ||
/// </summary> | ||
class SetClipboardHelper : StaHelper | ||
{ | ||
readonly string _format; | ||
readonly object _data; | ||
|
||
public SetClipboardHelper(string format, object data) | ||
{ | ||
_format = format; | ||
_data = data; | ||
} | ||
|
||
protected override void Work() | ||
{ | ||
var obj = new System.Windows.Forms.DataObject( | ||
_format, | ||
_data | ||
); | ||
|
||
System.Windows.Forms.Clipboard.SetDataObject(obj, true); | ||
} | ||
} | ||
|
||
abstract class StaHelper | ||
{ | ||
readonly ManualResetEvent _complete = new ManualResetEvent(false); | ||
|
||
public void Go() | ||
{ | ||
var thread = new Thread(new ThreadStart(DoWork)) | ||
{ | ||
IsBackground = true, | ||
}; | ||
thread.SetApartmentState(ApartmentState.STA); | ||
thread.Start(); | ||
} | ||
|
||
// Thread entry method | ||
private void DoWork() | ||
{ | ||
try | ||
{ | ||
_complete.Reset(); | ||
Work(); | ||
} | ||
catch (Exception ex) | ||
{ | ||
if (DontRetryWorkOnFailed) | ||
throw; | ||
else | ||
{ | ||
try | ||
{ | ||
Thread.Sleep(1000); | ||
Work(); | ||
} | ||
catch | ||
{ | ||
// ex from first exception | ||
System.Windows.Forms.MessageBox.Show(ex.Message,"Copy to Clipboard",System.Windows.Forms.MessageBoxButtons.OK,System.Windows.Forms.MessageBoxIcon.Error); | ||
// LogAndShowMessage(ex); | ||
} | ||
} | ||
} | ||
finally | ||
{ | ||
_complete.Set(); | ||
} | ||
} | ||
|
||
public bool DontRetryWorkOnFailed { get; set; } | ||
|
||
// Implemented in base class to do actual work. | ||
protected abstract void Work(); | ||
} | ||
|
||
|
||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Oops, something went wrong.