Thread Safety - WinForms

  • Thread starter Thread starter GroZZleR
  • Start date Start date
G

GroZZleR

Hey all,

I'm threading newbie, I've never attempted it before. I've got a
button that when clicked starts up a thread to process some files.
This keeps my GUI responsive and gets the tasks done.

Here's an example of what I'm doing:
====================================
private void button1_Click(object sender, System.EventArgs e)
{
if(_searchThread != null && _searchThread.IsAlive)
_searchThread.Suspend();

_searchThread = new Thread(new ThreadStart(TestThread));

_searchThread.IsBackground = true;
_searchThread.Start();
}

private void TestThread()
{
lock(_filesList)
{
_filesList.Items.Clear();

for(int i = 0; i < 15; ++i)
{
_filesList.Items.Add(i.ToString());
}
}
}
====================================

_filesList is a ListView control.

Is that lock all that's required to assure thread safety? My thread
doesn't appear to stack, so I know it's being suspended and such
properly. I don't get any exceptions, but as a threading noob, I want
to make sure I'm doing this right.

Thanks a lot guys.
 
All code that processes the _fileList needs to lock the fileList first. This is one of the reasons I hate the lock keyword - it seems to suggest that it erects an invisible forcefield around the object. All is does is acquire the monitor of the object. And all code that reads or manipulates the fileLiust needs to do the same to make sure only one of them processes the list at a time.

Regards

Richard Blewett
http://www.dotnetconsult.co.uk/weblog
http://www.dotnetconsult.co.uk

Hey all,

I'm threading newbie, I've never attempted it before. I've got a
button that when clicked starts up a thread to process some files.
This keeps my GUI responsive and gets the tasks done.

Here's an example of what I'm doing:
====================================
private void button1_Click(object sender, System.EventArgs e)
{
if(_searchThread != null && _searchThread.IsAlive)
_searchThread.Suspend();

_searchThread = new Thread(new ThreadStart(TestThread));

_searchThread.IsBackground = true;
_searchThread.Start();
}

private void TestThread()
{
lock(_filesList)
{
_filesList.Items.Clear();

for(int i = 0; i < 15; ++i)
{
_filesList.Items.Add(i.ToString());
}
}
}
====================================

_filesList is a ListView control.

Is that lock all that's required to assure thread safety? My thread
doesn't appear to stack, so I know it's being suspended and such
properly. I don't get any exceptions, but as a threading noob, I want
to make sure I'm doing this right.

Thanks a lot guys.


[microsoft.public.dotnet.languages.csharp]
 
Since, as Richard said you will have to synchronize other critical sections within your code where references to the _fileList object has been made, employ ReaderWriterLock type instead of using the Monitor type/lock keyword for thread synchronization. The ReaderWriterLock type allows for multiple readers and a single writer without being too restrictive like the Monitor type.

HTH, Metallikanz!
 
Back
Top