unblock some item in form
-
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 -
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 " thxIt 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]
-
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 " thxThe 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
-
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 " thxabdellah 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
-
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
-
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