void MainFunction() { ThreadHelperClass.SetText(this, lblStatusValue, "Loading sample images to memory..."); SourceImagesLoader(txtSampleImagesFolderLocation.Text); if (isSampleImageFolderEmpty) { ThreadHelperClass.SetText(this, lblStatus, "The selected folder has no images or the images is not supported"); } else { ThreadHelperClass.SetText(this, lblStatusValue, "Loading finished. " + PTAMSourceImages.Length + " images were loaded into memory."); } Thread.Sleep(100); ThreadHelperClass.SetText(this, lblStatusValue, "Preparing for sending..."); MMF mappedFile = new MMF(); mappedFile.CreateNewFile(PTAMModuleSourceImageFileName, 10000000); int position = 0; do { Stopwatch overallPerformance = new Stopwatch(); overallPerformance.Start(); // Create preview of loaded image ThreadHelperClass.SetImage(this, imgImagePreview, PTAMSourceImages[position]); ThreadHelperClass.SetText(this, lblStatusValue, "Now previewing image: " + fileNames[position]); position++; if (position == fileNames.Length) { position = 0; } #region Receiver threat receiverThread = new Thread(ResultReceiver); receiverThread.Start(); #endregion Receiver threat ThreadHelperClass.SetText(this, lblStatusValue, "Writing image to memory-mapped file"); #region Image converter // Convert image to array and save to tempImage Stopwatch imageConversionWatcher = new Stopwatch(); imageConversionWatcher.Start(); byte[] tempImage; using (var ms = new MemoryStream()) { PTAMSourceImages[position].Save(ms, PTAMSourceImages[position].RawFormat); tempImage = ms.ToArray(); } imageConversionWatcher.Stop(); testerImageConversionPerformance = imageConversionWatcher.ElapsedMilliseconds; #endregion Image converter #region Memory-Mapped File writing Stopwatch MMFWatcher = new Stopwatch(); MMFWatcher.Start(); // Write the image to memory-mapped file mappedFile.AddInformation(Convert.ToBase64String(tempImage)); MMFWatcher.Stop(); testerMemoryMappedFilePerformance = MMFWatcher.ElapsedMilliseconds; #endregion Memory-Mapped File writing ThreadHelperClass.SetText(this, lblStatusValue, "File writen. Sending notification to PTAM Module"); // Notify Kursor3D Module about the image SendReport(PTAMModuleNamedPipeSendingChannel); ThreadHelperClass.SetText(this, lblStatusValue, "Module connected. Now waiting for the module to complete the process"); // Wait for receiver thread to complete if (receiverThread.IsAlive) { receiverThread.Join(); } receiverThread = null; ThreadHelperClass.SetText(this, lblStatusValue, "Module has finished the work. Writing the received values"); #region UI setter #region Pose #region Position ThreadHelperClass.SetText(this, lblPositionXValue, receivedXPosition.ToString()); ThreadHelperClass.SetText(this, lblPositionYValue, receivedYPosition.ToString()); ThreadHelperClass.SetText(this, lblPositionZValue, receivedZPosition.ToString()); #endregion Position #region Orientation ThreadHelperClass.SetText(this, lblOrientationXValue, receivedXOrientation.ToString()); ThreadHelperClass.SetText(this, lblOrientationYValue, receivedYOrientation.ToString()); ThreadHelperClass.SetText(this, lblOrientationZValue, receivedZOrientation.ToString()); #endregion Orientation #endregion Pose #region Points ThreadHelperClass.SetText(this, lblTotalPointCreatedValue, totalPointsCreated.ToString()); #endregion Points overallPerformance.Stop(); testerOverallPerformance = overallPerformance.ElapsedMilliseconds; ThreadHelperClass.SetText(this, lblOverallPerformanceValue, testerOverallPerformance.ToString() + "ms"); ThreadHelperClass.SetText(this, lblMemoryMappedFilePerformanceValue, testerMemoryMappedFilePerformance.ToString() + "ms"); ThreadHelperClass.SetText(this, lblImageConversionPerformanceValue, testerImageConversionPerformance.ToString() + "ms"); ThreadHelperClass.SetText(this, lblPTAMModuleOverallPerformanceValue, PTAMModuleOverallPerformance.ToString() + "ms"); ThreadHelperClass.SetText(this, lblPTAMModuleTrackingPerformanceValue, PTAMTrackingPerformance.ToString() + "ms"); ThreadHelperClass.SetText(this, lblPTAMModuleMappingPerformanceValue, PTAMMappingPerformance.ToString() + "ms"); ThreadHelperClass.SetText(this, lblPTAMModuleImageConversionPerformanceValue, PTAMImageConversionPerformance.ToString() + "ms"); ThreadHelperClass.SetText(this, lblPTAMModuleMemoryMappedFileReadingPerformanceValue, PTAMMemoryMappedFileReadingPerformance.ToString() + "ms"); #endregion UI setter } while (!isExitRequested); }
void ModelerThread() { ThreadHelperClass.SetText(this, lblStatusValue, "Requesting the HUB resolution..."); bool isExitRequested = false; char receivedCode; NamedPipesServer imageNotifier = new NamedPipesServer(); MMF ImageData; Bitmap imageReceived; #region Screen Resolution Request int width; int height; // Connect for first time check for resolution used by main program NamedPipesServer server = new NamedPipesServer(); server.CreateNewServerPipe("res-pipe", NamedPipesServer.PipeDirection.DirectionInOut, NamedPipesServer.SendMode.MessageMode); server.WaitForConnection(); string resolution = server.ReadMessage(); // Separate screen width and height string[] res = resolution.Split('x'); width = Convert.ToInt32(res[0]); height = Convert.ToInt32(res[1]); string receivedResolution = "Connected client resolution: " + width + " x " + height; ThreadHelperClass.SetText(this, lblStatusValue, "HUB's resolution received"); ThreadHelperClass.SetText(this, lblScreenResolutionValue, receivedResolution); #endregion Screen Resolution Request bool isFirstTime = true; do { ThreadHelperClass.SetText(this, lblStatusValue, "Waiting new notification..."); try { // Memory-mapped file access server.CreateNewServerPipe("ImageNotifier", NamedPipesServer.PipeDirection.DirectionInOut, NamedPipesServer.SendMode.ByteMode); server.WaitForConnection(); if (server.ReadByte() == (char)'y') { ThreadHelperClass.SetText(this, lblStatusValue, "Receiving image..."); byte[] file = null; Console.WriteLine('y'); // Load image from Memory-mapped file and display as texture for model. MMF mappedFile = new MMF(); mappedFile.OpenExisting("MapTest"); file = Convert.FromBase64String(mappedFile.ReadContent(MMF.DataType.DataString)); using (var ms = new MemoryStream(file)) { imageReceived = new Bitmap(ms); } ThreadHelperClass.SetImage(this, imgTesterPreviewImage, imageReceived); } } catch (Exception err) { throw; } server.Disconnect(); server.ClosePipe(); //try //{ // imageNotifier.CreateNewServerPipe("test-pipe", NamedPipesServer.PipeDirection.DirectionInOut, NamedPipesServer.SendMode.ByteMode); // imageNotifier.WaitForConnection(); // receivedCode = (char)imageNotifier.ReadByte(); // imageNotifier.WaitForPipeDrain(); // if (receivedCode == 'x') // isExitRequested = true; // else // isExitRequested = false; // if (receivedCode == 'y') // { // ThreadHelperClass.SetText(this, lblStatusValue, "New notification. Receiving..."); // byte[] file = null; // // Load image from Memory-mapped file // MMF mappedFile = new MMF(); // mappedFile.OpenExisting("MapTest"); // file = Convert.FromBase64String(mappedFile.ReadContent(MMF.DataType.DataString)); // using (var ms = new MemoryStream(file)) // { // imageReceived = new Bitmap(ms); // } // ThreadHelperClass.SetImage(this, previewImage, imageReceived); // } //} //catch (Exception err) //{ // throw; //} //server.Disconnect(); //server.ClosePipe(); } while (!isExitRequested); }