mirror of
https://github.com/qmlnet/qmlnet.git
synced 2026-05-15 14:15:54 -06:00
[GH-ISSUE #165] Trigger signal on main thread from secondary thread #105
Labels
No labels
bug
enhancement
help wanted
help wanted
pull-request
question
todo
No milestone
No project
No assignees
1 participant
Notifications
Due date
No due date set.
Dependencies
No dependencies set.
Reference: github-starred/qmlnet#105
Loading…
Add table
Add a link
Reference in a new issue
No description provided.
Delete branch "%!s()"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Originally created by @WingedToaster on GitHub (Sep 16, 2019).
Original GitHub issue: https://github.com/qmlnet/qmlnet/issues/165
I'm trying to integrate a barcode reader into an app, the serial port is waiting for data come in on a secondary thread but if I try to show the result from that thread, I get a message saying the signal must be called on the main thread, but I'm not quite sure how to do that. In Qt I'm able to just link up the two threads and then when a barcode is read I can
emitit to the main thread. How can I do that in Qml.Net?Any help would be appreciated.
@pauldotknopf commented on GitHub (Sep 17, 2019):
The QCoreApplication has a method to invoke actions in the GUI. I'm mobile right now, but you should be able to find what you are looking for there.
@WingedToaster commented on GitHub (Sep 17, 2019):
I tried a few things and it crashes every time...
I don't have my scanner handy tonight but to trigger it, I just created a thread, slept for 4 seconds and then tried to call it...
ScannerController.cs
Then in my qml, I do have a call to connect to the signal
scanner.resultSignal.connect(function() {...})but this is the error I get now...Seems like I'm missing something where the signal needs to be registered or something, but I don't know what...
@pauldotknopf commented on GitHub (Sep 17, 2019):
You'll want to use the
DispatchandDispatchAsyncmethods here.The
AwaitTaskdoesn't dispatch anything, it simply blocks the caller until the given task is completed.I don't blame you though, there isn't a lot of documentation.
@WingedToaster commented on GitHub (Sep 17, 2019):
OK, I was afraid of that since it's the instance method. I assume there's no quick way of getting the instance of the running application and I should try to follow the example of the
AppModelwith theUiDelegatecode in the PhotoFrame example...@pauldotknopf commented on GitHub (Sep 17, 2019):
In my solutions, I create an
IDispatcherthat takes the instance ofQCoreApplicationat the root, and registers it with a dependency container. Then, when yourAppModelget's resolved, you can take in anIDispatcherto dispatch your actions to the GUI thread.Another option is to store the
SynchronizationContextinto a your own variable, which can be used to dispatch the action.@WingedToaster commented on GitHub (Sep 18, 2019):
Ok, after spending my entire day on this, not really knowing what I was doing (but thinking I did)...and failing...miserably...I mean really miserably...I finally figured this out...I think...
ScanController.cs
Program.cs
ScanPage.qml