Ciao ragazzi, ho un problema che non riesco a capire da cosa dipenda.
In questo codice, non vi sto a spiegare cosa fa, Ho una sezione dove creo delle cartelle e tramite un thread inizio a scrivere su un file.
Viene letta una lista, che nel mio caso ha un solo elemento, il metodo può essere eseguito n volte.
In alcuni casi delle iterazioni mi ritrovo che la variabile: testListNumber, che al ripartire di
while (iterationNumber > 0)
viene resettata addirittura 2 volte. Venga modifica in corso d'opera, esattamente:
fm.CreateLogFile(constants.GetResultsFolder() + @"\Results_" + iterations + @"\" + TestList.ElementAt(testListNumber).Split('.')[0] + @"\Logs.asc");
//Load test inside the testEditor
//START TAKING CAN Logs
Thread.Sleep(2000);
new Thread(() => vector.TakeLogs(CancellationLogsToken, (constants.GetResultsFolder() + @"\Results_" + iterations + @"\" + TestList.ElementAt(testListNumber).Split('.')[0] + @"\Logs.asc"))).Start();
Nella creazione del file: TestList.ElementAt(testListNumber) il testlistnumber è 0.
Successivamente al "TakeLogs" me lo ritrovo magicamente ad 1 e ovviamente ho un'eccezione. Non sto capendo cosa possa essere.
Ho pensato che possa essere una questione di thread. Ma è proprio assurdo.
public void RunLoop(List<string> TestList, int loopNumber, CancellationToken token)
{
try
{
int iterations = 1;
this.TestList = TestList;
int messageNumber = 0;
int snapshotListIndex = 0;
//int endTestTime = 0;
//int testCounter = 1;
int testListNumber = 0;
TestEditorUserControl testEditor = null;
int ListLogsIndex = 0;
decimal iterationNumber = loopNumber;
CancellationTokenSource logsToken = new CancellationTokenSource();
CancellationToken CancellationLogsToken = logsToken.Token;
CancellationTokenSource tokenSource = new CancellationTokenSource();
CancellationToken CancellationToken = tokenSource.Token;
camera = Camera.Instance();
camera.StartCamera();
Thread.Sleep(1500);
while (iterationNumber > 0)
{
if (StopLoop) //Stop button pressed
{
UpdateProgressBar(0);
UpdateTestNameLabel(string.Empty);
UpdateCurrentStepLabel(string.Empty);
//TestNameLabel.Text = string.Empty;
//CurrentStepLabel.Text = string.Empty;
tokenSource.Cancel(); //KILL ALL THREADS AND LIST
logsToken.Cancel(); //CLOSING THE LOG FILE
if (taskList.Count > 0)
foreach (Thread task in taskList) { task.Abort(); }
taskList.Clear();
testListNumber = 0;
return;
}
testListNumber = 0;
foreach (string testName in TestList)
{
if (StopLoop) //Stop button pressed
{
UpdateProgressBar(0);
UpdateTestNameLabel(string.Empty);
UpdateCurrentStepLabel(string.Empty);
//TestNameLabel.Text = string.Empty;
//CurrentStepLabel.Text = string.Empty;
tokenSource.Cancel(); //KILL ALL THREADS AND LIST
logsToken.Cancel(); //CLOSING THE LOG FILE
if (taskList.Count > 0)
foreach (Thread task in taskList) { task.Abort(); }
taskList.Clear();
return;
}
UpdateProgressBar(0);
UpdateTestNameLabel(testName);
UpdateCurrentStepLabel(string.Empty);
tokenSource = new CancellationTokenSource();
CancellationToken = tokenSource.Token;
messageNumber = 0;
snapshotListIndex = 0;
//endTestTime = 0;
taskList.Clear();
testEditor = null;
ListLogsIndex = 0;
//iterationNumber = iterations;
if (!constants.ArduinoPortNumber.Equals(String.Empty))
{
arduino = Arduino.Instance();
}
if (!(Directory.Exists(constants.GetResultsFolder() + @"\Results_" + iterations)))
{
directory = Directory.CreateDirectory(constants.GetResultsFolder() + @"\Results_" + iterations);
}
if (!(Directory.Exists(constants.GetResultsFolder() + @"\Results_" + iterations + @"\" + TestList.ElementAt(testListNumber))))
{
directory = Directory.CreateDirectory(constants.GetResultsFolder() + @"\Results_" + iterations + @"\" + TestList.ElementAt(testListNumber).Split('.')[0]);
}
//Just wait some seconds until the folders are created
Thread.Sleep(2000);
fm.CreateLogFile(constants.GetResultsFolder() + @"\Results_" + iterations + @"\" + TestList.ElementAt(testListNumber).Split('.')[0] + @"\Logs.asc");
//Load test inside the testEditor
//START TAKING CAN Logs
Thread.Sleep(2000);
new Thread(() => vector.TakeLogs(CancellationLogsToken, (constants.GetResultsFolder() + @"\Results_" + iterations + @"\" + TestList.ElementAt(testListNumber).Split('.')[0] + @"\Logs.asc"))).Start();
LogsPathList.Add(constants.GetResultsFolder() + @"\Results_" + iterations + @"\" + TestList.ElementAt(testListNumber).Split('.')[0] + @"\Logs.asc");
//Load Test inside TestEditor
testEditor = ApplicationTabUserControl.Instance().ReadTest(constants.GetTestsFolder() + @"\" + TestList.ElementAt(testListNumber).Split('.')[0] + ".txt");
SetUpdatePercentage(testEditor.GetData().Count);
string currentFolder = constants.GetResultsFolder() + @"\Results_" + iterations + @"\" + TestList.ElementAt(testListNumber).Split('.')[0];
testListNumber++;
//Just a wait before GUI is updated and then start the test
foreach (TestListNode node in testEditor.TreeListView.Roots)
{
if (StopLoop) //Stop button pressed
{
UpdateProgressBar(0);
UpdateTestNameLabel(string.Empty);
UpdateCurrentStepLabel(string.Empty);
tokenSource.Cancel(); //KILL ALL THREADS AND LIST
logsToken.Cancel(); //CLOSING THE LOG FILE
if (taskList.Count > 0)
foreach (Thread task in taskList) { task.Abort(); }
taskList.Clear();
return;
}
UpdateCurrentStepLabel(node.StepNumber + "." + node.StepName + ": " + node.Value);
if (node.StepName.Contains("Wait"))
{
Thread.Sleep(Convert.ToInt32(node.Value.Split(new char[] { ' ' })[0])); //the time to wait
UpdateProgressBar(PercentageValue);
}
else if (node.StepName.Contains("Arduino"))
{
if (!arduino.IsPortOpened())
arduino.OpenPort();
arduino.ActivatePIN(node.Value.Split(new char[] { '-' })[0], Int32.Parse(node.Value.Split(new char[] { '-' })[1])); // Set teh command and the time until keep press
UpdateProgressBar(PercentageValue);
}
else if (node.StepName.Contains("Snapshot"))
{
camera.CaptureScreenshot(node.Value, @"C:\Users\galloda\source\repos\ATB_Tool\Snapshots");
UpdateProgressBar(PercentageValue);
}
else if (node.StepName.Contains("Check"))
{
TimestampList.Add(DateTime.Now.Hour + ":" + DateTime.Now.Minute + ":" + DateTime.Now.Second + ":" + DateTime.Now.Millisecond);
messageNumber++;
UpdateProgressBar(PercentageValue);
}
//send message on CAN network, with cycle time (using threads) or not
else if (node.StepName.Contains("Send"))
{
bool exists = false;
int counter = 0;
Message msgToSend = ApplicationTabUserControl.Instance().GetListMessageToSend().ElementAt(messageNumber);
// TO DELETE!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
msgToSend.CycleTime = 10;
//SECTION IN WHICH THE THREADS ARE CLOSED
var searchThread = taskList.Where(y => y != null && y.Name.Equals(msgToSend.Name)).FirstOrDefault();
if (searchThread != null)
{
searchThread.Abort();
//tokenSource.Cancel();
taskList.Remove(taskList.Where(y => y != null && y.Name.Equals(msgToSend.Name)).FirstOrDefault());
taskList.Add(new Thread(() => vector.SendCyclicMessage(msgToSend, CancellationToken)));
taskList.Last().Name = msgToSend.Name;
taskList.Last().Start();
}
else
{
taskList.Add(new Thread(() => vector.SendCyclicMessage(msgToSend, CancellationToken)));
taskList.Last().Name = msgToSend.Name;
taskList.Last().Start();
}
while (!taskList.ElementAt(taskList.Count - 1).IsAlive) ; //STOP THE PROGRAM UNTIL THE THREAD IS RUNNED
counter++;
messageNumber++;
UpdateProgressBar(PercentageValue);
Thread.Sleep(2000);
}
}
if (runningProgressBar.Value < 100) //If there is a pair number of step we set the maximum percentage value cause the round function is to fix
UpdateProgressBar(100);
tokenSource.Cancel(); //KILL ALL THREADS AND LIST
logsToken.Cancel(); //CLOSING THE LOG FILE
if (taskList.Count > 0)
foreach (Thread task in taskList) { task.Abort(); }
taskList.Clear();
//CHECK THE LOG FILE IF IT IS INSERTED AS COMMAND
//Wait just a second to be sure the log file has been closed
Thread.Sleep(1000);
foreach (TestListNode node in testEditor.TreeListView.Roots)
{
if (node.StepName.Contains("Check"))
{
string valueToMatch = string.Empty;
string[] words = node.Value.Split(new string[] { "(", ")", " " }, StringSplitOptions.None);
for (int i = 2; i < words.Length - 1; i++)
{
valueToMatch = valueToMatch + words[i];
}
string timestamp = TimestampList.ElementAt(ListLogsIndex);
fm.SearchMessageONCan(node.MessageName, valueToMatch, LogsPathList.ElementAt(ListLogsIndex), timestamp);
}
}
ListLogsIndex++;
ResultTableNode nodeResult = null;
if (TestPassed) //Add to the result list a row with test name and the result
{
nodeResult = new ResultTableNode(TestNameLabel.Text, "Passed", iterations.ToString());
}
else //Add to the result list a row with test name and the result
{
nodeResult = new ResultTableNode(TestNameLabel.Text, "Failed", iterations.ToString());
}
ResultsGrid.GetData().Add(nodeResult);
UpdateResultsList(ResultsGrid.GetData());
}
testListNumber = 0;
iterations++;
iterationNumber--;
}
tokenSource.Cancel(); //KILL ALL THREADS AND LIST
logsToken.Cancel(); //CLOSING THE LOG FILE
if (taskList.Count > 0)
{
foreach (Thread task in taskList) { task.Abort(); }
taskList.Clear();
}
}
catch (Exception ex)
{
MessageBox.Show("Error: " + ex.Message, "Error");
Environment.Exit(1);
}
finally
{
camera.CloseCamera();
//Closing the arduino port if it is opened
//if (arduino != null)
//{
// arduino.Reset();
// arduino.ClosePort();
//}
}
}