Created
June 11, 2012 15:47
-
-
Save ielcoro/2910771 to your computer and use it in GitHub Desktop.
Actualizador ClickOnce
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/// <summary> | |
/// Inicia la monitorización de versiones | |
/// </summary> | |
private void MonitorizarVersiones() | |
{ | |
try | |
{ | |
if (ApplicationDeployment.IsNetworkDeployed) | |
{ | |
if (ComprobarVersion()) | |
{ | |
uxVersion.Visibility = BarItemVisibility.Always; | |
timerComprobarVersiones.Interval = Properties.Settings.Default.IntervaloMilisegundosCompracionVersiones; | |
timerComprobarVersiones.Tick += (s, e) => | |
{ | |
timerComprobarVersiones.Stop(); | |
ComprobarVersion(); | |
}; | |
timerComprobarVersiones.Start(); | |
} | |
} | |
} | |
catch (Exception ex) | |
{ | |
Program.Mensajes.MostrarMensaje(Mensajes.E012, ex); | |
} | |
} | |
/// <summary> | |
/// Comprueba la versión actual y si existe una versión nueva | |
/// </summary> | |
/// <returns>True o False</returns> | |
private bool ComprobarVersion() | |
{ | |
bool result = false; | |
try | |
{ | |
uxVersion.Caption = ApplicationDeployment.CurrentDeployment.CurrentVersion.ToString(); | |
if (ApplicationDeployment.CurrentDeployment.CheckForUpdate()) | |
{ | |
DescargarActualizacion(this.TokenCancelacion.Token); | |
} | |
else | |
timerComprobarVersiones.Start(); | |
result = true; | |
} | |
catch (Exception ex) | |
{ | |
Program.Log.Loguear(ex); | |
result = false; | |
} | |
return result; | |
} | |
private Task DescargarActualizacion(CancellationToken tokenCancelacion) | |
{ | |
Task result = null; | |
try | |
{ | |
var adaptadorTarea = new TaskCompletionSource<object>(); | |
tokenCancelacion.Register(() => ApplicationDeployment.CurrentDeployment.UpdateAsyncCancel()); | |
ApplicationDeployment.CurrentDeployment.UpdateCompleted += (s, e) => | |
{ | |
if (e.Error != null) | |
adaptadorTarea.SetException(e.Error); | |
else if (e.Cancelled) | |
adaptadorTarea.SetCanceled(); | |
else | |
{ | |
Task.Factory.StartNew(OnDescargarActualizacionTerminado, tokenCancelacion, TaskCreationOptions.None, this.Scheduler); | |
adaptadorTarea.SetResult(e.UserState); | |
} | |
}; | |
ApplicationDeployment.CurrentDeployment.UpdateProgressChanged += (s, e) => | |
{ | |
Task.Factory.StartNew(() => OnActualizarProgresoDescargaActualizacion(e.ProgressPercentage), tokenCancelacion, TaskCreationOptions.None, this.Scheduler); | |
}; | |
ApplicationDeployment.CurrentDeployment.UpdateAsync(); | |
result = adaptadorTarea.Task; | |
} | |
catch (Exception ex) | |
{ | |
Program.Log.Loguear(ex); | |
} | |
return result; | |
} | |
private void OnDescargarActualizacionTerminado() | |
{ | |
try | |
{ | |
uxActualizarProgreso.Visibility = BarItemVisibility.Never; | |
uxBarraActualizar.Visible = true; | |
} | |
catch (Exception ex) | |
{ | |
Program.Log.Loguear(ex); | |
} | |
} | |
private void OnActualizarProgresoDescargaActualizacion(int porcentaje) | |
{ | |
try | |
{ | |
uxActualizarProgreso.Visibility = BarItemVisibility.Always; | |
uxActualizarProgreso.EditValue = porcentaje; | |
} | |
catch (Exception ex) | |
{ | |
Program.Log.Loguear(ex); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment