✅ 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?K
Kouhai /人◕ ‿‿ ◕人\•412d ago
You'll need a way to schedule a method to be invoked on the main thread the same way BeginInvoke works
F
1FriendlyDoge•412d ago
Cant use BeginInvoke because I use AvaloniaUI instead of winforms :(
K
Kouhai /人◕ ‿‿ ◕人\•412d ago
You can use
Dispatcher.BeginInvoke
F
1FriendlyDoge•412d ago
also not defined (probably because it isnt cross platform)
K
Kouhai /人◕ ‿‿ ◕人\•412d ago
Ah, it's called InvokeAsync in Avalonia
F
1FriendlyDoge•412d ago
Do I just run that in my Task?
K
Kouhai /人◕ ‿‿ ◕人\•412d ago
Yes, InvokeAsync will schedule a function to be called on the main thread
F
1FriendlyDoge•412d ago
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
K
Kouhai /人◕ ‿‿ ◕人\•412d ago
Don't call
.Start
F
1FriendlyDoge•412d ago
Same effect sadly
K
Kouhai /人◕ ‿‿ ◕人\•412d ago
Have you checked whether the passed method gets called or not?
F
1FriendlyDoge•412d ago
yup, put Environment.Exit(0); right after for debugging, got called
K
Kouhai /人◕ ‿‿ ◕人\•412d ago
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 returnF
1FriendlyDoge•412d ago
The 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
J
jcotton42•412d ago
is there any reason you can't just do ?
F
1FriendlyDoge•412d ago
My UI would freeze doing that
J
jcotton42•412d ago
no it won't
await
doesn't block the calling thread
or at least, it shouldn't unless you've done something wrongF
1FriendlyDoge•412d ago
Is there any way to make a constructor async, or what is the best way to make it call an async function that runs this?
J
jcotton42•412d ago
constructors cannot be async
what is this constructor for? A window or page?
(or something similar)
F
1FriendlyDoge•412d ago
ViewModel of a page
J
jcotton42•412d ago
does the Page have like a loaded event?
F
1FriendlyDoge•412d ago
Pretty sure no
Viewmodel constructor is pretty much that
J
jcotton42•412d ago
can you link the control you're using? I can't seem to find one called Page
F
1FriendlyDoge•412d ago
J
jcotton42•412d ago
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?
J
jcotton42•412d ago
but otherwise you might need to use the dispatcher I guess
F
1FriendlyDoge•412d ago
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
J
jcotton42•412d ago
if AuthStatus is being bound to, you might not even need the dispatcher
F
1FriendlyDoge•412d ago
J
jcotton42•412d ago
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
F
1FriendlyDoge•412d ago
Already tried both of those, both dont work
J
jcotton42•412d ago
I also see nothing about Start
need to dip, have a work meeting starting
F
1FriendlyDoge•411d ago
I copied 1:1 from that page, both approaches do nothing
Alright, thank you for your help
(Bump)
$close
M
MODiX•411d ago
Use the
/close
command to mark a forum thread as answeredWe are a programming server aimed at coders discussing everything related to C# (CSharp) and .NET.
52KMembers
View on DiscordWant results from more Discord servers?
More Posts❔ methodHello,
If I create a method, does the method know about things in the Main method? Or am I supposed ❔ Project structure1. In my Solution there should be only 1 Project right? Unless its a big App
2. I started using fo❔ how to use EF6 in library with no dependancy injection and no startup.csI used EF core power tools to generate DB Context and reverse engineer the models.
now I get an erro❔ HeadFirst Design PatternSo I started with this Book today. First problem starts with the Duck Project:
and when he tests h✅ EqualityComparerHello, anyone know of this code work ?
--> `EqualityComparer<ICollection<myclass>> .Default.Equals❔ error attaching script (unity)this error appears when trying to attach the script, does anyone know how to solve it?❔ i have an app that runs well on my pc but not on other pcsi have an app that runs well on my pc but on when i zip it and bring it to another pc the ui is all yield???```cs
public IEnumerable<int> A()
{
for (int i = 0; i <= 10; i += 2)
yield return i;
}
C# Data Table issuehttps://pastebin.com/DSHwSh0M
based on this program, I need to set the values of the `Old PS A11Y S❔ Microsoft Graph SDK - Serializing objectsHello,
I am currently working with Microsoft Graph API to work with the Planner. My goal is to backu❔ XML deserialise different types into a single listI'm using the build in XML serialisation library (unless it's with .NET framework?), and I current h❔ Access things from different scene in UnityI have a Class Selector scene that has a Class Selector Script component (MonoBehaviour) attached to❔ C# how do I write an If-statement to handle a button?Hi, I wanted some assistance with writing a If-statement that filters out the zones section in my ap❔ Magnetic Links for application?Hey, does someone have an article/tutorial/explanation how I can develop magnetic links for my appli❔ Show values from a list in a datagridview getting source from bindingsourceHello! I have the following problem: I am currently developing a winforms app with a datagridview th❔ Render email content .NET 7 (razor)I have to reinplement email content rendering, because currently its not looking really good, especi❔ GetAxis wont workpublic class Driver : MonoBehaviour
{
// Start is called before the first frame update
const✅ (SOLVED)Help a Beginneri am following CodeMonkeys 11Hr youtube video on coding a game in unity. I have ran into a issue whe❔ Exclude items from linq query if they contain one from an array of substringsI've got a list of strings, that if they appear anywhere in a linq query column i want to exclude th✅ Comprehension questions (LeetCode Binary Search)Like the title says it's about this puzzle https://leetcode.com/problems/binary-search/description/