Skip to content
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

How to raise Notifications from async processes? #221

Closed
h0lg opened this issue Feb 11, 2024 · 4 comments · Fixed by #225
Closed

How to raise Notifications from async processes? #221

h0lg opened this issue Feb 11, 2024 · 4 comments · Fixed by #225

Comments

@h0lg
Copy link
Contributor

h0lg commented Feb 11, 2024

Hey fabulous, I've tried out the new Notifications API in 2.4.0-pre1 and it seems to work fine for synchronous messages, but when trying to notify from an async process, the Dispatcher throws one of these my way:

System.InvalidOperationException
  HResult=0x80131509
  Message=Call from invalid thread
  Source=Avalonia.Base
  StackTrace:
   at Avalonia.Threading.Dispatcher.<VerifyAccess>g__ThrowVerifyAccess|16_0() in Avalonia.Threading\Dispatcher.cs:line 182
   at Avalonia.AvaloniaObject..ctor() in Avalonia\AvaloniaObject.cs:line 96
   at Avalonia.StyledElement..ctor() in Avalonia\StyledElement.cs:line 218
   at Avalonia.Visual..ctor()
   at Avalonia.Layout.Layoutable..ctor()
   at Avalonia.Interactivity.Interactive..ctor()
   at Avalonia.Input.InputElement..ctor() in Avalonia.Input\InputElement.cs:line 507
   at Avalonia.Controls.Control..ctor()
   at Avalonia.Controls.Primitives.TemplatedControl..ctor()
   at Avalonia.Controls.ContentControl..ctor()
   at Avalonia.Controls.Notifications.NotificationCard..ctor()
   at Avalonia.Controls.Notifications.WindowNotificationManager.<Show>d__13.MoveNext()
   at System.Threading.Tasks.Task.<>c.<ThrowAsync>b__128_1(Object state)
   at System.Threading.ThreadPoolWorkQueue.Dispatch()
   at System.Threading.PortableThreadPool.WorkerThread.WorkerThreadStart()

A PR with reproducable examples on the NotificationPage in the Gallery follows.

It also includes some questions regarding the recent changes towards the concept of CmdMsg, which I don't understand, seemingly replacing Cmd<'msg>, which I think I understand by now.

@edgarfgp
Copy link
Member

edgarfgp commented Feb 11, 2024

Thanks for raising this issue. This looks like it needs to be run in the UI Thread, in Avalonia you can use

Dispatcher.UIThread.Post(fun _ -> dispatch(NotifyInfo "started"))

Will look like into your PR and add a sample for how to achieve this

@edgarfgp
Copy link
Member

Fixed #222

@h0lg
Copy link
Contributor Author

h0lg commented Feb 19, 2024

@edgarfgp Thank you for looking into this :)

I see you created an example for a custom notification position using a model property. I hoped there'd be a less intrusive solution - but I guess that works.

I also find that you refactored my example for multiple dispatched notifications from a background thread into some kind of recursive loop with a timerCmd and some additional messages and helper functions. Apart from making the NotificationPage code quite a bit harder to understand (I can't say I can easily follow that many similar names), it also doesn't answer my question - which was how to use the dispatch function handed to me by Cmd.ofEffect in a less awkward way than Dispatcher.UIThread.Post(fun _ -> dispatch(NotifyInfo "started")).

To be clear, the work - in the examples represented by the Async.Sleep 1000 - should happen on a background thread while the notifications are dispatched on the UI thread as conveniently as possible.

It's up to you whether you want to simplify the NotificationPage examples - but can you tell me how to easily dispatch multiple notifications from Cmd.ofEffect - as in my original example? Thanks!

@h0lg
Copy link
Contributor Author

h0lg commented Feb 19, 2024

how to use the dispatch function handed to me by Cmd.ofEffect in a less awkward way than Dispatcher.UIThread.Post(fun _ -> dispatch(NotifyInfo "started"))

Maybe we have to raise this as an issue in https://github.com/fabulous-dev/Fabulous instead?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
2 participants