A
aine_canby
I have a class which has a member which takes a long time to populate.
Some of this member is serialised with an xml file, and some is
established through a set of commands issued on the system. For this
reason I have decided to populate this member using a thread.
class MyClass
{
Member member
Thread populateMemberThread;
bool isMemberPopulated;
PopulateMember()
{
isMemberPopulated = false;
populateMemberThread = new
System.Threading.Thread(PopulateMemberThread);
populateMemberThread.Start();
}
PopulateMemberThread()
{
member = new Member();
XmlSerializer xmlSerializer = ...
...
isMemberPopulated = true;
}
StopPopulateMember()
{
if (populateMemberThread.ThreadState !=
System.Threading.ThreadState.Stopped)
populateMemberThread.Abort();
}
}
The data associated with this member is displayed in a tree view on
the MainForm when member is fully populated. This is done as follows -
populateMemberTreeViewThread = new
System.Threading.Thread(PopulateMemberTreeViewThread);
populateMemberTreeViewThread.Start();
void PopulateMemberTreeViewThread()
{
while (!myClassObject.IsMemberPopulated )
{
Thread.Sleep(500);
}
this.Invoke(new
PopulateMemberTreeViewDelegate(PopulateMemberTreeView));
}
// Form Main's OnClosing
private void OnClosing(object sender, EventArgs ev)
{
if (populateMemberTreeViewThread.ThreadState !=
System.Threading.ThreadState.Stopped)
populateMemberTreeViewThread.Abort();
myClass.StopPopulateMemberThread();
}
So, my question is what improvements can you suggest regarding my code
above.
One issue I can see is if member was accessed before it was finished
being populated. Should I therefore lock member when I populate it?
Thanks,
Aine.
Some of this member is serialised with an xml file, and some is
established through a set of commands issued on the system. For this
reason I have decided to populate this member using a thread.
class MyClass
{
Member member
Thread populateMemberThread;
bool isMemberPopulated;
PopulateMember()
{
isMemberPopulated = false;
populateMemberThread = new
System.Threading.Thread(PopulateMemberThread);
populateMemberThread.Start();
}
PopulateMemberThread()
{
member = new Member();
XmlSerializer xmlSerializer = ...
...
isMemberPopulated = true;
}
StopPopulateMember()
{
if (populateMemberThread.ThreadState !=
System.Threading.ThreadState.Stopped)
populateMemberThread.Abort();
}
}
The data associated with this member is displayed in a tree view on
the MainForm when member is fully populated. This is done as follows -
populateMemberTreeViewThread = new
System.Threading.Thread(PopulateMemberTreeViewThread);
populateMemberTreeViewThread.Start();
void PopulateMemberTreeViewThread()
{
while (!myClassObject.IsMemberPopulated )
{
Thread.Sleep(500);
}
this.Invoke(new
PopulateMemberTreeViewDelegate(PopulateMemberTreeView));
}
// Form Main's OnClosing
private void OnClosing(object sender, EventArgs ev)
{
if (populateMemberTreeViewThread.ThreadState !=
System.Threading.ThreadState.Stopped)
populateMemberTreeViewThread.Abort();
myClass.StopPopulateMemberThread();
}
So, my question is what improvements can you suggest regarding my code
above.
One issue I can see is if member was accessed before it was finished
being populated. Should I therefore lock member when I populate it?
Thanks,
Aine.