You are on page 1of 3

private void SetData(ConfigData data)

{
bool save = false;

this.Dispatcher.Invoke(DispatcherPriority.Normal, (Action)(() =>


{
if (data.Medias != null)
{
SetCredential(data.UriCredential);
UpdateMedia(data);
save = true;
if (NotifyUpdateAction != null) NotifyUpdateAction(UpdateEnu
m.Medias);
}
if (data.SetariComenziLcd.Setari != null)
{
SetCredential(data.UriCredential);
UpdateSetari(data);
save = true;
if (NotifyUpdateAction != null) NotifyUpdateAction(UpdateEnu
m.Setari);
}
if (data.SetariComenziLcd.ComenziLcd != null)
{
UpdateComenzi(data);
save = true;
if (NotifyUpdateAction != null) NotifyUpdateAction(UpdateEnu
m.ComenziLcd);
}

}));

if (save)
{
if (data.Medias != null) lastDataReceive.Medias = data.Medias;
if (data.SetariComenziLcd.Setari != null) lastDataReceive.Setari
ComenziLcd.Setari = data.SetariComenziLcd.Setari;
SaveConfig(lastDataReceive);
}

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using IWTVnew.MediaDisplay.Core;
using System.Net;
using System.Configuration;
using System.Windows.Threading;

namespace IWTVnew.MediaDisplay.Concret.Other
{
public class ServerDownload : AServerDownload<Media>
{

private IList<Media> mediasToDownload = new List<Media>();


private WebClient webClient = null;
private DispatcherTimer timer = new DispatcherTimer();
public ServerDownload()
{
int secondsInterval = int.Parse( ConfigurationSettings.AppSettings[
"SecondsRetryDownloadMedia"] );
timer.Interval = TimeSpan.FromSeconds( secondsInterval );
timer.Tick += new EventHandler(timer_Tick);
}
void timer_Tick(object sender, EventArgs e)
{
timer.Stop();
lock (mediasToDownload)
{
if (mediasToDownload.Count > 0 && webClient != null)
{
WebDownload(mediasToDownload[0]);
}
}
}
internal protected override void DownloadMedia2(Media media)
{

lock (mediasToDownload)
{
mediasToDownload.Add(media);
if (webClient == null)
{

WebDownload(media);
}
}
}

private void WebDownload(Media media)


{
webClient = new WebClient();
webClient.DownloadFileCompleted += DownloadFileCompleted;
if (WebProxyIwtv.IsProxyEnabled) webClient.Proxy = WebProxyIwtv.WebP
roxy;
if (WebProxyIwtv.IsNetworkCredentialEnabled) webClient.Credentials =
WebProxyIwtv.NetworkCredential;
webClient.DownloadFileAsync(media.UriServer, media.UriClient, media)
;
}

private void DownloadFileCompleted(object sender, System.ComponentModel.


AsyncCompletedEventArgs e)
{
if (e.Error != null)
{
timer.Start();
return;
}
Media media = e.UserState as Media;
lock (mediasToDownload)
{
if (e.Cancelled)
{
webClient = null;
return;
}
mediasToDownload.Remove(media);
if (mediasToDownload.Count > 0)
{
WebDownload(mediasToDownload[0]);
}
else webClient = null;
}
base.FinishDownload(media);

}
public override void CancelDownload()
{
lock (mediasToDownload)
{
if (webClient!= null)webClient.CancelAsync();
mediasToDownload.Clear();
webClient = null;
}
}
}
}

You might also like