Pausing thread untill asynchronous operation finishes.
-
I am doing some asynchronous operation(
Webclient.DownloadFileAsync()
) which is working on a separate thread. This will raise some events when the download process finished. Is it possible to stop the current thread until these events are fired ? Inside the event handler, I need to resume the thread. Any idea's ?All C# applications should call Application.Quit(); in the beginning to avoid any .NET problems.- Unclyclopedia My Website | Ask smart questions
-
I am doing some asynchronous operation(
Webclient.DownloadFileAsync()
) which is working on a separate thread. This will raise some events when the download process finished. Is it possible to stop the current thread until these events are fired ? Inside the event handler, I need to resume the thread. Any idea's ?All C# applications should call Application.Quit(); in the beginning to avoid any .NET problems.- Unclyclopedia My Website | Ask smart questions
Hello, If you mean Stop and Start the MainThread and you have the instance of the main Window for example. You could call something like this from your working thread.
private void StopThread() { if(InvokeRequired) { Invoke..... return; } Thread actThread = Thread.CurrentThread; actThread.Suspend(); // to start use "actThread.Start();" }
All the best, Martin
-
Hello, If you mean Stop and Start the MainThread and you have the instance of the main Window for example. You could call something like this from your working thread.
private void StopThread() { if(InvokeRequired) { Invoke..... return; } Thread actThread = Thread.CurrentThread; actThread.Suspend(); // to start use "actThread.Start();" }
All the best, Martin
Hi, I am doing unit test with NUnit. The application which I am testing is a downloader which downloads files from remote server. So once the downloading finishes, I need to assert the file size. But since the download process is happening in the other thread, NUnit GUI is not waiting until download completes. Hence I was not able to test the events. I managed it by running the download class in another thread, just below to that I used
Thread.Sleep()
to make NUnit GUI wait for some time. By this time, the thread which I created will finish the download work and calls all events. So I can test the events too. Anyway thanks for your suggestion.All C# applications should call Application.Quit(); in the beginning to avoid any .NET problems.- Unclyclopedia My Website | Ask smart questions
-
Hi, I am doing unit test with NUnit. The application which I am testing is a downloader which downloads files from remote server. So once the downloading finishes, I need to assert the file size. But since the download process is happening in the other thread, NUnit GUI is not waiting until download completes. Hence I was not able to test the events. I managed it by running the download class in another thread, just below to that I used
Thread.Sleep()
to make NUnit GUI wait for some time. By this time, the thread which I created will finish the download work and calls all events. So I can test the events too. Anyway thanks for your suggestion.All C# applications should call Application.Quit(); in the beginning to avoid any .NET problems.- Unclyclopedia My Website | Ask smart questions
i'd catch the donwloadcomplete event in unit-test and setting a flag (complete=true). you can start the test then as follows:
[Test] public void TestDownload() { Downloader.DownloadReady += ... // set complete=true in declared method Downloader.StartDownload(path) while (!complete) Application.DoEvents(); }
-
i'd catch the donwloadcomplete event in unit-test and setting a flag (complete=true). you can start the test then as follows:
[Test] public void TestDownload() { Downloader.DownloadReady += ... // set complete=true in declared method Downloader.StartDownload(path) while (!complete) Application.DoEvents(); }
WOW ! That's great. Thanks for pointing this out
All C# applications should call Application.Quit(); in the beginning to avoid any .NET problems.- Unclyclopedia My Website | Ask smart questions
-
I am doing some asynchronous operation(
Webclient.DownloadFileAsync()
) which is working on a separate thread. This will raise some events when the download process finished. Is it possible to stop the current thread until these events are fired ? Inside the event handler, I need to resume the thread. Any idea's ?All C# applications should call Application.Quit(); in the beginning to avoid any .NET problems.- Unclyclopedia My Website | Ask smart questions
-
i'd catch the donwloadcomplete event in unit-test and setting a flag (complete=true). you can start the test then as follows:
[Test] public void TestDownload() { Downloader.DownloadReady += ... // set complete=true in declared method Downloader.StartDownload(path) while (!complete) Application.DoEvents(); }
Calling
Application.DoEvents
is bad practice. It often results in unexpected results, so I would really advice against it. If you can get a handle to the thread I'd use itsJoin
method. If this isn't the case, you can use anAutoResetEvent
to signal from one thread to the other. You can read more about it here[^].Standards are great! Everybody should have one!
-
I am doing some asynchronous operation(
Webclient.DownloadFileAsync()
) which is working on a separate thread. This will raise some events when the download process finished. Is it possible to stop the current thread until these events are fired ? Inside the event handler, I need to resume the thread. Any idea's ?All C# applications should call Application.Quit(); in the beginning to avoid any .NET problems.- Unclyclopedia My Website | Ask smart questions