You should probably take a look at the
Monitor
class.
System.Threading.Monitor
Using
Invoke
performs a thread synchronisation and I normally only use it to allow background threads to update UI elements.
I normally use a code sample similar to this when I have multiple threads access objects in the background:
public class MixCollection()
{
public delegate void MixAdded(Mix mix);
public event MixAdded OnMixAdded;
private List<Mix> _theList = new List<Mix>();
public void Add(Mix item)
{
bool complete = false;
while(!complete)
{
if(System.Threading.Monitor.TryEnter(_theList, 100))
{
try
{
complete = true;
_theList.Add(item);
if(OnMixAdded != null)
OnMixAdded(item);
}
finally
{
System.Threading.Monitor.Exit(_theList);
}
}
else
System.Threading.Thread.Sleep(100);
}
}
}
public class MixUpdater()
{
private Thread _bgThread;
private MixCollection _mixes;
public MixUpdater(MixCollection mixes)
{
_mixes = mixes;
Start();
}
private bool _stop = false;
public void Stop() { _stop = true; }
public void Start()
{
if(_stop)
{
_bgThread = new Thread(new ThreadStart(Work));
_bgThread.Start();
}
}
private void Work()
{
while(!_stop)
{
_mixes.Add(mix);
Thread.Sleep(1000);
}
}
}
Then in your UI:
Note although back in your UI class, the thread bubbling the event is still a background thread and as such you need to invoke to update your UI.
public class MyForm : Form
{
MixCollection _mixes;
MixUpdater _mixUpdater;
public MyForm()
{
_mixes = new MixCollection();
_mixes.OnMixAdded += new MixCollection.MixAdd(OnMixAdded);
_mixUpdater = new MixUpdater(_mixes);
}
public void OnMixAdded(Mix mix)
{
ListBox.dispatcher.Invoke((Action)( ( ) => { ListBox.Items.Add(mix); } ), null);
}
}
I've typed all this off the top of my head, so I'm sorry if it's not quite right in your project, but you should get the general idea.