Answer the question
In order to leave comments, you need to log in
Parallel.ForEach not running on new thread?
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);
});
});
}
Answer the question
In order to leave comments, you need to log in
The code
this.Text = string.Format("current thread #{0}",
Thread.CurrentThread.ManagedThreadId);
var text = string.Format("current thread #{0}", Thread.CurrentThread.ManagedThreadId);
this.Invoke(()=> Text = text);
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question