Problème d'interblocage de threads
Salut a tous, j'ai un problème d'interblocage. Voici j'ai une classe chargé de parcourir des répertoires et/ou partage de fichiers, je fournit a cette classe une liste des chemins a parser, puis je lance la fonction StarTscan() qui lance une autre fonction ScanDirectory() en utilisant un pool de thread les fichiers trouvés sont poussé dans une List<string> Files. Ma classe contient un timer qui execute une fonction toute les deux secondes, cette fonction lit Files et c'est la que sa pose problème, un blocage ce produit entre le pool de threads et le timber. J'ai essayé pas mal de chose mais rien de fonctionne.
Voici donc 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 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 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296
|
using System;
using System.AddIn.Hosting;
using System.Collections.Generic;
using System.IO;
using System.Threading;
using System.Timers;
using Common;
using Host.View;
using IndexationContract;
namespace SystemCore
{
public class ShareExplorer
{
#region 'Configuration'
/// <summary>
/// Liste des partages de fichiers ou répertoires à exploirer
/// </summary>
public List<string> shareToScan { get; set; }
/// <summary>
/// Liste des répertoires ou fichiers exclu
/// </summary>
public List<string> Excludes { get; set; }
/// <summary>
/// Liste des fichiers trouvés
/// </summary>
private List<string> Files = new List<string>();
/// <summary>
/// Liste des répertoires
/// </summary>
private List<string> Directories = new List<string>();
/// <summary>
/// Un pool de thread très simple
/// </summary>
private NaiveThreadPool NTpool = new NaiveThreadPool();
/// <summary>
/// Instance de la bibliothèque de fonctions commune
/// </summary>
private CommonFunctions cfunctions = new CommonFunctions();
/// <summary>
/// Objet pour la synchronisation
/// </summary>
private ReaderWriterLockSlim _lock = new ReaderWriterLockSlim();
/// <summary>
/// Permet de s'assurer qu'une seule lecture
/// de la file d'attente peut être faite à la fois
/// </summary>
private bool _isScavenging = false;
/// <summary>
/// Timer permettant de parcourir la file d'attente
/// </summary>
private System.Timers.Timer _timer { get; set; }
public ShareExplorer ()
{
_timer = new System.Timers.Timer();
_timer.Elapsed += timer_Elapsed;
_timer.Interval = 2000;
_timer.Start();
}
#endregion
#region 'Timer'
/// <summary>
/// Fonction attaché au timer
/// elle récupère les chemins contenus dans <![CDATA[Files<string>]]>
/// et ajoute un thread de traitement dans le pool de thread
/// </summary>
public void timer_Elapsed(object sender, ElapsedEventArgs e)
{
if (!_isScavenging)
{
_isScavenging = true;
_lock.ExitUpgradeableReadLock();
try
{
List<string> ItemsToDelete = new List<string>();
Files.ForEach(
delegate(string path)
{
if (!string.IsNullOrEmpty(path))
{
Console.WriteLine(path);
if (!Excludes.Contains(path))
{
Console.WriteLine("Ajout d'un thread dans le pool");
NTpool.Add(new Thread(new ParameterizedThreadStart(new AddInLoader().LoadAddIn)), new object[] { path, 1, 1 });
}
ItemsToDelete.Add(path);
}
});
if (ItemsToDelete.Count > 0)
{
_lock.EnterWriteLock();
try
{
ItemsToDelete.ForEach(
delegate(string path)
{
Files.Remove(path);
});
}
finally
{
ItemsToDelete.Clear();
_lock.ExitWriteLock();
}
}
}
finally
{
_lock.ExitUpgradeableReadLock();
}
_isScavenging = false;
}
}
#endregion
#region 'Functions'
/// <summary>
/// Démarre la fonction d'exploration
/// </summary>
public void StarTscan()
{
try
{
shareToScan.ForEach(
delegate(string path)
{
ThreadPool.QueueUserWorkItem(new WaitCallback(ScanDirectory), path);
});
}
catch (Exception e)
{
Console.WriteLine(e);
}
}
/// <summary>
/// Fonction principal
/// </summary>
/// <param name="arg">Chemin du répertoire ou partage</param>
private void ScanDirectory(object arg)
{
try
{
// Share path
string share = Convert.ToString(arg);
// Faut-il un Addin pour ce genre de partage ?
AddInToken ProtocolToken = SharedObject._protocol.GetAddInToken(null, share, 2);
if (ProtocolToken == null)
{
if (!Directory.Exists(share))
throw new Exception("Le répertoire ou le partage de fichier n'existe pas");
else
ListDir(share);
}
else
{
// Activation de l'Addin
AddinsProtocolViewHost addinInstance = ProtocolToken.Activate<AddinsProtocolViewHost>(AddInSecurityLevel.FullTrust, "Proto");
// Appel de l'AddIn
foreach (IndexationData Data in addinInstance.Explore(share))
if (Data == null)
continue;
else
IndexerBuffer.Add(Data);
// On ferme l'AddIn
AddInController.GetAddInController(addinInstance).Shutdown();
}
}
catch (Exception e)
{
Console.WriteLine(e);
}
}
#endregion
#region 'Scan'
/// <summary>
/// Fait le liste des répertoires et lance la fonction Scan()
/// </summary>
/// <param name="arg">Chemin du répertoire</param>
private bool ListDir(string arg)
{
string[] files = Directory.GetDirectories(arg);
for (int i = 0; i < files.Length; i++)
this.Scan(files[i]);
Directories.ForEach(
delegate(string share)
{
this.Scan(share);
});
return true;
}
/*
/// <summary>
/// List directories
/// </summary>
private void ListDir()
{
Directories.ForEach(
delegate(string share)
{
this.Scan(share);
});
}*/
/// <summary>
/// Liste tout les dossiers et
/// fichiers
/// </summary>
/// <param name="dir">path du répertoire</param>
private void Scan(string dir)
{
if (!Directory.Exists(dir))
return;
if (!cfunctions.ChechDirectoryAttributes(dir))
return;
string[] files = Directory.GetFileSystemEntries(dir);
for (int i = 0; i < files.Length; i++)
{
_lock.EnterWriteLock();
try
{
if ((File.GetAttributes(files[i]) & FileAttributes.Directory) == FileAttributes.Directory)
Directories.Add(files[i]);
else
if (!Files.Contains(files[i]))
Files.Add(files[i]);
}
finally
{
_lock.ExitWriteLock();
}
}
}
// Un peu de ménage
~ShareExplorer()
{
_timer.Close();
_timer.Dispose();
Files.Clear();
Excludes.Clear();
shareToScan.Clear();
Directories.Clear();
}
#endregion
}
} |
Merci de votre aide !!!!