Willy-Kimura / SharpClipboard

A library for anonymously monitoring clipboard entries.
188 stars 36 forks source link

Can this be used in Console app? #37

Open airadam opened 9 months ago

airadam commented 9 months ago

I've been trying to use this library in a .NET 7.0 Windows console app, but even after registering a simple delegate against ClipboardChanged, I can't get it to fire at all. As the initial documentation describes using it in VS with the Designer, I thought I'd better check - can I expect it to work at all in my use case, or will it only work in a Forms app or similar?

using System;
using WK.Libraries.SharpClipboardNS;

namespace ExampleApp
{
    internal class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Starting.");
            var clipboard = new SharpClipboard();
            clipboard.ClipboardChanged += ClipboardChanged;

            while(true)
            {
            }
        }

        private static void ClipboardChanged(Object sender, SharpClipboard.ClipboardChangedEventArgs e)
        {
            Console.WriteLine("Change detected.");
            Console.WriteLine(e.Content.ToString());
        }
    }
}

Thanks!

Salmakis commented 3 months ago

it works for me, but needed to make it [STAThread] also i got the target framework to the one with -windows in my csproj file.

i.e. <TargetFramework>net7.0</TargetFramework> to <TargetFramework>net7.0-windows</TargetFramework>

(works with 8.0 also so far)