public MainForm()
{
InitializeComponent();
this.Text = string.Format("current thread #{0}",
Thread.CurrentThread.ManagedThreadId);
}
private void button1_Click(object sender, EventArgs e)
{
ProcessFiles();
}
private void ProcessFiles()
{
string[] files = Directory.GetFiles(@"F:\Troelsen\asd", "*.jpg", SearchOption.AllDirectories);
string newDir = @"F:\Troelsen\asd\Modified";
Directory.CreateDirectory(newDir);
Parallel.ForEach(files, currentFile =>
{
//Thread.CurrentThread.Name = "Parallel.ForEach";
string filename = Path.GetFileName(currentFile);
using (Bitmap bitmap = new Bitmap(currentFile))
{
bitmap.RotateFlip(RotateFlipType.Rotate180FlipNone);
if (filename != null) bitmap.Save(Path.Combine(newDir, filename));
}
this.Invoke((Action) delegate
{
this.Text = string.Format("current thread #{0}",
Thread.CurrentThread.ManagedThreadId);
});
});
}
Нажимаю на button1 и приложение намертво зависает, даже после завершения обработки всех .jpg.
Код взят с книги Andrew Troelsen "Pro C# 5.0 and the .NET 4.5 Framework"