开发者

set something on GUI form and thread sleep

开发者 https://www.devze.com 2023-03-13 12:10 出处:网络
I have a GUI window form. I want to set image to pictureBox and next sleep 3 seconds. pictureBox.Image = image;

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

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

But if I do it like my code above, my form try to set image, next go to sleep for 3 seconds, and just after that my form draws itself. So my picture show after this 3 seconds. How can I set image, show it and just after that "go to sleep" ?

edit 1

exactly I want to do something like that:

I have two thread, UI and GUI. UI reads from net socket and call right method from GUI. And can be script like that:

  • UI call on GUI: set image
  • UI call on GUI: do something (then GUI must clear image)

But I want to have sure, that I will be able to see this image. So after GUI set image, I call this thread for 3 seconds. So, how can I do it?

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 other function f3...fn

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

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

 }

And, I my UI thread call function f1 and after it f2, I want to be sure that my user will be able to see this picture. But if my UI thread call function f1 and some between f3..fn call it normally.

edit 2

No I make it that: I define function in GUI form (which is called by 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 isn't the best solution. If will be script like this:

- UI call f1
- UI call f3

UI will be sleep for 3 seconds and I don't expect it.

What is the best solution for my problem?


You should never let your GUI sleep. At least not the way you are doing. If you use Thread.Sleep or other blocking mechanisms you will prevent the UI from doing what it is suppose to be doing; dispatching and processing messages.

If you really want to cause a delayed action then you would be better off using a System.Windows.Forms.Timer. Here is what I think you need to do. This of course is based on my vague understanding of what you mean by "go to sleep".

  • Set your image.
  • Then immediately disable all of the controls necessary that simulates whatever "go to sleep" means for you.
  • Then start the timer.
  • Finally in the Tick event add code that reverses whatever "go to sleep" means for you.


Try one of these:

Form.Refresh ();
Application.DoEvents;

Application.DoEvents

I'm assuming WindowsForms.

0

精彩评论

暂无评论...
验证码 换一张
取 消