I wrote the following code to perform genetic algorithm that loops 10 times and each time I call the "FitnessFunction" function, I use a background worker to move an object on the topology and once it reach a specific point then I cancel that background worker and back to the "Genetic_Algorithm" function...
Unfortunately, I got the following error when I click on "MOVE-AUV-GAAPS" button which call the "Genetic_Algorithm" method:
Cross-thread operation not valid: Control 'topology' accessed from a thread other than the thread it was created on...
and point to:
while(true)
which is in the "AUVbackGWgenetic_DoWork" function.
"Genetic_Algorithm" function :
<pre> static int geneticIteration;
static BackgroundWorker AUVbackGWgenetic;
public void Genetic_Algorithm(int[][,] population)
{
double[,] FitnessValue = new double[6, 2];
int[] RouletteWheel = new int[6];
int round=0;
for (int geneticIteration = 0; geneticIteration < 10; geneticIteration++)
{
round = geneticIteration + 1;
writer = new StreamWriter("C:/Users/Welcome/Desktop/listOfChromosomesForAllRounds.txt", true);
writer.WriteLine("----------------------------------------------------------------------");
Chromosomes(population,round);
FitnessFunction(population);
writer.Close();
}
}
"FitnessFunction" function :
public void FitnessFunction(int[][,] population)
{
double[,] FitnessValue = new double[6, 2];
AUVbackGWgenetic = new BackgroundWorker();
AUVbackGWgenetic.DoWork += new DoWorkEventHandler (AUVbackGWgenetic_DoWork);
AUVbackGWgenetic.RunWorkerAsync(geneticIteration);
}
"AUVbackGWgenetic_DoWork" function :
private void AUVbackGWgenetic_DoWork(object sender, DoWorkEventArgs e)
{
Random rnd = new Random();
SoundPlayer player = new SoundPlayer("C:/Users/Welcome/Desktop/project/GAAPS/Sonar_pings.wav");
auv.auvX = rnd.Next(200, 325);
auv.auvY = rnd.Next(200, 325);
while (true)
{
Thread.Sleep(300);
if (AUVbackGWgenetic.CancellationPending)
{
e.Cancel = true;
return;
}
if (auv.auvY == 200)
{
if (auv.queue1.Count != 0)
{
Thread.Sleep(1000);
foreach (Packet packet in auv.queue1)
{
auv.SendToSink(packet);
}
auv.queue1.Clear();
topology.CreateGraphics().DrawString("Received Aggrigated Data from AUV # " + auv.auvID, new Font("Times New Roman", 7.0f), Brushes.Red, sink.sinkX - 48, sink.sinkY - 15);
topology.CreateGraphics().DrawLine(dash, auv.auvX, auv.auvY, sink.sinkX + 20, sink.sinkY + 45);
player.Play();
Thread.Sleep(3000);
player.Stop();
}
}
if (forward)
{
auv.auvX += 3;
auv.auvY += 3;
if (auv.auvY >= 326)
{
backward = true;
forward = false;
countNumOfPaths++;
}
}
if (backward)
{
auv.auvX -= 3;
auv.auvY -= 3;
if (auv.auvY >= 200)
{
backward = false;
forward = true;
countNumOfPaths++;
}
if (countNumOfPaths >= 2)
{
AUVbackGWgenetic.CancelAsync();
}
}
iSetupDisplay = 0;
if (iSetupDisplay != -1)
{
iSetupDisplay += 10;
if (iSetupDisplay >= topology.Width)
iSetupDisplay = -1;
topology.Refresh();
}
}
}
The following link contains a picture for my WF App :
Here
What I have tried:
I need to run the background worker each time I call "FitnessFunction" function" which is responsible to stop it under a specific condition.