Skip to content

Commit

Permalink
it works
Browse files Browse the repository at this point in the history
  • Loading branch information
Francisco Santos committed Mar 21, 2017
1 parent 5d2b5d2 commit 7b710fa
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 17 deletions.
2 changes: 1 addition & 1 deletion NetworkCardRestart/App.config
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,5 @@
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5.2" />
</startup>
<requestedExecutionLevel level="requireAdministrator" uiAccess="false" />
<!--<requestedExecutionLevel level="requireAdministrator" uiAccess="false" />-->
</configuration>
59 changes: 43 additions & 16 deletions NetworkCardRestart/Program.cs
Original file line number Diff line number Diff line change
@@ -1,28 +1,34 @@
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Management;
using System.Net;
using System.Net.NetworkInformation;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using ROOT.CIMV2.Win32;

namespace NetworkCardRestart
{
class Program
{
private const int MillisecondsTimeout = 3000;
private const int MillisecondsTimeoutDC = 5000;
private const int MillisecondsTimeoutC = 7000;

private const int TimesPing = 4;
private const int AttemptsToRestart = 20;

static void Main(string[] args)
{
while (!PingTest())
uint counter = 0;
while (!PingTest() && counter++ < AttemptsToRestart)
{
RestartAdapter();
}
Console.ReadLine();
if (counter == AttemptsToRestart)
{
Console.WriteLine("We have reached the maximum amount of tries.");
}
Console.WriteLine("Finishing.");
//Console.ReadLine();
}

private static void RestartAdapter()
Expand All @@ -45,9 +51,12 @@ private static void RestartAdapter()
if (adapter.AdapterType.Equals("Ethernet 802.3"))
{
adapter.Disable();
Console.WriteLine("Waiting " + MillisecondsTimeout / 1000 + " seconds to activate the card.");
Thread.Sleep(MillisecondsTimeout);
Console.WriteLine("Waiting " + MillisecondsTimeoutDC / 1000 + " seconds to activate the card.");
Thread.Sleep(MillisecondsTimeoutDC);
adapter.Enable();
Console.WriteLine("Waiting " + MillisecondsTimeoutC / 1000 +
" seconds in order to get the card ready.");
Thread.Sleep(MillisecondsTimeoutC);
}
});
t.Start();
Expand All @@ -68,18 +77,36 @@ private static bool PingTest()

Ping ping = new Ping();

PingReply pingStatus = ping.Send(IPAddress.Parse("google.com"));

if (pingStatus != null && pingStatus.Status == IPStatus.Success)
try
{
Console.WriteLine("We have Internet access.");
return true;
IPAddress ip = IPAddress.Parse("8.8.8.8");

for (var i = 0; i < TimesPing; ++i)
{
var reply = ping.Send(ip);
Console.WriteLine("Reply from {0} Status: {1} time:{2}ms",
reply.Address,
reply.Status,
reply.RoundtripTime);
if (reply.Status.ToString().Equals("Success"))
{
Console.WriteLine("We have Internet access.");
return true;
}
}
}
else
catch (Exception e)
{
Console.WriteLine("We don't have Internet access.");
//Console.WriteLine(e.Source);
Console.WriteLine(e.Message);
// Wait a little more
Console.WriteLine("Waiting a little longer.");
Thread.Sleep(MillisecondsTimeoutC);
return false;
}

Console.WriteLine("We don't have Internet access.");
return false;
}
}
}

0 comments on commit 7b710fa

Please sign in to comment.