0
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace d3
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            pictureBox1.Image = Image.FromFile("C:\\Users\\ocean\\Desktop\\deneme.png");
            pictureBox1.Location = new Point(0, 0);
            pictureBox2.Image = Image.FromFile("C:\\Users\\ocean\\Desktop\\pul1.png");
        }

        protected override void OnMouseClick(MouseEventArgs e)
        {
            Graphics theGraphics = Graphics.FromHwnd(this.Handle);

            for (int i = 0; i < 200; i += 5)
            {
                pictureBox2.Location = new Point(i, 100);
                theGraphics.Flush();

                System.Threading.Thread.Sleep(50);
                pictureBox2.Invalidate();
            }           
        }
    }
}

In this code picturebox2 is moving ok but previous locations image stay ON THEuntil the loop finishes. When loop finished the older parts would be erased. I dont want the previous paintings inside the loop I just want to move on pictureBox1. Im new at C# so please help me:) In J2Me I was using flushgraphics but here I tried and it did not work and If you can give an example I would be happy .

John Saunders
  • 160,644
  • 26
  • 247
  • 397
albatross
  • 455
  • 2
  • 8
  • 27
  • 1
    do not do this in a for loop with a thread.sleep, instead do it with a timer that moves the image 5 pixels every 50 ms then stops. This will allow the change to draw to the screen properly – box86rowh Aug 27 '11 at 15:26
  • This code doesn't allow the form's PaintBackground method to run, required to replace the pixels. Looping like this is never not a problem, stuff stops working properly if the message loop doesn't run. Use a Timer instead, this.Update() is the band-aid. – Hans Passant Aug 27 '11 at 17:31

1 Answers1

1

In C#, just as in Swing, if you are on the UI or event thread, nothing that you change will be noticed by the user until you are done.

So, if you want to move these, your best bet is to start by getting off the UI thread, by starting a new thread, and then go through your loop.

But, the problem is that you will need to be on the UI thread to make the changes.

This is why your sleep didn't work, you are just putting the event thread to sleep, btw.

Which version of C# are you using? There are many options on creating a thread, and on working with the UI thread.

Here is a link to creating a thread: http://msdn.microsoft.com/en-us/library/7a2f3ay4(v=vs.80).aspx

Here is a way to deal with how to get back to the UI thread:

http://blogs.msdn.com/b/csharpfaq/archive/2004/03/17/91685.aspx.

For example, to create a thread you can do this, which came from http://www.rvenables.com/2009/01/threading-tips-and-tricks/

I do my threads this way as I find it simpler to see what is happening.

 (new Thread(() => {
        DoLongRunningWork();
        MessageBox.Show("Long Running Work Finished!");
    }) { Name = "Long Running Work Thread",
        Priority = ThreadPriority.BelowNormal }).Start();

The most complete answer on how to do your update on the UI thread would be this question:

How to update the GUI from another thread in C#?

UPDATE:

For the part where it states to do long running work, you can add this before the thread block:

TaskScheduler uiScheduler = TaskScheduler.FromCurrentSynchronizationContext();

Then you can do this inside your thread:

(new Task(() =>
    {
        //copy pixel
        pictureBox2.Invalidate();  // You may want to just invalidate a small block                 around the pixel being changed, or every some number of changes.
    }))
.Start(uiScheduler);

By making these changes you could simplify making the change you want, but some of this may be more complex than what you want, which is why I gave some other links to give more information. But, for more on using Task you can look at this excellent blog:

http://reedcopsey.com/2010/03/18/parallelism-in-net-part-15-making-tasks-run-the-taskscheduler/

Community
  • 1
  • 1
James Black
  • 41,583
  • 10
  • 86
  • 166
  • visual c#2010 express edition can you write an example code because in j2me I wrote in runnable method which is an interface. Inside it there is a loop while(true) and in the end there is a thread and each movement I wrote flushGraphics and it works well however in c# I tried flushgraphics it does not wok I m new at c# so if you can write an example code it would be grateful. Thnx by the way – albatross Aug 27 '11 at 21:16