M
Melina
Hi,
How can i invoke a function in a mutli-threading environment with .NET 2.0 ?
For example, assuming each thread (in a thread polling application...) needs
to invoke the writeLog function from his createDocument function:
private void createDocument() {
writeLog("Test");
}
private void writeLog(string data) {
StreamWriter sw = new StreamWriter(@"c:\temp\test.txt", true);
sw.WriteLine(data);
sw.Flush();
sw.Close();
}
This code won't run properly because of simultaneous access to writeLog from
the different threads.
I know i can add a Lock into the writeLog function, but i would prefer
invoke delegates.
On a UI control like a TextBox, i can call the Invoke function with the
delegate. But how to do this with a function ?
Many thanks for helping.
Melina.
How can i invoke a function in a mutli-threading environment with .NET 2.0 ?
For example, assuming each thread (in a thread polling application...) needs
to invoke the writeLog function from his createDocument function:
private void createDocument() {
writeLog("Test");
}
private void writeLog(string data) {
StreamWriter sw = new StreamWriter(@"c:\temp\test.txt", true);
sw.WriteLine(data);
sw.Flush();
sw.Close();
}
This code won't run properly because of simultaneous access to writeLog from
the different threads.
I know i can add a Lock into the writeLog function, but i would prefer
invoke delegates.
On a UI control like a TextBox, i can call the Invoke function with the
delegate. But how to do this with a function ?
Many thanks for helping.
Melina.