im newbie in C#, want to make a small app that checks the disks and in case of write timeout or exceptions (disk disconnection or something) do the computer restart.
This code seems to be working normally when i run it – it simply writes to some txt file date every one second, but when i offline the disk and online it again it firstly goes correct and then stop working normally.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
using System.Threading;
using System.Timers;
using System.Collections;
public class Example
{
public static int failedCounter = 0;
public static int errorRestartThreshold = 5;
const int delayInMilliseconds = 1000;
const int diskThresholdGb = 20;
const string writeDirectoryName = "cdiskmonitorCheck";
const string writeFileName = "cdiskmonitorCheck.txt";
const string fullErrorDirectoryName = "c:\cdiskmonitorCheck";
const string fullErrorFileName = fullErrorDirectoryName +"\"+ "cdiskmonitorError.log";
public bool exceptionFlag = false;
public static void Main()
{
Example e = new Example();
e.OnStart();
}
public void OnStart()
{
//Thread t = new Thread(Listener);
foreach (var drive in DriveInfo.GetDrives())
{
if (drive.IsReady == true)
{
double totalSpace = drive.TotalSize;
int totalSpaceinGB = (int)(totalSpace / 1024 / 1024 / 1024);
if (totalSpaceinGB > diskThresholdGb)
{
string diskName = drive.Name;
Console.WriteLine(diskName);
if (diskName == "D:\")
{
Console.WriteLine("we start");
Thread t = new Thread(Listener);
t.Start(diskName);
}
}
}
}
Console.ReadLine();
}
private void Listener(object diskLetter)
{
Thread thread = null;
//diskLetter = "fff:";
string fullWriteFolderPath = diskLetter +writeDirectoryName;
string fullWriteFilePath = fullWriteFolderPath + "\" + writeFileName;
while (true)
{
var task = Task.Run(() =>
{
exceptionFlag = false;
DateTime start = DateTime.Now;
thread = Thread.CurrentThread;
try
{
//create directory
if (!Directory.Exists(fullWriteFolderPath))
{
Directory.CreateDirectory(fullWriteFolderPath);
}
Console.WriteLine(fullWriteFilePath);
Console.WriteLine(start.ToString("MM/dd/yyyy hh:mm:ss.fff"));
using (StreamWriter sw = new StreamWriter(fullWriteFilePath))
{
sw.WriteLine(start.ToString("MM/dd/yyyy hh:mm:ss.fff"));
}
failedCounter = 0;
}
catch (Exception ex)
{
exceptionFlag = true;
//break;
Console.WriteLine(ex.ToString());
}
DateTime finish = DateTime.Now;
System.TimeSpan diff = finish - start;
int diffInMilliseconds = (int)diff.TotalMilliseconds;
//we wait for sometime to be 1 second
if (diffInMilliseconds < delayInMilliseconds)
{
Task.Delay((delayInMilliseconds - diffInMilliseconds)).Wait();
//Console.WriteLine((delayInMilliseconds - diffInMilliseconds));
}
});
//Check if task is finished.
bool isCompletedSuccessfully = task.Wait(TimeSpan.FromMilliseconds(1200));
//Console.WriteLine(isCompletedSuccessfully);
DateTime current = DateTime.Now;
if (!Directory.Exists(fullErrorDirectoryName))
{
Directory.CreateDirectory(fullErrorDirectoryName);
}
if (exceptionFlag)
{
current = DateTime.Now;
thread.Abort(true);
failedCounter++;
Console.WriteLine("Exception writting to file");
using (StreamWriter sw = File.AppendText(fullErrorFileName))
{
sw.WriteLine(current.ToString("MM/dd/yyyy hh:mm:ss.fff") + " exception, counter:" + failedCounter.ToString());
}
}
else
{
if (!isCompletedSuccessfully)
{
current = DateTime.Now;
failedCounter++;
Console.WriteLine("Timeoutted writting to file");
thread.Abort(true);
using (StreamWriter sw = File.AppendText(fullErrorFileName))
{
sw.WriteLine(current.ToString("MM/dd/yyyy hh:mm:ss.fff") + " timeoutted, counter:" + failedCounter.ToString());
}
}
else
{
failedCounter = 0;
Console.WriteLine("Task completed during period");
}
}
Console.WriteLine(failedCounter);
if (failedCounter >= errorRestartThreshold)
{
current = DateTime.Now;
using (StreamWriter sw = File.AppendText(fullErrorFileName))
{
sw.WriteLine(current.ToString("MM/dd/yyyy hh:mm:ss.fff") + " we restart server., counter:" + failedCounter.ToString());
}
}
}
}
}
working example
For example when i disconnect the disk D and then in few moments connect it back it still cant write to it and generates wrong result
i seaching for the answer for 2 weeks already, can anyone knows what need to be corrected? Thank you so much!
Юрий XXX is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.