protected override void AddDogActionsToVault() { int numActions = _numActions; int spinDelay = _rgen.Value.Next(1, 2500); TimeStampSource.Calibrate(); Thread.SpinWait(spinDelay); while (numActions-- > 0) { { //using the basicvault's locked resource object is easy: get or set. //we don't have to worry about copying the value out of the vault -- because it is vault-safe, //the value is effectively a deep copy (and the string member is immutable and sealed). //The copy cannot be used to affect the value stored in the vault after the lock is released. //Remember, if you are using an ATOMIC vault, SpinLock is a busy wait //If you are using a BASIC vault, SpinLock is the same as Lock //Regardless, will throw TimeoutException if lock not obtained within vault's default timeout. //you can also use an explicit positive timespan, a cancellation token, or both via overloads. using var lck = _recordVault.SpinLock(); lck.Value = new DogActionRecord($"Dog named {Name} performed an action."); } //give another thread a chance. //Thread.Yield() also works. //You can also not include this, but, if this is an atomic vault //with a busy wait, you probably want to do something to yield here //or you may timeout threads competing for access on a system without a lot //of available execution cores. Unnecessary, though perhaps sometimes desirable, if //using read-write vault or monitor vault .... though in those cases, Thread.Yield() is //probably the better option if you want to make sure give other threads get an event //shot of having a go at the lock. Thread.Sleep(TimeSpan.FromMilliseconds(1)); } }
static void Main() { string tsMsg = TimeStampSource.IsHighPrecision ? "This demonstration has HIGH PRECISION timestamps." : "This demonstration has LOW PRECISION timestamps."; Console.WriteLine("Beginning quick start demo. " + tsMsg); TimeStampSource.Calibrate(); DateTime demoStart = TimeStampSource.Now; DemonstrateBasicVault(); Console.WriteLine(); Console.WriteLine(); DemonstrateMutableResourceVault(); Console.WriteLine(); Console.WriteLine(); Console.WriteLine("Begin ReadWriteVault demo."); string res = ReadWriteVaultDemo.RunDemo(TimeSpan.FromDays(7.5)); FileInfo fi = new FileInfo(ReadWriteDemoVaultOutputFilePath); { using var sw = fi.CreateText(); sw.Write(res); } Console.WriteLine($"ReadWriteVault demo completed and log written to [{fi.FullName}]."); Console.WriteLine(); Console.WriteLine(); DateTime demoEnd = TimeStampSource.Now; Console.WriteLine("All three demos completed ok."); Console.WriteLine($"Elapsed milliseconds: [{(demoEnd - demoStart).TotalMilliseconds:F3}]."); }
static void Main(string[] args) { CgTimeStampSource.SupplyAlternateProvider(HpTimeStampProvider.CreateInstance()); TimeStampSource.Calibrate(); var gameFactories = new ClortonGameFactorySource(); (bool gotFileOk, string errorGettingFile, FileInfo outputFile, int numGames, VaultType varietyOfVault) = GetOutputFile(args); GameFactory = varietyOfVault == VaultType.Basic ? gameFactories.BasicVaultGameFactory : gameFactories.CustomVaultGameFactory; if (numGames == 1) { Console.WriteLine("Type of vault selected: [" + varietyOfVault + "]."); } Debug.Assert(gotFileOk == (outputFile != null)); if (gotFileOk) { if (numGames > 1) { try { PlayMultipleClortonGames(numGames, outputFile); } catch (Exception ex) { Console.Error.WriteLine(ex.ToString()); Console.Error.WriteLine("Unexpected error initializing game. Terminating."); Environment.Exit(-1); } } else { try { PlayClortonGame(outputFile); } catch (Exception ex) { Console.Error.WriteLine(ex.ToString()); Console.Error.WriteLine("Unexpected error initializing game. Terminating."); Environment.Exit(-1); } } } else { Console.Error.WriteLine("There was a problem with the output file. Additional info: [" + errorGettingFile + "]."); } }
static void Main(string[] args) { CgTimeStampSource.SupplyAlternateProvider(HpTimeStampProvider.CreateInstance()); TimeStampSource.Calibrate(); (bool gotFileOk, string errorInfo, FileInfo outputFile, int numGames, TimeSpan timeout) = GetOutputFile(args); Debug.Assert((outputFile != null) == gotFileOk); if (gotFileOk) { Console.WriteLine($"Going to play {numGames} CafeBabe games, each with a time limit of {timeout.TotalSeconds:F5} seconds. Results will be written to file {outputFile?.FullName ?? "UNSPECIFIED"} "); if (numGames > 1) { try { PlayMultipleCafeBabeGames(numGames, outputFile, timeout); } catch (Exception ex) { Console.Error.WriteLine(ex.ToString()); Console.Error.WriteLine("Unexpected error initializing game. Terminating."); Environment.Exit(-1); } } else { try { PlayCafeBabeGame(outputFile, timeout); } catch (Exception e) { Console.Error.WriteLine(e.ToString()); Console.Error.WriteLine("Unexpected error initializing game. Terminating."); Environment.Exit(-1); } } } else { Console.Error.WriteLine( "There was a problem parsing the parameters passed hereto." + $"{(!string.IsNullOrEmpty(errorInfo) ? (" " + errorInfo) : string.Empty)}"); } }
public Task Execute(string source, ObservableCollection <string> output, bool addMonthToFolderName, bool addYearToFolderName, TimeStampSource timeStampSource, bool commit) { output.Clear(); output.Add($"{DateTime.Now} Starting day-splitting"); output.Add($"{DateTime.Now} Fetching source files"); var sourceFiles = Directory.GetFiles(source, "*.*", SearchOption.TopDirectoryOnly); output.Add($"{DateTime.Now} Found {sourceFiles.Length} source files"); var movedFiles = 0; var filesWithoutTimeStamp = 0; var sb = new StringBuilder(); foreach (var sourceFile in sourceFiles) { var takenTime = timeStampSource switch { TimeStampSource.MetaData => _timeStampBuilder.BuildFromMetaData(sourceFile), TimeStampSource.FileName => _timeStampBuilder.BuildFromFileName(sourceFile), TimeStampSource.OperatingSystem => null, _ => null }; if (takenTime.HasValue) { var targetFile = DetermineTargetFile(sourceFile, takenTime.Value, addMonthToFolderName, addYearToFolderName); if (commit) { var folder = Path.GetDirectoryName(targetFile); if (!Directory.Exists(folder)) { Directory.CreateDirectory(folder); } } sb.Clear(); sb.AppendLine($"{DateTime.Now} Found time: {takenTime}"); sb.AppendLine($"Source: {sourceFile}"); sb.AppendLine($"Target: {targetFile}"); output.Add(sb.ToString()); if (commit) { File.Move(sourceFile, targetFile); output.Add($"{DateTime.Now} Moving {sourceFile} to {targetFile}"); } movedFiles += 1; } else { filesWithoutTimeStamp += 1; } } sb.Clear(); sb.AppendLine($"{DateTime.Now} Finished day-splitting"); sb.AppendLine($"Moved {movedFiles} from {sourceFiles.Length}"); sb.AppendLine($"{filesWithoutTimeStamp} files without a timestamp"); output.Add(sb.ToString()); return(Task.CompletedTask); }