Set something in the form of a graphical interface and a flow of sleep

I have a GUI window form. I want to set the image to pictureBox and the next dream 3 seconds.

   pictureBox.Image = image;
   Thread.Sleep( 3000 );

But if I do this as my code above, my form will try to set the image, then go to sleep mode for 3 seconds, and right after that, my form is drawn by itself. So, my picture is after these 3 seconds. How can I set the image, show it, and immediately after that "sleep"?

change 1

exactly i want to do something like this:

I have two threads, a user interface and a graphical interface. The user interface reads from the network socket and the correct call method from the graphical interface. And there could be a script like this:

  • GUI User Interface: Image Setup
  • GUI user interface: do something (then the GUI should clear the image)

But I want to be sure that I can see this image. Therefore, after installing the GUI, I call this thread for 3 seconds. So how can I do this?

Example: (function from GUI)

public void f1() {
     MethodInvoker method = () => {
            pictureBox.Image = image;
            pictureBox.Update();
           // do something more
     };

     if ( InvokeRequired ) {
           Invoke( method );
     } else {
           method();
     }

 }

public void f2() {
     MethodInvoker method = () => {
            pictureBox.Image = null;
            pictureBox.Update();
           // do something more
     };

     if ( InvokeRequired ) {
           Invoke( method );
     } else {
           method();
     }

 }

And another function f3 ... fn

 public void f3() {
     MethodInvoker method = () => {
           // do something 
     };

     if ( InvokeRequired ) {
           Invoke( method );
     } else {
           method();
     }

 }

And, I am my function of calling the thread f1 and after it f2, I want to be sure that my user can see this image. But if my thread call function f1 and some of f3..fn call it normally.

change 2

No, I do this: I define a function in the form of a GUI (which is called by the UI):

  public void f1() {
         MethodInvoker method = () => {
                pictureBox.Image = image;
                pictureBox.Update();
         };

         MethodInvoker method2 = () => {
                // something
         }

        if ( InvokeRequired ) {
                Invoke( method );
                Thread.Sleep( 3000 );  // sleep UI thread
                Invoke( method2 );
        } else {
                method();
                method2();
        }
  }

It works, but it is not the best solution. If there is a script as follows:

- UI call f1
- UI call f3

UI will sleep for 3 seconds, and I do not expect this.

What is the best solution for my problem?

+3
source share
2 answers

:

Form.Refresh ();
Application.DoEvents;

Application.DoEvents

WindowsForms.

+3

. , , . Thread.Sleep , , ; .

, System.Windows.Forms.Timer. , . , , , " ".

  • .
  • , , , "" .
  • .
  • , Tick , , , " " .
+3

All Articles