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. unblock some item in form

unblock some item in form

Scheduled Pinned Locked Moved C#
help
7 Posts 5 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.
  • A Offline
    A Offline
    abdellah ab
    wrote on last edited by
    #1

    hey exemple int i = 0; while (i<20000000) { if (i=5000) { PictureBox.image = "x.gif" } i++; } the problem is that the picture box does not change the image if i=5000 ; it changed after loop "while " thx

    H D D 3 Replies Last reply
    0
    • A abdellah ab

      hey exemple int i = 0; while (i<20000000) { if (i=5000) { PictureBox.image = "x.gif" } i++; } the problem is that the picture box does not change the image if i=5000 ; it changed after loop "while " thx

      H Offline
      H Offline
      Heath Stewart
      wrote on last edited by
      #2

      It would seem you're blocking the UI thread - you're making the UI thread do so much work it can't keep up with all the other requests like to repaint. I assume you're executing this code in response to some control event or initialization code? While I'm sure this is just an example, assuming there's other work you should do that work in a worker thread and invoke the property setting on the UI thread since many times executing code on a control in a different thread than in which it was created can cause issues. First you'll need to define a method to set your image property.

      private void SetImageLocation(string uri)
      {
      if (pictureBox1.InvokeRequired)
      {
      Action setImageLocation = SetImageLocation;
      pictureBox1.Invoke(setImageLocation, new object[] { uri });
      }
      else
      {
      pictureBox1.ImageLocation = uri;
      }
      }

      To create a worker thread, consider using the ThreadPool like so:

      private void DoWork()
      {
      for (int i = 0; i < 20000; ++i)
      {
      if (i == 5000)
      {
      SetImageLocation("x.gif");
      }
      }
      }

      // Assumes you're executing work in response to a Button.Click event.
      private void button1_Click(object sender, EventArgs e)
      {
      ThreadPool.QueueUserWorkItem(DoWork);
      }

      This posting is provided "AS IS" with no warranties, and confers no rights. Program Manager II Visual Studio Professional Deployment Experience Microsoft [My Articles] [My Blog]

      1 Reply Last reply
      0
      • A abdellah ab

        hey exemple int i = 0; while (i<20000000) { if (i=5000) { PictureBox.image = "x.gif" } i++; } the problem is that the picture box does not change the image if i=5000 ; it changed after loop "while " thx

        D Offline
        D Offline
        DaveAuld
        wrote on last edited by
        #3

        The way Heath describes is the correct way to handle this type of situation. The really bad practice, which you should not do (but is handy when you just want to throw something together as a quick test or throwaway util, is;

        int i = 0;
        while (i<2000000000)
        {
        if (i==5000) {
        pictureBox1.Load("x.gif");

                        Application.DoEvents();
        
                    }
                    
                    i++;
                }
        

        This forces the message pump to catch up. But as i said already, this is bad practice, and should not be used in production code and will probably lead to you being shot by the code police :) o see why you should not be using it in production code, look here; http://blog.somecreativity.com/2007/11/19/a-look-at-doevents/[^], or search Google for DoEvents and it will become obvious its baaaaaaaad. So why tell you about then? Well, rather than you find out on your own and not know its bad and start to use it, its better to be told about it and learn why its bad and should be avoided, if that makes sense!

        Dave Find Me On: Web|Facebook|Twitter|LinkedIn


        Folding Stats: Team CodeProject

        1 Reply Last reply
        0
        • A abdellah ab

          hey exemple int i = 0; while (i<20000000) { if (i=5000) { PictureBox.image = "x.gif" } i++; } the problem is that the picture box does not change the image if i=5000 ; it changed after loop "while " thx

          D Offline
          D Offline
          Dr Walt Fair PE
          wrote on last edited by
          #4

          abdellah ab wrote:

          if (i=5000) {

          I'm surprised that even compiles! However, you may need to force a refresh of the control, not that I recommend you do that.

          CQ de W5ALT

          Walt Fair, Jr., P. E. Comport Computing Specializing in Technical Engineering Software

          B 1 Reply Last reply
          0
          • D Dr Walt Fair PE

            abdellah ab wrote:

            if (i=5000) {

            I'm surprised that even compiles! However, you may need to force a refresh of the control, not that I recommend you do that.

            CQ de W5ALT

            Walt Fair, Jr., P. E. Comport Computing Specializing in Technical Engineering Software

            B Offline
            B Offline
            BobJanova
            wrote on last edited by
            #5

            In C#, it won't compile.

            D 1 Reply Last reply
            0
            • B BobJanova

              In C#, it won't compile.

              D Offline
              D Offline
              DaveAuld
              wrote on last edited by
              #6

              I think he was writing pseudo code, as there are a few ommissions :)

              Dave Find Me On: Web|Facebook|Twitter|LinkedIn


              Folding Stats: Team CodeProject

              B 1 Reply Last reply
              0
              • D DaveAuld

                I think he was writing pseudo code, as there are a few ommissions :)

                Dave Find Me On: Web|Facebook|Twitter|LinkedIn


                Folding Stats: Team CodeProject

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

                Indeed, and we knew what he meant. I'm very happy that doesn't compile, actually, because I do it accidentally quite a lot and if I were writing C I would get my fingers burnt every time.

                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