Foros del Web » Programación para mayores de 30 ;) » .NET »

backgroundWorker1 C#

Estas en el tema de backgroundWorker1 C# en el foro de .NET en Foros del Web. Hola, tengo un metodo que lo pueden ver abajo.. este cuando se llama tarda un buen tiempo en hacer toda su tarea entonces deseo implementarle ...
  #1 (permalink)  
Antiguo 19/02/2008, 14:49
 
Fecha de Ingreso: septiembre-2006
Ubicación: Culiacán Sinaloa México
Mensajes: 5
Antigüedad: 17 años, 7 meses
Puntos: 1
Pregunta backgroundWorker1 C#

Hola, tengo un metodo que lo pueden ver abajo.. este cuando se llama tarda un buen tiempo en hacer toda su tarea entonces deseo implementarle un progresbar para que valla cargando de la mano al trabajo que esta realizando. estube tratando con backgroundWorker1 pero la verdad no pude hecharlo andar.

saludos!

private void Cortar()
{
int i = 0;
CortarArchivo Objeto = new CortarArchivo();
Objeto.PatchArchivo = this.txtPatchCortar.Text;
Objeto.NumeroPartes = byte.Parse(this.txtNumPartes.Text);
FileInfo fileInfo = new FileInfo(this.txtPatchCortar.Text);
string Aux = fileInfo.Length.ToString();
Objeto.TotalBytes = int.Parse(Aux) ;

string[] buffer = Objeto.Corte();

while (i < Objeto.NumeroPartes)
{
using (StreamWriter sw = new StreamWriter(this.txtPatchGuardarC.Text + txtNombreCortar.Text + i+".jr",true, Encoding.Default))
{
sw.Write(buffer[i]);
i++;
}
}
}
  #2 (permalink)  
Antiguo 19/02/2008, 16:35
Avatar de tomerqueves  
Fecha de Ingreso: marzo-2005
Ubicación: algeciras (cadiz)
Mensajes: 200
Antigüedad: 19 años, 1 mes
Puntos: 7
Re: backgroundWorker1 C#

mira thread o algo asi, hablo de memoria.
no sé si te servira. pero se supone que los threads
o com se escriba :S
sirven para liberar en hilos bloques y así otro puede
ir en segundo plano mientra por encima algo menos
pesado está en primer plano.


Un saludo.
__________________
A todos los moderadores y admiinistradores. Si algun día me banean, por favor devolverme la carita de mi avatar
  #3 (permalink)  
Antiguo 19/02/2008, 20:40
Avatar de Peterpay
Colaborador
 
Fecha de Ingreso: septiembre-2007
Ubicación: San Francisco, United States
Mensajes: 3.858
Antigüedad: 16 años, 8 meses
Puntos: 87
Re: backgroundWorker1 C#

Un pequeño ejemplo

protected int ComputeComplexCalculation(int x)
{
System.Threading.Thread.Sleep(5000);
return x * x;
}
private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
{
int arg = (int)e.Argument;
backgroundWorker1.ReportProgress(25);
e.Result = ComputeComplexCalculation(arg);
backgroundWorker1.ReportProgress(50);

BackgroundWorker worker = sender as BackgroundWorker;
if (worker.CancellationPending)
{
e.Cancel = true;
}
}

private void backgroundWorker1_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
backgroundWorker1.ReportProgress(100);
string msg = "";
if (e.Error != null)
{
msg = e.Error.Message;
}
else if (e.Cancelled)
{
msg = "Canceled";
}
else
{
msg = e.Result.ToString();
}
MessageBox.Show(msg);
BtnStartAsync.Enabled = true;
BtnCancelAsync.Enabled = false;
}

private void backgroundWorker1_ProgressChanged(object sender, ProgressChangedEventArgs e)
{
this.Text = e.ProgressPercentage.ToString();
progressBar1.Value = e.ProgressPercentage;
}

private void BtnStartAsync_Click(object sender, EventArgs e)
{
BtnStartAsync.Enabled = false;
BtnCancelAsync.Enabled = true;
backgroundWorker1.RunWorkerAsync(50);
//int res=ComputeComplexCalculation(50);
//MessageBox.Show(res.ToString());
}

Espero te sirva para resolver tu problematica.

Saludos
peter
  #4 (permalink)  
Antiguo 19/02/2008, 22:15
 
Fecha de Ingreso: septiembre-2006
Ubicación: Culiacán Sinaloa México
Mensajes: 5
Antigüedad: 17 años, 7 meses
Puntos: 1
Re: backgroundWorker1 C#

Cita:
Iniciado por Peterpay Ver Mensaje
Un pequeño ejemplo

protected int ComputeComplexCalculation(int x)
{
System.Threading.Thread.Sleep(5000);
return x * x;
}
private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
{
int arg = (int)e.Argument;
backgroundWorker1.ReportProgress(25);
e.Result = ComputeComplexCalculation(arg);
backgroundWorker1.ReportProgress(50);

BackgroundWorker worker = sender as BackgroundWorker;
if (worker.CancellationPending)
{
e.Cancel = true;
}
}

private void backgroundWorker1_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
backgroundWorker1.ReportProgress(100);
string msg = "";
if (e.Error != null)
{
msg = e.Error.Message;
}
else if (e.Cancelled)
{
msg = "Canceled";
}
else
{
msg = e.Result.ToString();
}
MessageBox.Show(msg);
BtnStartAsync.Enabled = true;
BtnCancelAsync.Enabled = false;
}

private void backgroundWorker1_ProgressChanged(object sender, ProgressChangedEventArgs e)
{
this.Text = e.ProgressPercentage.ToString();
progressBar1.Value = e.ProgressPercentage;
}

private void BtnStartAsync_Click(object sender, EventArgs e)
{
BtnStartAsync.Enabled = false;
BtnCancelAsync.Enabled = true;
backgroundWorker1.RunWorkerAsync(50);
//int res=ComputeComplexCalculation(50);
//MessageBox.Show(res.ToString());
}

Espero te sirva para resolver tu problematica.

Saludos
peter
Gracias peter, estube probando y me arroja una exepcion que dice:

"Este BackgroundWorker indica que no notifica el proceso. Modifique WorkerReporstProgress para que indique que si notifica el progreso."

Que estare haciendo mal?
  #5 (permalink)  
Antiguo 19/02/2008, 22:18
Avatar de Peterpay
Colaborador
 
Fecha de Ingreso: septiembre-2007
Ubicación: San Francisco, United States
Mensajes: 3.858
Antigüedad: 16 años, 8 meses
Puntos: 87
Re: backgroundWorker1 C#

Cambia esa propiedad a true, busca con intellisense en ese objeto en particular.

bw.ReportsPrgress=true;

saludos

peter
  #6 (permalink)  
Antiguo 19/02/2008, 23:47
 
Fecha de Ingreso: septiembre-2006
Ubicación: Culiacán Sinaloa México
Mensajes: 5
Antigüedad: 17 años, 7 meses
Puntos: 1
Re: backgroundWorker1 C#

Cita:
Iniciado por Peterpay Ver Mensaje
Cambia esa propiedad a true, busca con intellisense en ese objeto en particular.

bw.ReportsPrgress=true;

saludos

peter

Muchas gracias, ya logre lo que queria ^^
Atención: Estás leyendo un tema que no tiene actividad desde hace más de 6 MESES, te recomendamos abrir un Nuevo tema en lugar de responder al actual.
Respuesta




La zona horaria es GMT -6. Ahora son las 21:43.