Also trying to get running on .net Maui... #22
-
|
This is just for desktop applications though. I am completely oblivious/new to async programming patterns. I understand starting my own task and awaiting a result just fine, but awaiting a keyboard listener that should run for the life of the program (and therefore there is nothing to await) just confuses me more. After using RunAsync() on an initialized TaskPoolGlobalHook with an applicable event listener: void OnKeyPressed(object sender, KeyboardHookEventArgs e) { // assign string to some GUI field here } I get a peculiar exception message: Exception thrown: 'System.Runtime.InteropServices.COMException' in WinRT.Runtime.dll I have done a similar callback system in a basic console test app. I still had an error when I tried to call the destructor with one of the keys being listened itself (something about a referenced item not existing), but the key codes were able to be printed to the console just fine. You mentioned the singleton nature of hardware listeners in some of your docs and I'm wondering if maybe it's conflicting with the MAUI system input listeners themselves, but probably I just have no idea how to use an async function. Obviously I can't use Run() the regular way because that will just block my entire app from updating, unless I run and destroy it for every keypress. Are there any current examples of using the non-react version in an application like this? Background: used to being spoonfed key codes from useful Rust libraries and game engines and am a newb with c#. |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 18 replies
-
|
Hi! Thanks for using SharpHook! It looks like a problem with threads. You see, when you call Your code will then look something like this: void OnKeyPressed(object sender, KeyboardHookEventArgs e)
{
MainThread.BeginInvokeOnMainThread(() =>
{
// assign string to some GUI field here
});
}If you have any problems, feel free to continue the discussion. |
Beta Was this translation helpful? Give feedback.
-
|
Okay I've been running this successfully on windows .net 6 and 7 for a while, but the same code on mac (visual studio, etc) seems to make the hook and then the thread immediately drops. Have you seen anything similar from others? It's the same environment as before, using with .net maui apps. Using the non-react, happens with both task pool and simple, already made sure no parts of the program are accidentally disposing it while the program's running. |
Beta Was this translation helpful? Give feedback.
Hi! Thanks for using SharpHook!
It looks like a problem with threads. You see, when you call
RunAsyncon aTaskPoolGlobalHook(orSimpleGlobalHook) it will start a new thread and run the global hook there. In case ofSimpleGlobalHook, all event handlers will also run in that same new thread. In case ofTaskPoolGlobalHook, the event handlers will run on some thread in the thread pool forTaskobjects. Sounds difficult to someone who's new to this, but you don't have to dive into details. All you have to know is that if you want to update the UI in MAUI (or any other UI framework for .NET) you'll have to do that in the main thread. You can use theMainThreadclass for this.Your code will t…