C
C#11mo ago
Pogo

EventHandler not triggering.

in Program.cs
namespace ClipboardHistoryCLI
{
internal class Program
{
static void Main(string[] args)
{
ClipboardHandler handler = new ClipboardHandler();

CommandParser parser = new CommandParser(args);
string[] list;

list = parser.ListComponents();
Console.WriteLine($"{list} TEST");

do
{
Console.WriteLine("HI");
Thread.Sleep(1000);
}
while (true);
}
}
}
namespace ClipboardHistoryCLI
{
internal class Program
{
static void Main(string[] args)
{
ClipboardHandler handler = new ClipboardHandler();

CommandParser parser = new CommandParser(args);
string[] list;

list = parser.ListComponents();
Console.WriteLine($"{list} TEST");

do
{
Console.WriteLine("HI");
Thread.Sleep(1000);
}
while (true);
}
}
}
in ClipboardHandler.cs:
namespace ClipboardHistoryCLI._Clipboard
{
internal class ClipboardHandler
{
private Dictionary<string, DataPackageView> _clipboardHistory = [];

public ClipboardHandler()
{
Clipboard.ContentChanged += new EventHandler<object>(this.ClipboardChanged);
}

private void ClipboardChanged(object? sender, object e)
{
Console.WriteLine("TEST");
Console.WriteLine($"{sender}, {e}");
Console.WriteLine($"{sender.GetType()}, {e.GetType()}");
}
namespace ClipboardHistoryCLI._Clipboard
{
internal class ClipboardHandler
{
private Dictionary<string, DataPackageView> _clipboardHistory = [];

public ClipboardHandler()
{
Clipboard.ContentChanged += new EventHandler<object>(this.ClipboardChanged);
}

private void ClipboardChanged(object? sender, object e)
{
Console.WriteLine("TEST");
Console.WriteLine($"{sender}, {e}");
Console.WriteLine($"{sender.GetType()}, {e.GetType()}");
}
Wrote a very basic setup to learn to work with an EventHandler. The event however does not trigger, I had wrapped Main into an async method, I made ClipboardChanged async, i added an async delay on the main keep-alive loop, nothing worked for me. Any idea why the EventHandler doesnt trigger? ClipboardChanged is an official UWP EventHandler: https://learn.microsoft.com/en-us/uwp/api/windows.applicationmodel.datatransfer.clipboard.contentchanged?view=winrt-22621
Clipboard.ContentChanged Event (Windows.ApplicationModel.DataTransf...
Occurs when the data stored in the Clipboard changes.
15 Replies
Jimmacle
Jimmacle11mo ago
FWIW, the explicit EventHandler is outdated, you can just Clipboard.ContentChanged += ClipboardChanged; have you put a breakpoint in the handler to see if it's really triggering or not?
Pogo
PogoOP11mo ago
weird that they didnt update their own docs? how do i add a breakpoint in VS?
Jimmacle
Jimmacle11mo ago
click in the left hand gutter, that's how it works in rider anyway
Buddy
Buddy11mo ago
Isn't it the clipboard history API?
Pogo
PogoOP11mo ago
yep found it
Clipboard.ContentChanged += new EventHandler<object>(this.TrackClipboardChanges_EventHandler);
Clipboard.ContentChanged += new EventHandler<object>(this.TrackClipboardChanges_EventHandler);
Buddy
Buddy11mo ago
Press Windows key + V
Pogo
PogoOP11mo ago
there's a clipboard HISTORY api and a normal one clipboard history changed and regular clipboard content changed (i want to track when new content entered the current clipboard) though I dont understand what im doing wrong, is my eventhandler incorrect? is my keep alive loop incorrect?
Buddy
Buddy11mo ago
Never used said API, but this seems interesting
You can only access the clipboard when the calling application is in focus on the UI thread (for example, when its CoreWindow is active). Use the CoreWindow.Activated event to track the state of the calling app.
https://learn.microsoft.com/en-us/uwp/api/windows.applicationmodel.datatransfer.clipboard?view=winrt-22621
Pogo
PogoOP11mo ago
also no, clipboardchanged did not even trigger
No description
Buddy
Buddy11mo ago
Which may be an API exclusive to UWP applications
Pogo
PogoOP11mo ago
well, i use the UWP framework but
Buddy
Buddy11mo ago
As console applications does not have a UI Thread
Pogo
PogoOP11mo ago
they explicitly want me to instance a CoreWindow..? bro why do they do this they do this with every application ever how about if i spawn an invisible CoreWindow no actual window anywhere it just 'exists'
Jimmacle
Jimmacle11mo ago
sounds like a security thing if they want the window to be in focus
Pogo
PogoOP11mo ago
yet their own clipboard history app is allowed to grrr
Want results from more Discord servers?
Add your server