blazor webpage not displaying new information or delaying with Task.Delay()
so i'm making a blazor app that plays the war game. after each round i want it to display the winner of the round as well as the card that won them the round. i was able to get this information to display in the console but never able to got it to display on the actual webpage and i'm not sure why
52 Replies
Task.Delay() and StateHasChanged() don't seem to have any impact
so i'm not sre what else to do
displays just fine in console as well
What exactly isn't working as expected?
And could you please share the complete .razor page including the Properties?
ya
if yo need me to post the classes as well just lmk
uhhh
when i run the program nothing updates on the actual web page
at the same time the console has output so i know that it's running fine
and i've walked thru it a few times in debug mode and the program itself works how i want it to
it just won't display things on the page
So the @if statements in your html evaluate correctly?
they should
whenever i fire playgame it changes running to true and currently there's nothing that changes running to false
and that should trigger the foreach
I believe you can actually put breakpoints in your HTML as well for debugging.
But could you just try putting some random text in that if to see if it get's rendered?
inside the if statements
or just like random
Inside the if just some
<span>SomeText</span>
to see if that updates correctly.nop
it jst doesn't seem to like detect the change in running? even tho i put StateHasChanged after every round
so im not really sure man
ā¹ļø
Typically you don't need to call StateHasChanged() at all.
Ok try to display the running bool outside your if statement to see if the variable actually updates the way you want to
yup running doesn't update at all on the webpage
it doesn't seem anything changes on the webpage after i run playgame
i added this at the end of playgame and when i click the button again
it doesn't display that error message
just to be sure if you add this, does that work and update correctly on your end?
i'll see
works good before i start a game
doesn't work after i start a game
idk šš
Ok let's see then
Wait a second
So you are calling Task.Delay() without awaiting.
Never ever call an async Method and not await it!
how do dat
We don't really need to get into await / async here, but you should maybe read some guides or watch a video or two about it.
You will not need to do any async programming for this anyways.
But the very basics are.
Async methods are awaited using the
await
keyword.
This can only be used in async
methods.
Async
methods should (basically) always return a Task
or Task<T>
.to use it in this case would i need to turn the whole RunGame() into an async task
i want it to wait 1 second after each turn instead of going instantly
Ok, then you will have to turn everything into
async
methods.gotcha
so if i'm not returning anything from the method
do i just add async
and if i'm returning something it has to be an async Task<datatype of what i will retrn>
If you would return
void
return a task
instead.
gotcha
async void methods have some very very specific cases, where they have to be used.
But generally they should be avoided if possible.
okay i'll try to remember that
i got it to work and the information displays now
but generally where do you want to put the return if it's just a Task
I don't quite understand the question?
hm
so in this one you returned it inside of this if statement
and for mine
i ended up slapping the return at the very end
when the game is over
what is the return actually doing? just breaking out of the method?
Generally speaking yes.
What I did is called an early return. It is used to make code more readable.
You should really try applying this to your code.
Let me write up an example real quick.
gotcha
Let's take this method.
If you just read it from top to bottom, you kind of know what will happen.
But it get's more nested and confusing real fast, especially the longer and more complex your method ist.
This could be reduced down to
Now you can just read the method from top to bottom.
You have a clear idea of where you can stop reading and the code is segmented into nice little pieces.
This is just for demonstration, your method should't actually return at so many places, when you have returns all over the place you can start thinking of extracting stuff into their own smaller methods.
A more realistic use case would be something called guard clauses
Where you check some conditions at the start of your method and return early if they are not met.
i see
does the ? on string? make it so that it can pass through even if it's null
Yes that's right.
That's C# nullable annotations.
It should be turned on by default in the newer project templates.
i see
these examples are super helpful
thank you for showing them
Sure no problem, happy to help
would it be ideal to convert all my other methods in my @Code{} to async tasks as well
oops
sorry whoever i pinged
Hmm that's actually not that easy to answer.
Having a method marked with
async
does come with a bit of a performance cost (it is 100% negligible in your and most cases!).
A method that doesn't await anything often just returns a Task, without being an async method itself.
I honestly can't give you a strong answer to your question.
Async has this property of being "contagious", if one thing down the chain is async, everything else has to be async as well.
So you could just write everything to have that in mind.
But async also has downsides.
For example you cannot use out
parameters.
Or yield
.
So the real answer in the end probably is "use async, when you need it".
Anything that needs to do IO calls at some point, like web requests or reading files, should be async.i see
in my case only 1 other thing really "interacts" with RunGame()
which is just PlayGame()
and i did get a green squiggle under that one saying i never declared an await for RunGame()
so i gess that is the only one i need to convert
i guess that's all i need in terms of actual programming help, you were super helpful so thank you a lot!
my project is basically done for the most part
Now to the horrible lands of css huh?
how did you know š i was just about to ask about formatting this table
Yeah that's not something I can help you with.
I personally always use some component library like MudBlazor and pray that I don't have to actually touch the CSS too much.
gotcha
i will just look online for this one cus it should be relatively simple
hopefully
once again thank you for your help, i really appreciate it and i learn a lot
Yeah some basic tables should be doable.
And there are a lot of ressources for CSS
100%
Is this some project for school / university or just for yourself to learn?
it's a project for university
Good luck then!
Looks like a neat project
thank you!
it's been pretty fun so far
just ended up getting stumped and came here and you were able to help me solve it
Yeah that was a sneaky problem, but we got there in the end.