I am developing an application that uses a progressbar to show the user any development in the program. The program I am writing uses a progressbar to show the user how many packets of data have been sent in the course of execution. I have created an event in the underlying library that triggers once another packet has been transmitted. Here are the event lines I have set up:
public delegate void ChangedEventHandler(object sender, EventArgs e);
public event ChangedEventHandler PercentageUpdate;
protected internal virtual void OnPercentageChanged(EventArgs e)
{
if (PercentageUpdate != null) PercentageUpdate(this, e);
}
Note that the above code is set up in the underlying library. I have the event triggered in this library like so:
//reinitializing is extraneous, as Receive() calls are overwriting
try
{
fsa.rnd = fsa.TransferSocket.Receive(fsa.File_Buffer, 0, FSArgs.BlockSize, SocketFlags.None);
}
catch (Exception e)
{
fsa.Dispose();
throw new GeneralNetworkingException("FileSocket receive() failed!", e);
}
fsa.tot += Convert.ToInt64(fsa.rnd);
fsa.TransferPercentage = (fsa.tot/fsa.FileSize) * 100;
fsa.OnPercentageChanged(EventArgs.Empty); //throw event for form
Finally, in the application, I have the event initialized like so:
public delegate void UpdatePerc(int index);
f_list[f_list.Count-1].fsa.PercentageUpdate += (sender, e) => filesocket_percentage_updated(sender, e, sel_index);
private void filesocket_percentage_updated(object sender, EventArgs e, int index)
{
UpdateP(index);
}
private void UpdateP(int index)
{
if (progressBar1.InvokeRequired)
{
this.BeginInvoke(new UpdatePerc(UpdateP), new object[] { index });
}
else
{
if (sel_index == index) //then redraw progressbar
{
if (x_list[index] > 1)
{
y_list[index]++;
if (((int)(y_list[index] % x_list[index])) == 0)
{
z_list[index]++;
progressBar1.Increment(1);
progressBar1.Update();
}
}
else
{
z_list[index]++;
progressBar1.Increment(1);
progressBar1.Update();
richTextBox1.Text += (z_list[index]+ '\n');
}
}
else
{
if (x_list[index] > 1)
{
y_list[index]++;
if (((int)(y_list[index] % x_list[index])) == 0)
{
z_list[index]++;
}
}
else
{
z_list[index]++;
}
}
}
}
As you can see I am trying to invoke the progressbar to update on a parallel thread. However, nothing happens at all. Any help would be appreciated!
Thanks.