Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Pausing thread untill asynchronous operation finishes.

Pausing thread untill asynchronous operation finishes.

Scheduled Pinned Locked Moved C#
csharphtmlcomquestioncareer
8 Posts 6 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • N Offline
    N Offline
    N a v a n e e t h
    wrote on last edited by
    #1

    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

    M T P 3 Replies Last reply
    0
    • N N a v a n e e t h

      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

      M Offline
      M Offline
      Martin 0
      wrote on last edited by
      #2

      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

      N 1 Reply Last reply
      0
      • M Martin 0

        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

        N Offline
        N Offline
        N a v a n e e t h
        wrote on last edited by
        #3

        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

        B 1 Reply Last reply
        0
        • N N a v a n e e t h

          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

          B Offline
          B Offline
          buchstaben
          wrote on last edited by
          #4

          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(); }

          N B 2 Replies Last reply
          0
          • B buchstaben

            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(); }

            N Offline
            N Offline
            N a v a n e e t h
            wrote on last edited by
            #5

            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

            1 Reply Last reply
            0
            • N N a v a n e e t h

              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

              T Offline
              T Offline
              telha
              wrote on last edited by
              #6

              Another simple way is to use Thread.Join() method.....

              Muhammad Talha

              1 Reply Last reply
              0
              • B buchstaben

                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(); }

                B Offline
                B Offline
                Bekjong
                wrote on last edited by
                #7

                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 its Join method. If this isn't the case, you can use an AutoResetEvent to signal from one thread to the other. You can read more about it here[^].

                Standards are great! Everybody should have one!

                1 Reply Last reply
                0
                • N N a v a n e e t h

                  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

                  P Offline
                  P Offline
                  pbraun
                  wrote on last edited by
                  #8

                  The way that I have implemented many solutions for this is to use a ManualResetEvent to pause the thread. Then when the work is complete in the second thread, set the ManualResetEvent allowing the first thread to continue.

                  Phil

                  1 Reply Last reply
                  0
                  Reply
                  • Reply as topic
                  Log in to reply
                  • Oldest to Newest
                  • Newest to Oldest
                  • Most Votes


                  • Login

                  • Don't have an account? Register

                  • Login or register to search.
                  • First post
                    Last post
                  0
                  • Categories
                  • Recent
                  • Tags
                  • Popular
                  • World
                  • Users
                  • Groups