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. When main thread working the Whole Form Freezes

When main thread working the Whole Form Freezes

Scheduled Pinned Locked Moved C#
question
5 Posts 4 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.
  • K Offline
    K Offline
    kourvoisier
    wrote on last edited by
    #1

    How can i prevent my form from freezing when doing a large fucntion. For instance: I read a 20,000 line file line by line. after each line i do something with the data. This could take a long time. So i would use a progress bar to show progress. The progress bar starts but after a couple of seconds the progress bar does not move and the form freezes up until the process is finished. How can i prevent this from happening. Do i use another Thread? :rose:

    S O L 3 Replies Last reply
    0
    • K kourvoisier

      How can i prevent my form from freezing when doing a large fucntion. For instance: I read a 20,000 line file line by line. after each line i do something with the data. This could take a long time. So i would use a progress bar to show progress. The progress bar starts but after a couple of seconds the progress bar does not move and the form freezes up until the process is finished. How can i prevent this from happening. Do i use another Thread? :rose:

      S Offline
      S Offline
      Sean89
      wrote on last edited by
      #2

      Yes. Using another thread will fix the problem. In .Net 2.0 you can use the backgroundworker class, which seems to be an improvement over the Thread class.

      1 Reply Last reply
      0
      • K kourvoisier

        How can i prevent my form from freezing when doing a large fucntion. For instance: I read a 20,000 line file line by line. after each line i do something with the data. This could take a long time. So i would use a progress bar to show progress. The progress bar starts but after a couple of seconds the progress bar does not move and the form freezes up until the process is finished. How can i prevent this from happening. Do i use another Thread? :rose:

        O Offline
        O Offline
        Office Lineman
        wrote on last edited by
        #3

        If you use a worker thread, make sure you don't try to update your progress bar directly from that thread; invoke a delegate method for updating your progress bar instead. -- I've killed again, haven't I?

        S 1 Reply Last reply
        0
        • O Office Lineman

          If you use a worker thread, make sure you don't try to update your progress bar directly from that thread; invoke a delegate method for updating your progress bar instead. -- I've killed again, haven't I?

          S Offline
          S Offline
          Sean89
          wrote on last edited by
          #4

          However, if you are using .Net 2.0 the background worker class has a progress updated event handler in which you can update your progress bar.

          1 Reply Last reply
          0
          • K kourvoisier

            How can i prevent my form from freezing when doing a large fucntion. For instance: I read a 20,000 line file line by line. after each line i do something with the data. This could take a long time. So i would use a progress bar to show progress. The progress bar starts but after a couple of seconds the progress bar does not move and the form freezes up until the process is finished. How can i prevent this from happening. Do i use another Thread? :rose:

            L Offline
            L Offline
            LighthouseJ
            wrote on last edited by
            #5

            I've run into this before. I ended up using creating a single delegate function called ThreadEvent which does everything and derived my own class called ThreadEventArgs from EventArgs to handle communicating what the thread is reporting. Here's my delegate that handles whenever the thread sends any signal: private void ThreadEventDelegate (object sender, ThreadEventArgs e) if (<control>.InvokeRequired) {   this.BeginInvoke(new ThreadEvent (ThreadEventDelegate), new Object[] { sender, e }); } else {   <handle the event properly here> } This is needed because the thread cannot change a control. What this does is it asks the control what unique thread number it is under. Whatever current thread is running is compared and if they are the same, then it doesn't need to invoke and it's safe to change the control. If they are different, the thread calls BeginInvoke to execute a new delegate asynchronously with the same data it was passed inside the thread it should have been.

            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