Hi,
I'm writing a program(academic project) which reads a 5000000-element-text-file and sorts it by merge sort, quick sort and exchange sort, by threading, and showing the progresses graphically.
I've only written merge sort, but there is a problem in showing the progress when I use thread!
For showing the progress, I used a picture box to make a custom progress bar, but it doesn't work. By the way, it works without thread and doesn't have any problem!
I need to show the progress by using a picture box!
Now, how can I figure this out??
somebody help me, please!
Thanks in advance.
And the code:
public int[] arr1;
private void button1_Click(object sender, EventArgs e)
{
if (InitializingData())
{
label1.Visible = true;
Thread t1 = new Thread(delegate() { MergeSort(arr1); });
t1.Start();
}
}
private bool InitializingData()
{
if (!File.Exists(Application.StartupPath + @"\input.txt"))
{
MessageBox.Show("The input file was not found!");
return false;
}
try
{
#region Reading input file as a string array, and reserving space for input data
StreamReader objFileReader = new StreamReader(Application.StartupPath + @"\input.txt");
string[] str = objFileReader.ReadLine().Split(',');
objFileReader.Dispose();
arr1 = new int[str.Length];
arr2 = new int[str.Length];
arr3 = new int[str.Length];
MessageBox.Show("The usage space was reserved successfully!!!");
#endregion
#region Putting input elements from the string array to integer array
int cnt = 0;
foreach (string obj in str)
{
arr1[cnt] = int.Parse(obj);
arr2[cnt] = int.Parse(obj);
arr3[cnt++] = int.Parse(obj);
}
MessageBox.Show("The input data initialized successfully!!!");
#endregion
}
catch
{
MessageBox.Show("Wrong input!!!");
return false;
}
return true;
}
#region Merge Sort...
private void MergeSort(int[] l_arr)
{
int h = l_arr.Length / 2;
int m = l_arr.Length - h;
int[] left = new int[h];
int[] right = new int[m];
if (l_arr.Length > 1)
{
for (int i = 0; i < h; i++)
left[i] = l_arr[i];
for (int i = h, j = 0; i < l_arr.Length; i++, j++)
right[j] = l_arr[i];
MergeSort(left);
MergeSort(right);
Merge(left, right, l_arr);
}
}
private void Merge(int[] left, int[] right, int[] l_arr)
{
int arrIndex, leftIndex, rightIndex;
arrIndex = leftIndex = rightIndex = 0;
while (leftIndex < left.Length && rightIndex < right.Length)
{
if (left[leftIndex] < right[rightIndex])
l_arr[arrIndex++] = left[leftIndex++];
else
l_arr[arrIndex++] = right[rightIndex++];
}
while (leftIndex < left.Length)
l_arr[arrIndex++] = left[leftIndex++];
while (rightIndex < right.Length)
l_arr[arrIndex++] = right[rightIndex++];
ShowProgressBar(pictureBox1, arr1.Length);
}
#endregion
#region Show progress bar
int cnt = 1;
private void ShowProgressBar(object sender, int max)
{
PictureBox objProgressBar = (PictureBox)sender;
Bitmap objBM = new Bitmap(objProgressBar.Width, objProgressBar.Height);
Graphics objGraphic = Graphics.FromImage(objBM);
objGraphic.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
objGraphic.FillRectangle(Brushes.Violet, new Rectangle(0, 0, ((cnt++) * objProgressBar.Width) / max, objProgressBar.Height));
try { objProgressBar.Image = objBM; }
catch { }
Application.DoEvents();
}
#endregion
-------------------------
I've changed ShowProgressBar to this:
int cnt = 1;
private void ShowProgressBar(object sender, int max)
{
if (InvokeRequired)
{
Invoke(new MethodInvoker(delegate { ShowProgressBar(sender, max); }));
}
else
{
PictureBox objProgressBar = (PictureBox)sender;
Bitmap objBM = new Bitmap(objProgressBar.Width, objProgressBar.Height);
Graphics objGraphic = Graphics.FromImage(objBM);
objGraphic.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
objGraphic.FillRectangle(Brushes.Violet, new Rectangle(0, 0, ((cnt++) * objProgressBar.Width) / max, objProgressBar.Height));
objProgressBar.Image = objBM;
this.Text = (((cnt) * 100) / max).ToString() + '%';
}
}
------------------------
The QuickSort method:
#region Quick Sort
int pivotpoint;
void QuickSort(int low, int high, int[] l_arr)
{
pivotpoint=low;
if (high > low)
{
partition(low, high, l_arr);
QuickSort(low, pivotpoint - 1, l_arr);
QuickSort(pivotpoint + 1, high, l_arr);
}
}
void partition(int low, int high, int[] l_arr)
{
int i, j;
int tmp;
int pivotitem = l_arr[low];
j = low;
for (i = low + 1; i <= high; i++)
{
if (l_arr[i] < pivotitem)
{
j++;
tmp = l_arr[i];
l_arr[i] = l_arr[j];
l_arr[j] = tmp;
}
}
pivotpoint = j;
tmp = l_arr[low];
l_arr[low] = l_arr[pivotpoint];
l_arr[pivotpoint] = tmp;
if (InvokeRequired)
{
Invoke(new MethodInvoker(delegate { partition(low, high, l_arr); }));
}
else
{
ShowProgressBar(pictureBox2, arr2.Length);
}
}
#endregion