Exemplo n.º 1
0
        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;
        }
Exemplo n.º 2
0
 public static extern void NCSPGetResultElement(int i, int j, out SHOTINFO info);