I'm writing a program to visualize different sorting algorithms. I'm using Winforms and using the form's paint even to update the graphs. The problem that I'm having is that I can't simply write a method that sorts the array because the form only gets updated when the method is completed. I tried using a timer as a for loop with global variables, and calling Refresh() but it makes my code a lot less reusable and looks ugly.
How can I fix this issue?
Edit: To be clear, the form isn't locking up, the form just isn't repainting itself.
Edit2: Here is where I'm currently calling my sorting methods. I had to replace the outer for loop that you normally use to sort arrays, with global variables
private void Timer_Tick(object sender, EventArgs e)
{
switch (sortMethod)
{
case "Selection Sort":
color = Sort.SelectionSort(color, ref i);
break;
case "Bubble Sort":
color = Sort.BubbleSort(color, ref i);
break;
case "Cocktail Shaker Sort":
bool sorted = false;
color = Sort.CocktailShakerShort(color, ref sorted);
break;
case "Gnome Sort":
if (pos < color.Length)
{
color = Sort.GnomeSort(color, ref pos);
}
break;
default:
timer.Enabled = false;
break;
}
Invalidate();
}