Hi I'm trying to adapt a code from as I started to write an application to read several mp3 headers from files and application was hanging up.
Before now, when I was pressing cancel button, application was simply doing nothing (no catching cancelAsync method), now it throws a run-time exception at that time.
This is the basic layout:
private readdirectories (DoWorkEventArgs e) {
BackgroundWorker bwg = sender as BackgroundWorker;
e.Result = leerCabecerasMP3(bwg);
if (bwg.CancellationPending) {
e.Cancel = true;
}
}
private string leerCabecerasMP3(BackgroundWorker bwg) {
while (!bwg.CancellationPending) {
readheader;
bwg.ReportProgress(progreso);
}
return "OK"
}
And the button handlers
private void button1_Click(object sender, EventArgs e) {
Boton_CancelarMP3.Enabled = true;
boton_buscarMP3.Enabled = false;
proceso.RunWorkerAsync();
}
void proceso_actualizaprogress(object sender, ProgressChangedEventArgs e) {
progressBar1.Value = e.ProgressPercentage;
progressBar1.Refresh();
}
void proceso_fin(object sender, RunWorkerCompletedEventArgs e)
do something;
}
private void Boton_CancelarMP3_Click(object sender, EventArgs e) {
proceso.CancelAsync();
}
For anyone interested below is the full code.
Before now, when I was pressing cancel button, application was simply doing nothing (no catching cancelAsync method), now it throws a run-time exception at that time.
This is the basic layout:
private readdirectories (DoWorkEventArgs e) {
BackgroundWorker bwg = sender as BackgroundWorker;
e.Result = leerCabecerasMP3(bwg);
if (bwg.CancellationPending) {
e.Cancel = true;
}
}
private string leerCabecerasMP3(BackgroundWorker bwg) {
while (!bwg.CancellationPending) {
readheader;
bwg.ReportProgress(progreso);
}
return "OK"
}
And the button handlers
private void button1_Click(object sender, EventArgs e) {
Boton_CancelarMP3.Enabled = true;
boton_buscarMP3.Enabled = false;
proceso.RunWorkerAsync();
}
void proceso_actualizaprogress(object sender, ProgressChangedEventArgs e) {
progressBar1.Value = e.ProgressPercentage;
progressBar1.Refresh();
}
void proceso_fin(object sender, RunWorkerCompletedEventArgs e)
do something;
}
private void Boton_CancelarMP3_Click(object sender, EventArgs e) {
proceso.CancelAsync();
}
For anyone interested below is the full code.