I'm writing an application with a layered communications interace. This was done to abstract the communications from the user-interface part of the application and also to make it more scaleable/maintainable. For instance:
Consider each box in the figure above as a separate class. The Generic Comms Interface populates string variables describing the transacted data and comms "health," which are in turn copied up to the Application through a series of public function calls. For example, the Application would make a call to the App-Sub-System:
class Application
{
private void SomeUpdateFunction()
{
this.textBox1.AppendText(this.AppSubSystem.GetText());
}
}
class AppSubSystem
{
public string GetText()
{
return this.GenericCommsInterface.GetText();
}
}
class GenericCommsInterface
{
public string GetText()
{
string sRetVal = this.sText; // sText is populated by other functions in the class.
this.sText = null; // Suspected race condition is here.
return sRetVal;
}
}
sText
is populated asynchronously by other functions in the class. I belive a race condition is happening between string sRetVal = this.sText;
and the following line this.sText = null;
. Can someone suggest a way to avoid or prevent this race condition? Would using StringBuilder
help, or is there another way that I should be doing this?