/// <summary> /// Extension for SmartCollections to resize themselves /// </summary> /// <returns>false if resizing is not possible, true if resizing was done</returns> public static bool Resize(this SmartCollection <int> c, int newSize) { int currentSize = c.Count; if (newSize >= 0 && currentSize != newSize) { int diff = newSize - currentSize; if (diff > 0) { // Add items var items = new List <int>(); for (int i = 0; i < diff; i++) { items.Add(new int()); } c.AddRange(items); return(true); } else { // Remove items c.DownsizeCollection(Math.Abs(diff)); return(true); } } return(false); }
async void NCSPRunThread() { // Open communication pipe for reading HPipe = new NamedPipeServer(@"\\.\pipe\NCSPPipe1", 0); HPipe.MessageReceived += hPipe_MessageReceived; // Use Task to ensure this thread waits for instruction to return var startPipeTask = Task.Factory.StartNew(() => { HPipe.Start(); }).FailFastOnException(); // Reset library settings Task resetTask = Task.Factory.StartNew(() => { NCSPLibReset(); }).FailFastOnException(); resetTask.Wait(); // Configure CSP Task setParamsTask = Task.Factory.StartNew(() => { NCSPSetParams(Model.ToParams()); }).FailFastOnException(); setParamsTask.Wait(); Task addCostsTask = Task.Factory.StartNew(() => { foreach (var c in Model.MinimumCosts) { NCSPAddMinCost(c); } }).FailFastOnException(); addCostsTask.Wait(); // Ugly, but the pipe server will block (listening) if we use wait/await Thread.Sleep(500); // Run CSP var error = await Task <int> .Factory.StartNew(() => { return(NCSPLibRun()); }).FailFastOnException(); if (error == 0) { if (NCSPGetSolutionFound()) { int numSolutions = NCSPGetAllResultsSize(); SmartCollection <ShotSolution> allSolutions = new SmartCollection <ShotSolution>(); for (int i = 0; i < numSolutions; i++) { ShotSolution s = new ShotSolution(); List <Shot> shots = new List <Shot>(); int numShots = NCSPGetResultSize(i); for (int j = 0; j < numShots; j++) { Shot shot = new Shot(); int noShotId = NCSPGetNoShotID(); SHOTINFO info = new SHOTINFO(); NCSPGetResultElement(i, j, out info); int numCharacters = NCSPGetNumCharactersInElement(info.ID); if (info.ID != noShotId) { if (numCharacters > 0) { List <String> characterNames = new List <String>(); for (int k = 0; k < numCharacters; k++) { var name = GetCharacterNameInShotByIndex(info.ID, k); characterNames.Add(name); } shot.Characters = new SmartCollection <string>(characterNames); } String fileLoc = GetFileLocation(info.ID); shot.FileLocation = fileLoc; shot.Info = info; shots.Add(shot); } } s.Shots = shots; s.Name = (i == 0) ? "Original" : "Variant " + i; allSolutions.Add(s); } Solution = allSolutions; MainCharacterName = GetMainCharacterName(); } else { // No solution found. Error message already displayed // the user via pipe comms } } else { // TODO: Provide well-defined error codes switch (error) { case 1: DebugOutput += "Pipe could not be initialised (try again)\n"; break; default: DebugOutput += "Error in running NCSP\n"; break; } } var stopPipeTask = await Task <int> .Factory.StartNew(() => { return(HPipe.StopServer()); }).FailFastOnException(); HPipe.MessageReceived -= hPipe_MessageReceived; RunButtonEnabled = true; }