I have another question about threadsaftey according to memory access.
What about the synchronisation of objects passed to BackgroundWorker RunWorkerAsync(object) and returning to Result?
Lets say we have class (simplified)
We assume that threads will not access the same objects at the same time.
public class WorkerArgs
{
// Do we have to sync this with lock, since bgWorker will access them?
public string FileName {get;}
public object Any {get;}
public WorkerArgs (string fileName, object any)
{
FileName = fileName;
Any = any;
}
}
...
private void worker_DoWork(object sender, DoWorkEventArgs e)
{
WorkerArgs args = (WorkerArgs)e.Arguments
string fileName = args.FileName;
MemoryStream ms = new MemoryStream();
while ( ... )
{
.. args.Any ....
}
// Do we have to box ms and put lock arround?
e.Result = ms;
}
private void worker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
... = (MemoryStream)e.Result ;
}
So it is still not clear to me when to Lock or Fencing when simultaneous access of two or more threads at the same time is barred. But threads can accessing the same object afterwards.
Thank you very much !