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 9115837
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T04:31:41+00:00 2026-06-17T04:31:41+00:00

I have a WPF canvas in which I want to display different color Rectangles

  • 0

I have a WPF canvas in which I want to display different color Rectangles (stored in a multi-dimensional array) every x miliseconds.

Random rnd = new Random();

foreach (var i in Enumerable.Repeat(1, 100))
{
    _rectGrid[rnd.Next(0, 30), rnd.Next(0, 30)].Fill = new SolidColorBrush(Colors.Blue);
    Thread.Sleep( 100 );
    // refresh somehow here?
}

This works, but I don’t see it update in real time since it is in the MainWindow constructor.

Clearly the Rectangles have to be created in the GUI thread, but if I create a Timer to change the colors it’s in a different thread.

Can I create a multi-dimensional array of values and bind the values to the colors in the Rectangle array so I can access them from another thread? And if I do that, how do I tell the GUI thread to redraw?

Maybe it would be simpler to have a button the user clicks first so this doesn’t happen in the constructor?

EDIT:

The DispatcherTimer worked great. Why MS has a separate class for this is beyond me.

enter image description here

Here is the source I used it for.

  • 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-06-17T04:31:42+00:00Added an answer on June 17, 2026 at 4:31 am

    You could use the DispatcherTimer like this:

        public MainWindow()
        {
            InitializeComponent();
    
            DispatcherTimer t = new DispatcherTimer();
            t.Tick += t_Tick;
            t.Interval = new TimeSpan(0, 0, 0, 0, 300);
            t.Start();
        }
        Random r = new Random();
        void t_Tick(object sender, EventArgs e)
        {
            byte[] rnd = new byte[4];
            r.NextBytes(rnd);
            this.Background = new SolidColorBrush(Color.FromArgb(rnd[0], rnd[1], rnd[2], rnd[3]));
        }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

In my WPF application I have a Canvas in which I do some drawing.
I have a WPF Panel (like Canvas for example) and I want to remove
I have a custom WPF Canvas, upon which I would like to show a
In my WPF project I have a canvas on which I draw an ellipse
I have a WPF Canvas with .NET-4.5. I added events (which autocreated methods for)
I have a WPF application in which I'm moving data around on a Canvas.
I have a WPF (.NET 3.5) control which renders about 20000 rectangles. This MyControl
Using WPF I have a list of rectangles (which can have an undefined number
I am using WPF and C# and I have a canvas with rectangles on
I have a WPF window, which contains a Canvas object (name: canvas). When the

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.