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 .
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.
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:
Then you can do this inside your thread:
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
Taskyou can look at this excellent blog:http://reedcopsey.com/2010/03/18/parallelism-in-net-part-15-making-tasks-run-the-taskscheduler/