From df050e6370167e97cacbac9801f10f17448ce703 Mon Sep 17 00:00:00 2001 From: amirnachman Date: Tue, 15 Aug 2023 09:25:35 +0300 Subject: [PATCH] Added new feature - Button to show named pipe connection status --- PipeViewer/Form1.cs | 79 +++++++++++++++++++++++++++++++++++---------- 1 file changed, 62 insertions(+), 17 deletions(-) diff --git a/PipeViewer/Form1.cs b/PipeViewer/Form1.cs index c32f11f..4629d50 100644 --- a/PipeViewer/Form1.cs +++ b/PipeViewer/Form1.cs @@ -118,7 +118,7 @@ public Form1() //dummyLoopRowsForDebug(); //Task.Run(() => dummyLoopRowsForDebug()); Task.Run(() => InitializePipeListWithProcesses()); - + //Task.Run(() => initializePipeListOrig()); //Thread t = new Thread(new ThreadStart(InitializePipeListWithProcesses)); //t.Start(); } @@ -152,7 +152,7 @@ private void InitializePipeListWithProcesses() { m_ProcessPIDsDictionary.Add(p.Id, p.ProcessName); } - + // initializePipeList(); } @@ -167,7 +167,8 @@ private void InitalizeColumnsHeaderDictionary() } } - private void initializePipeList() + // # version 1 + private void initializePipeListOrig() { String[] listOfPipes = System.IO.Directory.GetFiles(@"\\.\pipe\"); foreach (var namedPipe in listOfPipes) @@ -176,6 +177,52 @@ private void initializePipeList() } } + // # version 2 + //private void initializePipeListNew() + //{ + // String[] listOfPipes = System.IO.Directory.GetFiles(@"\\.\pipe\"); + + // // ONLY FOR TEST + // int numberOfPipesToSelect = 1000; + // string[] selectedPipes = new string[Math.Min(numberOfPipesToSelect, listOfPipes.Length)]; + // Array.Copy(listOfPipes, selectedPipes, selectedPipes.Length); + // // ONLY FOR TEST + + // int PartsSize = 30; + // for (int i = 0; i < Math.Ceiling((double)listOfPipes.Length / PartsSize); i++) + // { + // int StartIndex = PartsSize * i; + // ThreadPool.QueueUserWorkItem(state => + // { + // initializeSomePipes(listOfPipes, StartIndex, PartsSize); + // }); + // } + //} + + //private void initializeSomePipes(String[] listOfPipes, int StartIndex, int PipesAmount) + //{ + // int i = StartIndex; + // int Count = 0; + // while (i < listOfPipes.Length && Count < PipesAmount) + // { + // var namedPipe = listOfPipes[i]; + // addNamedPipeToDataGridView(namedPipe); + // i++; + // Count++; + // } + //} + + // # version 3 + private void initializePipeList() + { + string[] listOfPipes = System.IO.Directory.GetFiles(@"\\.\pipe\"); + + Parallel.ForEach(listOfPipes, namedPipe => + { + addNamedPipeToDataGridView(namedPipe); + }); + } + int CountSelectedRows(DataGridView dataGridView) { int count = 0; @@ -337,7 +384,6 @@ private void addNamedPipeToDataGridView(string i_NamedPipe) this.m_NamedPipesNumber += 1; this.toolStripStatusLabelTotalNamedPipes.Text = "Total Named Pipes: " + this.m_NamedPipesNumber; - } } @@ -427,16 +473,16 @@ private void toolStripButtonGrid_Click(object sender, EventArgs e) if (!m_IsGridButtonPressed) { toolStripButtonGrid.Image = global::PipeViewer.Properties.Resources.grid; - this.toolStripButtonGrid.Text = "Show Grid"; + this.toolStripButtonGrid.Text = "Hide Grid"; m_IsGridButtonPressed = true; this.dataGridView1.AdvancedCellBorderStyle.All = DataGridViewAdvancedCellBorderStyle.None; } else { - this.toolStripButtonGrid.Text = "Hide Grid"; toolStripButtonGrid.Image = global::PipeViewer.Properties.Resources.grid_disable; m_IsGridButtonPressed = false; + this.toolStripButtonGrid.Text = "Show Grid"; this.dataGridView1.AdvancedCellBorderStyle.All = DataGridViewAdvancedCellBorderStyle.Single; } } @@ -515,7 +561,15 @@ private void toolStripButtonRefresh_Click(object sender, EventArgs e) dataGridView1.Rows.Clear(); dataGridView1.Refresh(); m_NamedPipesNumber = 0; - Task.Run(() => initializePipeList()); + + Task.Run(() => { + //Stopwatch stopwatch = new Stopwatch(); // Create a stopwatch to measure time + //stopwatch.Start(); // Start the stopwatch + initializePipeList(); + //stopwatch.Stop(); // Stop the stopwatch when the task completes + //TimeSpan elapsedTime = stopwatch.Elapsed; + //MessageBox.Show($"Task completed in {elapsedTime.TotalMilliseconds} milliseconds", "Task Completed"); + }); } @@ -525,7 +579,7 @@ private void openFindWindow() { m_FormFindWindow = new FormSearch(); m_FormFindWindow.searchForMatch += new FormSearch.searchEventHandler(FindWindow_searchForMatch); - m_FormFindWindow.FormClosed += findWindow_FormClosed; + m_FormFindWindow.FormClosed += findWindow_FormClosed; m_IsFindFormOpen = true; m_FormFindWindow.Show(); } @@ -1046,11 +1100,6 @@ private void dataGridView1_CellMouseClick(object sender, DataGridViewCellMouseEv ToolStripItem[] items = createNewToolStripMenuItem(e.RowIndex, e.ColumnIndex); } contextMenuStripRightClickGridView.Show(Cursor.Position.X, Cursor.Position.Y); - - - - - } } private ToolStripItem[] createNewToolStripMenuItem(int rowIndex, int columnIndex) @@ -1207,7 +1256,6 @@ private void importToolStripMenuItem_Click(object sender, EventArgs e) } } - // Taken from https://stackoverflow.com/a/26259909/2153777 private void exportDataGridViewToCSV(string filename) { @@ -1366,7 +1414,6 @@ private void CheckIfShouldBeColored(DataGridViewRow row, int permissionColumnInd } } - private void includeToolStripMenuItem_Click(object sender, EventArgs e) { //ListViewItem item = new ListViewItem(m_RightClickCellContent); @@ -1386,8 +1433,6 @@ private void contextMenuStripRightClickGridView_Closing(object sender, ToolStrip } } - - private void copyCellToolStripMenuItem_Click(object sender, EventArgs e) { string copiedCell = "";