Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 431633
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T19:59:39+00:00 2026-05-12T19:59:39+00:00

I have a PictureBox control on a Form that is supposed to draw something

  • 0

I have a PictureBox control on a Form that is supposed to draw something every 100ms.

The background thread performs some calculation in a loop and after every iteration, it fires an event.

Edited (as a reply to a comment):

World world = new World();

void CreateBackgroundThread() {
    Thread backgroundThread = new Thread(world.BackgroundWorkerFunction);
    backgroundThread.Start();
}

 

public class World { 

    void BackgroundWorkerFunction() {
        IPiece piece = PieceFactory.Create();
        for (int i = 0; i < stepsCount; i++) {
            piece.Calculate();
            if (OnPieceStep != null) {
                OnPieceStep(piece);
            }
        }
    }

}

Inside the main form, there is a handler, set by:

world.OnPieceStep += DrawScreen;

and the Invoke wrapper (since the control to be drawn to, is created in the UI thread).

void DrawScreen(IPiece piece) {
    this.Invoke(new PieceStep(_drawScreen), piece);
}
void _drawScreen(IPiece piece) {
    drawer.Draw(world.Board, piece);
}

Now, I’d like the for loop to pause 100ms after each iteration, so I added Thread.Sleep(100); before firing an event:

for (int i = 0; i < stepsCount; i++) {
    IPiece piece = Calculate();
    if (OnPieceStep != null) {
        Thread.Sleep(100);
        OnPieceStep(piece);
    }
}

This however does not refresh the pictureBox every 100 ms, but draws only the last iteration of the loop, and only after the loop finishes.

Shouldn’t Thread.Sleep pause the thread it is called on, not the UI thread?
 
Update: I have just tried to click on the app while the background thread calculates. The program blocks (“not responding”), so the Thread.Sleep was obviously called on UI Thread. Is this expected behaviour or something is wrong with my threading?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-12T19:59:39+00:00Added an answer on May 12, 2026 at 7:59 pm

    As stated on this page:

    Thread.Sleep is unique amongst the
    blocking methods in that suspends
    Windows message pumping within a
    Windows Forms application, or COM
    environment on a thread for which the
    single-threaded apartment model is
    used. This is of little consequence
    with Windows Forms applications, in
    that any lengthy blocking operation on
    the main UI thread will make the
    application unresponsive – and is
    hence generally avoided – regardless
    of the whether or not message pumping
    is “technically” suspended. The
    situation is more complex in a legacy
    COM hosting environment, where it can
    sometimes be desirable to sleep while
    keeping message pumping alive.
    Microsoft’s Chris Brumme discusses
    this at length in his web log (search:
    ‘COM “Chris Brumme”‘).

    It seems that Thread.Sleep() in WinForms always pauses the UI, no matter in which thread is called.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

i have picturebox control in my form that SizeMode = StretchImage when i load
i have a user control that descends from UserControl. When dropped onto the form
I have a form that in run time i make many picture box control
I have a control (picturebox), in which I want to draw an 2D image
I'm implementing panning of a large image in a PictureBox control, and I have
I have a pictureBox on a Windows Form. I do the following to load
In C# i have a picturebox. i would like to draw 4 colors. The
I have an application that displays an image inside of a Windows Forms PictureBox
I have a PictureBox as UserControl . I added this User Control on the
I have a PictureBox control I want to display an image in it. I

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.