❔ Program showing unwanted entries after multiple uses
When I run my code that returns entries from my database, if I run it multiple times it starts to add entries to the top that I don't want in the first place. I can't figure out why this is happening, looking over my code I can I'm guessing it is a problem with my menu class, but I'm not sure.
Screenshot explanation: I ran my showContacts() method 4 times, and it wrote an unwanted entry every time i ran the method
pastebin:
https://paste.mod.gg/wfrhxghnoxws/1
BlazeBin - wfrhxghnoxws
A tool for sharing your source code with the world!
35 Replies
I see this method as not working properly the first 3 times, and working correctly only the 4th time
Since it fetches all the contacts, and yet, the first 3 times it displays only a single one
i think i explained it wrong
first time i use the method
second time i use the method
the functionality is weird, sometimes it sticks with one extra entry at the top and then other times it will have 3 or 4
Could be because you have
async void
methods here and there?
If it's largely unpredictable, I'd say async issues
And async void
is a glaring oneoh is async void not supposed to be a thing
Correct
Those methods need to be
async Task
And they need to be await
ed when calledoooooh
is it alright if I have variables that call the await function instead of just having it by itself?
Variables don't call functions
or i mean hold it
Or do you mean
?
the value that results from the function
is that a better way to do it?
right now i have
just that
var foo = await FooAsync()
Asynchronous methods return a
Task
Or a Task<T>
To get a T
value from a Task<T>
it needs to be await
ed
Similarly, to ensure that a Task
has been actually completed, it needs to be await
ed
So:
a) Preferable
b) Acceptable
c) Unacceptable
d) Unless for fire-and-forget reasons, but then it's better to use a discard to be explicit
oh awesome
def gotta do more research on async methods if i dont even know i was supposed to return a task 🤦♂️
* If a method
await
s something inside, it needs to be async
* If a method is async
it should be await
ed
* If a method is async
it needs to return Task
or Task<T>
Those are the three most important rules of thumb
Anything else is nuanceperfect thanks
huh somehow i missed that the first time i looked it over
if you really want it async, you can change the entry point to
static async Task Main(...)
and bubble the await
s down through the rest of your code
but given the application i don't think there's any point in using async/awaitcan you explain what you mean by bubble the awaits down
so your main is marked
async
which allows you to await
other methods called in it
by bubble down i just mean do that for all of your other methods in your code that need to be asyncAsync code is, uh, infectious
but since you aren't actually doing anything in parallel, async isn't giving you any benefit here so it's not worth the overhead and having to change all your other code to be async
your DbContext has synchronous variations of all of the current async methods you're calling on it (just remove the
Async
)
the main benefit of async/await is to avoid consuming CPU resources when you're waiting on IO bound work, but you don't have any other code that needs CPU while you wait for your async calls to finishyeah that makes sense since im not oding anything else
but lets say there is a frontend
i would then want to use async right?
correct
got it
yeah i'll go ahead and get rid of async for this proj and use it in the future
if it was something like a website where you're serving multiple requests at once
async/await
will make your code more efficient overall because the CPU threads that would normally be tied up waiting for your database to reply are free to handle more requestsmakes a lot more sense, thank you
i'm getting the same problem i was earlier with the extra entries now that everything is back to voids
should I be using a different keyword for the delcaration of these methods?
If a method doesn't return anything, it should be
void
That's unlikely to be an issue
If your methods are not async void
anymore but just void
, and if you have no async
code... I'm not sure what's wrong
Nothing I can see at a glanceyup no more async's
just internal void
yeah idk, i don't really see anything that can go wrong in
showContacts()
me neither lol
i'll just keep looking at it for a little and hope i figure it out
Pop a breakpoint somewhere and go through the code step by step?
good idea i didn't think of that
Was this issue resolved? If so, run
/close
- otherwise I will mark this as stale and this post will be archived until there is new activity.