Multi Thread - probleme release
Bonjour,
J'ai fait un programme pour afficher une image dans une fenetre (IMG) et afficher dans une autre fenetre (PNT) les coordonées des points de l'image sur lesquels on aura double cliquer.
Tant que l'utilisateur n'a pas cliquer sur le bouton OK de la fenetre PNT, les deux fenetres restent ouvertes.
Lorsque l'utilisateur presse le bouton OK, les 2 fenetres se ferment (car je sors de la fonction ou elles etaient crees) et la liste des points est enregistrée dans un fichier texte.
Mon programme fonctionne tres bien en version Debug. Par contre en Release lorsque j'appuie sur le bouton OK, la fenetre contenant l'image reste ouverte.
Je pense que mon programme bloque dans la boucle while (thrtrc.IsAlive) mais je ne comprend pas pourquoi. Quelqu'un aurait-il une idée ?
(Merci d'avance)
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120
|
/// Listen if an event happens in p
/// </summary>
/// <param name="p"></param>
public void EventListener(PNT p)
{
PTOIT = p;
PTOIT.changed += new PointsOfInterest.ChgEventHandler(PTOIT_changed);
}
/// <summary>
/// Action to perform if an event has happened
/// </summary>
void PTOIT_changed(object sender, EventArgs e)
{
done = !done;
}
private PNT poi;
private void printImg(string filename, Parameters ap, string title)
{
trc = new IMG();
trc.Text = title;
object[] parameters = new object[] { filename, ap };
this.thrtrc = new Thread(new ParameterizedThreadStart(trcBegin));
this.thrtrc.Start(parameters);
}
private Thread thrtrc;
private IMG trc;
/// <summary>
/// Print the image
/// </summary>
private void trcBegin(object parameters)
{
object[] parameterArray = (object[])parameters;
string filename = (string)parameterArray[0];
Parameters ap = (Parameters)parameterArray[1];
trc.CreateImage(filename, ap);
// won't quit until done is true
// the value of done will change if an event happens in the other window
while (done == false)
{
}
}
private void GetPointsOfInterest(Parameters ap)
{
List<double> pointsList = new List<double>();
string filename = "\\Image.png";
string resultfolder = ap.resTracesFolderName + "\\InterestingPoints";
//Open in a new Thread an IMG window and print the Trace in filename with Zedgraph
//The thread stays alive
printImg(filename, ap, "Select Points");
//Open a PNT Form
poi = new PNT();
//The current class is listening if an event happens in poi
//to change the value of done
this.EventListener(poi);
//the poi class is listening if an events happens in the IMG trc
//to write in its own textbox
poi.EventListener(trc);
//Show the poi window
poi.Show();
//wait until the thread thrtrc is done
//i.e. until the button ok of poi is pressed
while (thrtrc.IsAlive)
{
Thread.Sleep(50);
Application.DoEvents();
}
filename = resultfolder + "\\Points.txt";
pointsList = trc.GetPoints();
sw = new StreamWriter(filename);
if (sw == null) return;
sw.WriteLine("Points : ");
foreach (double point in pointsList)
sw.WriteLine(point.ToString());
sw.WriteLine("");
sw.Close();
} |