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:
Et mon code dans un thread faisant le traitement :
Code : Sélectionner tout - Visualiser dans une fenêtre à part
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()); } } }
Merci d'avance de votre aide.
Code : Sélectionner tout - Visualiser dans une fenêtre à part
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; }
Cordialement,
Vins86
Partager