C
C#17mo ago
GIGA BRAIN

❔ 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
Angius
Angius17mo ago
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
GIGA BRAIN
GIGA BRAINOP17mo ago
i think i explained it wrong
GIGA BRAIN
GIGA BRAINOP17mo ago
GIGA BRAIN
GIGA BRAINOP17mo ago
first time i use the method
GIGA BRAIN
GIGA BRAINOP17mo ago
GIGA BRAIN
GIGA BRAINOP17mo ago
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
Angius
Angius17mo ago
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 one
GIGA BRAIN
GIGA BRAINOP17mo ago
oh is async void not supposed to be a thing
Angius
Angius17mo ago
Correct Those methods need to be async Task And they need to be awaited when called
GIGA BRAIN
GIGA BRAINOP17mo ago
oooooh is it alright if I have variables that call the await function instead of just having it by itself?
Angius
Angius17mo ago
Variables don't call functions
GIGA BRAIN
GIGA BRAINOP17mo ago
or i mean hold it
Angius
Angius17mo ago
Or do you mean
var foo = FooAsync();
await foo;
var foo = FooAsync();
await foo;
?
GIGA BRAIN
GIGA BRAINOP17mo ago
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()
Angius
Angius17mo ago
Asynchronous methods return a Task Or a Task<T> To get a T value from a Task<T> it needs to be awaited Similarly, to ensure that a Task has been actually completed, it needs to be awaited So: a) Preferable
var foo = await FooAsync();
var foo = await FooAsync();
b) Acceptable
var task = FooAsync();
var foo = await task;
var task = FooAsync();
var foo = await task;
c) Unacceptable
FooAsync();
FooAsync();
d) Unless for fire-and-forget reasons, but then it's better to use a discard to be explicit
_ = FooAsync();
_ = FooAsync();
GIGA BRAIN
GIGA BRAINOP17mo ago
oh awesome def gotta do more research on async methods if i dont even know i was supposed to return a task 🤦‍♂️
Angius
Angius17mo ago
* If a method awaits something inside, it needs to be async * If a method is async it should be awaited * 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 nuance
GIGA BRAIN
GIGA BRAINOP17mo ago
perfect thanks
Jimmacle
Jimmacle17mo ago
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 awaits down through the rest of your code but given the application i don't think there's any point in using async/await
GIGA BRAIN
GIGA BRAINOP17mo ago
can you explain what you mean by bubble the awaits down
Jimmacle
Jimmacle17mo ago
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 async
Angius
Angius17mo ago
Async code is, uh, infectious
Jimmacle
Jimmacle17mo ago
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 finish
GIGA BRAIN
GIGA BRAINOP17mo ago
yeah that makes sense since im not oding anything else but lets say there is a frontend i would then want to use async right?
Jimmacle
Jimmacle17mo ago
correct
GIGA BRAIN
GIGA BRAINOP17mo ago
got it yeah i'll go ahead and get rid of async for this proj and use it in the future
Jimmacle
Jimmacle17mo ago
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 requests
GIGA BRAIN
GIGA BRAINOP17mo ago
makes 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?
Angius
Angius17mo ago
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 glance
GIGA BRAIN
GIGA BRAINOP17mo ago
yup no more async's just internal void
Jimmacle
Jimmacle17mo ago
yeah idk, i don't really see anything that can go wrong in showContacts()
GIGA BRAIN
GIGA BRAINOP17mo ago
me neither lol i'll just keep looking at it for a little and hope i figure it out
Angius
Angius17mo ago
Pop a breakpoint somewhere and go through the code step by step?
GIGA BRAIN
GIGA BRAINOP17mo ago
good idea i didn't think of that
Accord
Accord17mo ago
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.
Want results from more Discord servers?
Add your server