From d14a218584ab8bacda85f452eba15b41b0085db4 Mon Sep 17 00:00:00 2001 From: Jake Howard Date: Wed, 25 Jun 2014 21:52:11 +0100 Subject: [PATCH] Added development testing projects to Repo --- Snippets/Cyclic USB Detection (Old Style).cs | 64 ++++++++++++++++++++ Snippets/Read XML from USB.cs | 36 +++++++++++ Snippets/background Worker.cs | 62 +++++++++++++++++++ 3 files changed, 162 insertions(+) create mode 100644 Snippets/Cyclic USB Detection (Old Style).cs create mode 100644 Snippets/Read XML from USB.cs create mode 100644 Snippets/background Worker.cs diff --git a/Snippets/Cyclic USB Detection (Old Style).cs b/Snippets/Cyclic USB Detection (Old Style).cs new file mode 100644 index 0000000..a84fe29 --- /dev/null +++ b/Snippets/Cyclic USB Detection (Old Style).cs @@ -0,0 +1,64 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using System.IO; + +namespace USB_Detection_and_XML_Reading +{ + class Program + { + static void Main(string[] args) + { + string[] foundDrives= cycleDrivesForFile(); + Console.WriteLine("Cycle Ended!"); + foreach (char drive in foundDrives) + { + Console.WriteLine("File found on drive " + drive + ":\\"); + } + Console.ReadKey(); + } + + + static bool fileExists(string driveLetter) + { + string path = driveLetter + "config.sbf"; + return File.Exists(path); + } + + + static string[] cycleDrivesForFile() + { + /* + char[] letters = "ABCDEFGHIJKLMNOPQRSTUVWXYZ".ToCharArray(); + char[] systemDrives = "C".ToCharArray(); //Scan will not look in these drives + int count = 0; + + foreach (char driveLetter in letters) + { + if (systemDrives.Contains(driveLetter)) + { + continue; + } + if (fileExists(driveLetter)) + { + foundDrives.Add(driveLetter); + } + } + return foundDrives.ToArray(); + */ + List foundDrives = new List(); + DriveInfo[] driveArray = DriveInfo.GetDrives(); + for (int i = 0; i < driveArray.Count(); i++) + { + if (fileExists(driveArray[i].Name)) + { + foundDrives.Add(driveArray[i].Name); + } + Console.WriteLine("Drive " + i + ": " + driveArray[i].Name); + } + return foundDrives.ToArray(); + } + } +} diff --git a/Snippets/Read XML from USB.cs b/Snippets/Read XML from USB.cs new file mode 100644 index 0000000..36858bb --- /dev/null +++ b/Snippets/Read XML from USB.cs @@ -0,0 +1,36 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using System.Threading; +using System.IO; + +namespace Read_USB_XML +{ + class Program + { + static void Main(string[] args) + { + DriveInfo[] drives = DriveInfo.GetDrives(); + for (int i = 0; i < drives.Count(); i++) + { + string driveName; + string driveLetter; + try //if the volume label is invalid (CD Drive in laptop), then the program crashes + { + driveName = drives[i].VolumeLabel; + driveLetter = drives[i].Name; + } catch { + continue; + } + if (driveName == "") + { + driveName = "Unnamed Volume"; + } + Console.WriteLine("Drive " + i + ": " + driveLetter + ". Drive Label: " + driveName); + } + Console.ReadKey(); + } + } +} diff --git a/Snippets/background Worker.cs b/Snippets/background Worker.cs new file mode 100644 index 0000000..32f84d2 --- /dev/null +++ b/Snippets/background Worker.cs @@ -0,0 +1,62 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using System.Threading; + +namespace Background_Worker_Testing +{ + class worker + { + public void do_Work() + { + while (_ThreadActive) + { + Console.WriteLine("Thread Processing with ID " + id); + System.Threading.Thread.Sleep(id * 755); + } + } + public void request_Stop() + { + _ThreadActive = false; + } + private volatile bool _ThreadActive = true; + public volatile int id; + } + + + class Program + { + public static int count1 = 0; + public static int count2 = 0; + static void Main(string[] args) + { + Console.WriteLine("Program Started."); + worker worker1 = new worker(); + worker worker2 = new worker(); + worker1.id = 1; + worker2.id = 2; + Console.WriteLine("Workers defined."); + Thread thread1 = new Thread(worker1.do_Work); + Thread thread2 = new Thread(worker2.do_Work); + Console.WriteLine("Threads Defined"); + thread1.Start(); + while (!thread1.IsAlive); + Console.WriteLine("Thread 1 has begun!"); + thread2.Start(); + while (!thread2.IsAlive); + Console.WriteLine("Thread 2 has begun!"); + + System.Threading.Thread.Sleep(5000); + Console.WriteLine("Stopping Threads..."); + worker1.request_Stop(); + while (thread1.IsAlive); + worker2.request_Stop(); + while (thread2.IsAlive); + Console.WriteLine("Threads Stopped."); + Console.ReadKey(); + + } + } +}