Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Modify the name pipe initialization to run with threads (better performance) #12

Merged
merged 1 commit into from
Aug 15, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
79 changes: 62 additions & 17 deletions PipeViewer/Form1.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
Expand Down Expand Up @@ -152,7 +152,7 @@ private void InitializePipeListWithProcesses()
{
m_ProcessPIDsDictionary.Add(p.Id, p.ProcessName);
}

// initializePipeList();
}

Expand All @@ -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)
Expand All @@ -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;
Expand Down Expand Up @@ -337,7 +384,6 @@ private void addNamedPipeToDataGridView(string i_NamedPipe)
this.m_NamedPipesNumber += 1;
this.toolStripStatusLabelTotalNamedPipes.Text = "Total Named Pipes: " + this.m_NamedPipesNumber;


}
}

Expand Down Expand Up @@ -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;
}
}
Expand Down Expand Up @@ -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");
});
}


Expand All @@ -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();
}
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)
{
Expand Down Expand Up @@ -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);
Expand All @@ -1386,8 +1433,6 @@ private void contextMenuStripRightClickGridView_Closing(object sender, ToolStrip
}
}



private void copyCellToolStripMenuItem_Click(object sender, EventArgs e)
{
string copiedCell = "";
Expand Down