-
Notifications
You must be signed in to change notification settings - Fork 30
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
CreateNativeAsync needs to be called twice #279
Comments
This is likely related to #274. Some thinking needed on how best to deal with this. |
After some testing related with #274, I've found that the call to I've discovered that the Chroma SDK will actually signal when this validation has completed by emitting the Colore provides an event handler for catching that event (
By "carefully", I mean that the current implementation of (@Sharparam please change this to just returning As an example, I'm trying something like this (untested): public sealed class ChromaWindow : NativeWindow // Or System.Windows.Forms.Form, or WPF
{
private IChroma _chroma;
private bool _chromaMsgHandlerSet;
private TaskCompletionSource<bool> _chromaAccessGranted = new TaskCompletionSource<bool>();
protected override void WndProc(ref Message m)
{
if (_chromaMsgHandlerSet && _chroma.HandleMessage(m.HWnd, m.Msg, m.WParam, m.LParam))
{
return;
}
base.WndProc(ref m);
}
private async Task Init()
{
var hWnd = this.Handle; // Should be available after the HandleCreated event.
_chroma = await ColoreProvider.CreateNativeAsync().ConfigureAwait(false);
_chroma.Register(hWnd);
_chromaMsgHandlerSet = true;
_chroma.DeviceAccess += Chroma_DeviceAccess;
var granted = await _chromaAccessGranted.Task.ConfigureAwait(false);
if (!granted)
{
throw new Exception("Chroma SDK has denied access.");
}
}
private void Chroma_DeviceAccess(object sender, Colore.Events.DeviceAccessEventArgs e)
{
_chromaAccessGranted.SetResult(e.Granted);
}
} Hope that it helps. |
@poveden Thanks for the input! Apologies for the late response, have been absent from here for a while. I can change the behaviour of the handler to just log a warning and return That would enable people to check it if they have access to the Win32 event system. For the cases where they don't have it available though I'm unsure how to best handle it. Got any ideas? diff --git a/src/Colore/Implementations/ChromaImplementation.cs b/src/Colore/Implementations/ChromaImplementation.cs
index f1ee6b01..5934ea69 100644
--- a/src/Colore/Implementations/ChromaImplementation.cs
+++ b/src/Colore/Implementations/ChromaImplementation.cs
@@ -373,13 +373,20 @@ namespace Colore.Implementations
public bool HandleMessage(IntPtr handle, int msgId, IntPtr wParam, IntPtr lParam)
{
if (!_registered)
- throw new InvalidOperationException("Register must be called before event handling can be performed.");+ {
+ Log.Warn($"{nameof(HandleMessage)} called without event handling being registered");
+
+ return false;
+ }
if (handle != _registeredHandle)
{
- throw new ArgumentException(
- "The specified window handle does not match the currently registered one.",
- nameof(handle));
+ Log.Warn(
+ $"Unexpected handle passed to {nameof(HandleMessage)}. Expected 0x{{0}} but was 0x{{1}}",
+ _registeredHandle.ToString("X"),
+ handle.ToString("X"));
+
+ return false;
}
if (msgId != Constants.WmChromaEvent) |
Based on feedback from @poveden in #279, the implementation of the Win32 event handling method (ChromaImplementation.HandleMessage) has been changed to return `false` if event handling has not been registered, or the handle not having the expected value, as opposed to throwing an exception. This is to avoid unexpected behaviour in event processing.
@Sharparam Sorry for my late response. 😛 I'd actually drop the log calls. You want event pumps to be as fast as possible. By the way Windows works, any delay here will delay all the messages that come after (this is the UI thread, after all). The logic is simple: If the message is not addressed to you, just return
I don't understand what you mean by this. Whoever links to Colore will do so either from a Win32 window (which needs an event pump) or from something else (which doesn't have an event pump). They should know in which context they are running. This I'd say that you leave it as is, unless you
Hope that it helps. |
As noted in #274 this behaviour is now documented in the getting started guide. Users need to create an artifical delay after creating the Chroma instance to allow the internal SDK to finish initializing, or wait for the |
I'm following the Getting Started tutorial to set up basic key modifications, but whenever I run it, I have to click the button twice to get any changes. The first time it is clicked, my Chroma Studio effects freeze, and then clicking it again will set the colors correctly.
As demonstrated in #253, it looks like in order to successfully connect with Chroma, you have to initialize twice (since they had CreateNativeAsync followed by InitializeAsync), which can be a problem. Below is my code for reference, just in case I simply mistyped something (irrelevant usings are omitted). Anyone have a potential solution to this other than running the methods twice?
The text was updated successfully, but these errors were encountered: