I noticed a while ago that form applications are very slow to draw. When I minimize/ maximize or go over it with another window, I can see the form components redrawn and/or I can erase the form for a second or so.
I was told it is because the same thread draws and does everything else, the drawing is slow. I was also told if I make the main thread to nothing but draw and another thread do everything else, the drawing would be fast without affecting my application performance.
I tried usning worked threads based on this page: http://www.daveoncsharp.com/2009/09/create-a-worker-thread-for-your-windows-form-in-csharp/
This is my code:
This is not quite what I had in mind. First of all, the code as is runs very slowly, posibly hours because updating the main delegate and having it draw the extra * takes forever, exactly what I need to avoid.
If you comment out the first if else in the heavyworker you will notice that the entire 10000000 iterations takes less than a second, so simply updating the main thread and having it draw slows it down and absurd amount of times.
First, is there a way to have it update, but only draw to the screen maybe once a second or so like the Application.ScreenUpdating = False in excel VBA so it will run fast and still draw to the screen regularly.
Also, if you have a break point in the break point or possible break point spots in the code and it is stopped, the window stops drawing. The whole point of this is that I want drawing to be completely separate from the underlying operation, so it would continue drawing what was last on the screen even if I am debugging, or the application busy.
I know it can be done as I have seen .NET applications which were very fast without the mentioned drawing issues or delays and without slowing down the application.
Can anybody help me modify this basic code to run like I want to?
Also, this is less important, but I would like the text box to show the number of the current iteration, not a series of stars so I need to send the int i to the delegate which I have also been trying to do.
Thanks,
elkinm.
I was told it is because the same thread draws and does everything else, the drawing is slow. I was also told if I make the main thread to nothing but draw and another thread do everything else, the drawing would be fast without affecting my application performance.
I tried usning worked threads based on this page: http://www.daveoncsharp.com/2009/09/create-a-worker-thread-for-your-windows-form-in-csharp/
This is my code:
Code:
public partial class Form1 : Form
{
private Thread workerThread = null;
private bool stopProcess = false;
private delegate void UpdateStatusDelegate();
private UpdateStatusDelegate updatedtatusdelegate = null;
public Form1()
{
InitializeComponent();
this.updatedtatusdelegate = new UpdateStatusDelegate(this.UpdateStatus);
}
private void UpdateStatus()
{
this.textBox1.Text += "*";
}
private void HeavyOperation()
{
for (int i = 0; i < 10000000; i++)
{ // POSSIBLE BREAK POINT HERE
if (!this.stopProcess)
{
this.Invoke(this.updatedtatusdelegate);
}
else
{
this.workerThread.Abort();
}
if (i == 100000|| i == 9000000)
{ //BREAK POINT HERE
}
}
}
private void button1_Click(object sender, EventArgs e)
{
this.stopProcess = false;
this.workerThread = new Thread(new ThreadStart(this.HeavyOperation));
this.workerThread.Start();
}
private void button2_Click(object sender, EventArgs e)
{
this.stopProcess = true;
}
}
If you comment out the first if else in the heavyworker you will notice that the entire 10000000 iterations takes less than a second, so simply updating the main thread and having it draw slows it down and absurd amount of times.
First, is there a way to have it update, but only draw to the screen maybe once a second or so like the Application.ScreenUpdating = False in excel VBA so it will run fast and still draw to the screen regularly.
Also, if you have a break point in the break point or possible break point spots in the code and it is stopped, the window stops drawing. The whole point of this is that I want drawing to be completely separate from the underlying operation, so it would continue drawing what was last on the screen even if I am debugging, or the application busy.
I know it can be done as I have seen .NET applications which were very fast without the mentioned drawing issues or delays and without slowing down the application.
Can anybody help me modify this basic code to run like I want to?
Also, this is less important, but I would like the text box to show the number of the current iteration, not a series of stars so I need to send the int i to the delegate which I have also been trying to do.
Thanks,
elkinm.
Last edited: