Skip to content

Instantly share code, notes, and snippets.

@SamuelTulach
Created August 24, 2018 17:25
Show Gist options
  • Save SamuelTulach/8ef6cfc80d7f2308065a6f255fdd6e4c to your computer and use it in GitHub Desktop.
Save SamuelTulach/8ef6cfc80d7f2308065a6f255fdd6e4c to your computer and use it in GitHub Desktop.
Program na testování rychlosti internetu a jeho stability po delší dobu
using System;
using System.ComponentModel;
using System.Diagnostics;
using System.IO;
using System.Net;
using System.Threading;
namespace SpeedtestLog
{
class MainClass
{
static WebClient webClient;
static Stopwatch sw = new Stopwatch();
public static int total = 0;
public static string url = "http://35.242.207.92/largefile.iso";
public static string totaldownloaded = "";
public static long downloaded = 0;
public static long lastdownloaded = 0;
public static void DownloadFile(string urlAddress, string location)
{
using (webClient = new WebClient())
{
webClient.DownloadFileCompleted += new AsyncCompletedEventHandler(Completed);
webClient.DownloadProgressChanged += new DownloadProgressChangedEventHandler(ProgressChanged);
Uri URL = new Uri(url);
sw.Start();
try
{
webClient.DownloadFileAsync(URL, location);
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
}
}
private static void ProgressChanged(object sender, DownloadProgressChangedEventArgs e)
{
totaldownloaded = string.Format("{0} MB's / {1} MB's",
(e.BytesReceived / 1024d / 1024d).ToString("0.00"),
(e.TotalBytesToReceive / 1024d / 1024d).ToString("0.00"));
downloaded = e.BytesReceived;
}
private static void Completed(object sender, AsyncCompletedEventArgs e)
{
sw.Reset();
if (e.Cancelled == true)
{
Console.WriteLine("Download has been canceled.");
}
else
{
Console.WriteLine("--> " + total.ToString() + " <--");
DownloadFile(url, "file.iso");
total++;
}
}
private static void TimerCallback(Object o)
{
long speedl = downloaded - lastdownloaded;
string speed = speedl.ToString();
lastdownloaded = downloaded;
Console.WriteLine(speed + " (" + totaldownloaded + ")");
File.AppendAllText("log.txt", speed + Environment.NewLine);
}
public static void Main(string[] args)
{
Timer t = new Timer(TimerCallback, null, 0, 1000);
DownloadFile(url, "file.iso");
Console.ReadKey();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment