I wrote a program to copy a .doc
template, add some text to it, then save and close it. I have to do this many times, and saving and closing a word document is slow. I decided to use multi-threading, but I'm a noob at this stuff and just wrote what came to me. Any feedback would be greatly appreciated (on any aspect, not just the multi-threading).
Here is my code for a task:
public Task
{
public string Value;
public bool HasRun;
public Task(string value)
{
Value = value;
HasRun = false;
}
}
I create a list of Tasks that is a class level variable, accessible by all threads:
Tasks = db.table.select(a => new Task(a.value)).ToList();
Next, I run the following in each thread:
while(tasks.Any(a => a.HasRun == false))
{
var value = "";
lock(tasks)
{
var task = tasks.Where(a => a.HasRun == false).First();
value = task.Value;
task.HasRun = true;
}
CreateNewDocumentAppendValueAndSave(value);
}
x == false
, you can use!x
. It's more clear in my opinion. \$\endgroup\$