bouton bloquer pendant traitement
Bonsoir,
Je viens vers vous car je n'arrive pas à faire en sorte que mon bouton d'arrêt d'un while soit accessible. J'ai testé sur deux threads, mais aucun changement.
Voici mon code:
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
| static class ArreterListener implements ActionListener {
// Redéfinition de la méthode actionPerformed()
public void actionPerformed(ActionEvent e) {
traitement traiter = new traitement();
Thread thread = new Thread (traiter);
thread.start();
traiter.cancel();
System.out.println("Arret de la synchro");
}
}
static class BoutonListener implements ActionListener {
// Redéfinition de la méthode actionPerformed()
public void actionPerformed(ActionEvent e) {
if(!jtf.getText().isEmpty())
{
traitement traiter1 = new traitement();
Thread thread1 = new Thread (traiter1);
thread1.start();
traiter1.run(jtf.getText());
}
}
} |
Et mon code dans un thread faisant le traitement :
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
| public class traitement extends Thread{
private volatile boolean cancelled;
static StorageManager stMan;
public void run(String chemin) {
// TODO Auto-generated method stub
while (!cancelled)
{
try {
getCoureurs(chemin);
} catch (ClassNotFoundException | SQLException | IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
try
{
Thread.sleep (2000);
}
catch (InterruptedException exception){}
}
}
public void cancel()
{
cancelled = true;
} |
Merci d'avance de votre aide.
Cordialement,
Vins86