// Searching for all green square position.
List<List<List<List<int>>>> matchingSquares = new();
List<int> middleGreenSquare = new();
for (int cycle = 0; cycle < greenSquares.Count; cycle++)
{
middleGreenSquare = greenSquares[cycle];
matchingSquares.Add(new List<List<List<int>>>
{
new List<List<int>>(),
new List<List<int>>(),
new List<List<int>>(),
new List<List<int>>()
});
for (int i = 0; i < greenSquares.Count; i++)
{
if (middleGreenSquare[0] == greenSquares[i][2] &&
middleGreenSquare[1] == greenSquares[i][3]) // Up side
{
matchingSquares[cycle][0].Add(greenSquares[i]);
}
else if (middleGreenSquare[2] == greenSquares[i][0] &&
middleGreenSquare[3] == greenSquares[i][1]) // Down side
{
matchingSquares[cycle][1].Add(greenSquares[i]);
}
else if (middleGreenSquare[0] == greenSquares[i][1] &&
middleGreenSquare[2] == greenSquares[i][3]) // Left Side
{
matchingSquares[cycle][2].Add(greenSquares[i]);
}
else if (middleGreenSquare[1] == greenSquares[i][0] &&
middleGreenSquare[3] == greenSquares[i][2]) // Right Side
{
matchingSquares[cycle][3].Add(greenSquares[i]);
}
}
}
// Remove bad combination ( there may not be at least one pair for the middle square).
matchingSquares.RemoveAll(middleSquare => middleSquare.Exists(sideSquare=>
sideSquare.Count == 0));
// Finding groups of green squares where the sum of the square numbers is 66.
// Removing groups that have repeated square numbers.
// This leaves groups with unique numbers and satisfying the green condition.
List<List<List<int>>> groupsGreenSquares = new();
List<List<int>>? upSquares, downSquares, leftSquares, rightSquares;
List<int> usingSquares = new();
const int sumOfRightSqaureCombination = 66; // 1 + 2 + ... + 11
int squaresSum, uniqueSquaresCount;
foreach (var middleSquare in matchingSquares)
{
upSquares = middleSquare[0];
downSquares = middleSquare[1];
leftSquares = middleSquare[2];
rightSquares = middleSquare[3];
foreach(var upSquare in upSquares)
{
foreach(var downSquare in downSquares)
{
foreach(var leftSquare in leftSquares)
{
foreach(var rightSquare in rightSquares)
{
squaresSum = upSquare.Sum() + downSquare.Sum() + leftSquare[0] +
leftSquare[2] + rightSquare[1] + rightSquare[3];
if (squaresSum == sumOfRightSqaureCombination)
{
usingSquares.Clear();
usingSquares.AddRange(new List<int>
{
upSquare[0], upSquare[1], upSquare[2], upSquare[3],
downSquare[0], downSquare[1], downSquare[2], downSquare[3],
leftSquare[0], leftSquare[2],
rightSquare[1], rightSquare[3]
});
uniqueSquaresCount = usingSquares.Distinct().ToList().Count;
if (uniqueSquaresCount == usingSquares.Count)
{
groupsGreenSquares.Add(new List<List<int>>
{
upSquare,
leftSquare,
rightSquare,
downSquare
});
}
}
}
}
}
}
}
// Checking the yellow condition for three corners.
groupsGreenSquares.RemoveAll(groupGreenSquares =>
!DoCornersFitToYellowCondition(new List<List<int>>
{
squares[groupGreenSquares[0][0]],
squares[groupGreenSquares[0][1]],
squares[groupGreenSquares[1][0]],
squares[groupGreenSquares[1][1]],
squares[groupGreenSquares[2][0]],
squares[groupGreenSquares[2][1]],
squares[groupGreenSquares[1][2]],
squares[groupGreenSquares[1][3]],
squares[groupGreenSquares[2][2]],
squares[groupGreenSquares[2][3]],
squares[groupGreenSquares[3][2]],
squares[groupGreenSquares[3][3]]
}));
static bool DoCornersFitToYellowCondition(List<List<int>> squares)
{
int leftUpCorner = squares[0][2] + squares[2][1] + squares[3][0];
int rightUpCorner = squares[1][3] + squares[4][1] + squares[5][0];
int leftDownCorner = squares[6][3] + squares[7][2] + squares[10][0];
int rightDonwCorer = squares[8][3] + squares[9][2] + squares[11][1];
return leftUpCorner < 10 && rightUpCorner < 10 && leftDownCorner < 10 &&
rightDonwCorer < 10;
}
//Controls.Remove(_projectsListView);
то вызовется это ControlRemoved += (o, e) => Dispose();
и ислючений не будет.Close();
и будет ислючение. _projectsListView.ItemActivate += (o, e) =>
{
ListViewItem item = _projectsListView.SelectedItems[0];
string filePath = item.SubItems[2].Text;
string fileName = item.Name;
if (!File.Exists(filePath))
{
if (MessageBox.Show($"{fileName} не существует. Убрать проект из отрытых недавно?",
"Внимание!", MessageBoxButtons.YesNo) == DialogResult.Yes)
{
RemoveItemFromRecentOpenedProjectsFile(item);
}
return;
}
FilePath = filePath;
UpdateItemTimeInFile(filePath: RecentOpenedProjectsFilePath, item);
Close();
//Controls.Remove(_projectsListView);
};
Rows.AddRange(rows)
и теперь все добавляет даже в 1 потоке весьма быстро, и даже теперь с ParallerFor
теперь добавление даже 100 строк ощущается мгновенно. Но всерано посмотрю статью может еще что-то можно оптимизировать!tablesForSavingData = await Task.Run(()=> Serialization.DownloadTable(filePath));
await Task.Run(() =>
{
tablesForSavingData = Serialization.DownloadTable(filePath);
if (tablesForSavingData.Count != 0)
{
int i = 1;
foreach (var table in tablesForSavingData)
{
tablesForViewData.Add(
table.ToDataGridView("table" + (i++).ToString()));
}
}
else tablesForViewData.Add(defaultTable);
MakeTableActive(1);
Invoke(() =>
{
foreach (var table in tablesForViewData)
Controls.Add(table);
});
});
new Thread(() =>
{
tablesForSavingData = Serialization.DownloadTable(filePath);
if (tablesForSavingData.Count != 0)
{
int i = 1;
foreach (var table in tablesForSavingData)
{
tablesForViewData.Add(
table.ToDataGridView("table" + (i++).ToString()));
}
}
else tablesForViewData.Add(defaultTable);
MakeTableActive(1);
Invoke(() =>
{
foreach (var table in tablesForViewData)
Controls.Add(table);
});
}).Start();