lang:csharp:thread
Ceci est une ancienne révision du document !
Table des matières
Instruction lock
lock
permet d'empêcher deux threads d'exécuter un code entouré de l'objet verrouillé. Attention, lock
distingue uniquement des threads différents. Un lock(x)
dans un lock(x)
dans un même thread ne pose pas de problème, le deuxième lock
n'étant pas bloquant.
Blocage inter-threads
Sans valeur de retour
public static void readControlText(Control varControl) { if (varControl.InvokeRequired) { varControl.Invoke((Action)delegate {readControlText(varControl);})); } else { // Faire ce qu'il faut. } }
Avec valeur de retour
public static string readControlText(Control varControl) { if (varControl.InvokeRequired) { return (string)varControl.Invoke( new Func<String>(() => readControlText(varControl)) ); } else { // Renvoyer ce qu'il faut. return ""; } }
Création des threads
Thread classic avec lambda expression
gistfile1.cs Archive du 23/11/2018
Thread t = new Thread(() => { // Action à faire }); t.Start();
ou directement :
new Thread(() => { // Action à faire }).Start();
Thread se répétant
date = new System.Threading.Timer( (state) => { // Faire ce qu'on veut. }, this, 0, 1000); // Toutes les 1000 millisecondes.
lang/csharp/thread.1542976615.txt.gz · Dernière modification : 2018/11/23 13:36 de root