why is this only occasionally async
the main reason for why I need this to be async is because it is an entire form with downloads that has a progress bar a total size and live updating download speed and current mb downloaded (the reason why there is a stopwatch) I have managed to get it to be this exact code here and async a few times although most of the time it isn't
47 Replies
You say you need it to be async, but nothing in your code is async
Well, there's one method with
Async
suffix in the name
But it's not handled like an asynchronous method should be"We don't recommend that you use the WebClient class for new development. Instead, use the System.Net.Http.HttpClient class."
https://learn.microsoft.com/en-us/dotnet/api/system.net.webclient?view=net-8.0
I personally prefer webclient
I mean other downloads are async and the format is the same just changed link and filename
They are smaller files though
The worker itself is being ran asynchronously
Adding the suffixes async did not do anything
You can't say "I need this to be async" and "I prefer Webclient" at the same time
Webclient is not, never was, and never will be async
The
Async
suffixed method is not asynchronous eitherI've seen webclient being async before
It's not
Never was
The workaround is refreshing the form
Worked pretty well for one of my friends
Not too much for me
Never will be
I mean I can record my code being async if I can get it to async again still using webclient
Or record a more consistently async download
Again still using webclient
Again
None of your code is asynchronous
None of it
Zero percent
.BeginInvoke()
kinda sorta tries to look like it's async I guessexplain this:
forgot to show it downloaded
oh well
I guess we have a different definition, then. To me, async is... well,
async
and await
But Winforms seems to have its own weird magicyeah I only ever need the ui to be updatable so the speed and progress can change
https://learn.microsoft.com/en-us/dotnet/api/system.windows.forms.control.begininvoke?view=windowsdesktop-8.0
Executes a delegate asynchronously on the thread that the control's underlying handle was created on.Could it be that the thread is the main UI thread? That would cause it to block the main thread
probably
you have made this code remarkably complicated by not using HttpClient
well. i guess it is harder to get the progress with HttpClient. fine, whatever
but, do not use BackgroundWorker
the BackgroundWorker is completely pointless here, the first thing you do in the BackgroundWorker is go back to the UI thread to do everything
you could have just put the stuff inside
BeginInvoke
right into button_Click and the effect would be the samedid try that before
still not async
i mean. it is a fact that the BackgroundWorker does nothing here. you are not doing anything in the background, it does not make anything asynchronous. so there's no point in having it
as for why it's only "occasionally async," you would need to show what you mean exactly
so I have managed to have it async a few times
it is async sometimes although rarely
i don't understand what it means for it to be not async
ui not updating in this case
this also shows how it sometimes is async and sometimes isnt
I dont even understand how that part works
why are u using a backgroundworker and not even using the
backgroundWorker_ProgressChanged
of it which would be the correct way to update your GUI without locking it up by overloading the invoker?ok. well, to be clear, it is asynchronous. you can tell because the progress bar is updating. if it was not asynchronous, it would not move at all
it is hard to say what the problem is without seeing
dl_DownloadProgressChanged
private void dl_DownloadProgressChanged(object sender, DownloadProgressChangedEventArgs e)
{
this.BeginInvoke(new Action(() =>
{
lblSpeed.Text = string.Format("{0} kb/s", (e.BytesReceived / 1024d / sw.Elapsed.TotalSeconds).ToString("0.00"));
lblDownload.Text = string.Format("{0} MB / {1} MB",
(e.BytesReceived / 1024d / 1024d).ToString("0.00"),
(e.TotalBytesToReceive / 1024d / 1024d).ToString("0.00"));
progressBar1.Value = e.ProgressPercentage;
}));
}
u dont need invoke in the progresschanged
progresschanged happens in the ui thread
I did try doing it without it before and that caused issues one time and reverted it
based on the fact this ui is generally inconsistent I am surprised I didnt have that happen even with it
like what specifically?
the progress bar just didnt do anything
I doubt that was the cause
giving you're invoking everywhere
I'll try again to remove it
and another problem
the progress bar doesnt reset
also this exists https://paste.mod.gg/llviodhkmebw/0
BlazeBin - llviodhkmebw
A tool for sharing your source code with the world!
its not my code, I think I found it on stackoverflow long time ago, but its much better than using webclient
BlazeBin - urevhknroxjy
A tool for sharing your source code with the world!
I forgot to vopy the main download page I just added utils
fixed it
it's a mess
needed to remove advertising
ah ok because that progresschanged is not the backgroundworker one
that's why it doesnt work
it's hard to say what's wrong because the given code looks completely fine
like, the progress bar is clearly updating, it's unclear why updating the labels would not also work
I should mention it only applies to larger files
which is the largest file in your list
edge webview installer 170mb
does it help to get rid of the BeginInvoke in
dl_DownloadProgressChanged
and dl_DownloadFileCompleted
nvm epic games launcher 177
no
the progress bar still worked but didn't reset
if you put a MessageBox.Show inside of DownloadFileCompleted, does the message box show up, even in the times when the progress bar does not reset
sort of
and its memory leaking
well not memory leaking in the full sense
it's stacking the form over and over
in memory