✅ Run code in main thread after Task.Run completed?
Hello, I have a long running void function that I execute in a separate thread by calling
Task.Run(MyFunction)
. Now how could I run another function in the main thread when this one ends? Do I need to make an event or something? What are the common approaches to this?34 Replies
You'll need a way to schedule a method to be invoked on the main thread the same way BeginInvoke works
Cant use BeginInvoke because I use AvaloniaUI instead of winforms :(
You can use
Dispatcher.BeginInvoke
also not defined (probably because it isnt cross platform)
Ah, it's called InvokeAsync in Avalonia
Do I just run that in my Task?
Yes, InvokeAsync will schedule a function to be called on the main thread
Seems to have zero effect
AuthStatus is just an ObservableProperty that is bound to a textblock in my UI
Every component on its own works, just when I combine them nothing happens, the function ends without the UI being modified
Don't call
.Start
Same effect sadly
Have you checked whether the passed method gets called or not?
yup, put Environment.Exit(0); right after for debugging, got called
So the method is getting called...
Also in Avalonia's docs they say it's better to use
Post
if you don't need to wait for a result to returnThe weird thing is, it even calls the Environment.Exit when I put it in the dispatch body
This is so weird
And updating AuthStatus in sync works like normal
is there any reason you can't just do ?
My UI would freeze doing that
no it won't
await
doesn't block the calling thread
or at least, it shouldn't unless you've done something wrongIs there any way to make a constructor async, or what is the best way to make it call an async function that runs this?
constructors cannot be async
what is this constructor for? A window or page?
(or something similar)
ViewModel of a page
does the Page have like a loaded event?
Pretty sure no
Viewmodel constructor is pretty much that
can you link the control you're using? I can't seem to find one called Page
ah
hm
I found this https://github.com/AvaloniaUI/Avalonia/issues/1049
GitHub
How to bind command to loaded event? · Issue #1049 · AvaloniaUI/Ava...
Has Avalonia any api similar with this?
but otherwise you might need to use the dispatcher I guess
The thing is just that the dispatcher doesnt seem to work
It can dispatch everything I tried so far, just not UI changes for some reason
if AuthStatus is being bound to, you might not even need the dispatcher
also, found this https://docs.avaloniaui.net/guides/basics/accessing-the-ui-thread
You can access the current UI thread via Dispatcher.UIThread. You can either use Post or InvokeAsync, if you want to run a job on the UI thread. Use Post when you just want to start a job, but you don't need to wait for the job to be finished and you don't need the result. If you need to wait for the result, then use InvokeAsync instead.it looks you should be using Post, or using InvokeAsync, but also awaiting it
Already tried both of those, both dont work
I also see nothing about Start
need to dip, have a work meeting starting
I copied 1:1 from that page, both approaches do nothing
Alright, thank you for your help
(Bump)
$close
Use the
/close
command to mark a forum thread as answered