G
G
Goldfish2017-07-18 05:02:45
WPF
Goldfish, 2017-07-18 05:02:45

How to create an infinite background loop in Win Form?

Here is the built Win Form program (C#)

namespace sharp_test
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            SoundPlayer simpleSound = new SoundPlayer(@"kurlik.wav");
            simpleSound.PlayLooping();
        }


    }
}

The question is that I can’t help but figure out how to create an infinite loop now, while at the same time making it possible to access form controls.
This code needs to be made to work in a loop (in the background)
{
    Random rnd = new Random();
    int xi = 1;
    int yi = 1;
    while (true)
    {
        int vx = rnd.Next(0, 1);
        int vy = rnd.Next(0, 1);
        if (vx == 1) xi *= 1; else xi *= -1;
        if (vy == 1) yi *= 1; else yi *= -1;
        while (pictureBox1.Location.X > 12 & pictureBox1.Location.X < 272 & pictureBox1.Location.Y > 12 & pictureBox1.Location.Y < 249)
        {

            pictureBox1.Location = new Point(pictureBox1.Location.X + xi, pictureBox1.Location.Y + yi);
            textBox1.Text = Convert.ToString(pictureBox1.Location.X + xi);
        }
    }
}

(In short, this program should move the image from edge to edge from the very start of the application)
P.s tried through the timer and threads, but could not figure out how to access the controls, an error appeared all the time. I would be grateful for any sample code.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
T
tex0, 2017-07-18
@tex0

Are you sure you have WPF?
And that something is more similar to WinForms.
For working with controls from separate threads:
- for WinForms read here
- for WPF read about the thread model
UPD: the approach to solving the problem of norms (create a thread and spin your loop in it)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question