C
Claire
Hi,
I'm writing an internal mail system module for my project.
There may be several thousand 'users' on site, and I need the whole list for
filtering, so I decided to load up the users in the main form of the module
in a background thread
I used to read in this list the first time it was required, but as there's
usually a "dead" period between opening the main form and actually needing
the list it seemed a good idea to do it this way to speed things up.
Is that bad practice?
I also dont like what Ive done in the following.
"while (_ReadUsersThread != null)Thread.Sleep(0);"
I don't THINK that _ReadUsersThread could remain viable forever as it's set
to null on exit of the thread method, and this is one place I'd need a
synchronous read of the database anyway, but I think while loops like that
are bad.
I also think that I should be using a Lock when I access _ReadUsersThread,
but not sure what I should lock.
Would someone comment please on a better way of doing it?
private void MessageCentreInboxForm_Load(object sender, System.EventArgs e)
{
_ReadUsersThread = new Thread(new ThreadStart(ReadUsersThreadMethod));
_ReadUsersThread.IsBackground = true;
_ReadUsersThread.Start();
}
// property getter for AllUsers
private tableUsers[] AllUsers
{
get
{
string fnName = "AllUsers:get";
// Wait for _ReadUsersThread to finish if it's
// already reading from the database
while (_ReadUsersThread != null)Thread.Sleep(0);
if (_AllUsers == null)
{
try
{
_AllUsers = _database.ReadUsers(tableUsers.UserType.User,"",false);
}
catch(Exception e)
{
LogException(fnName, e);
frmWarning.ShowError(this, e);
}
}
return _AllUsers;
}
}
private void ReadUsersThreadMethod()
{
string fnName = "ReadUsersThreadMethod";
try
{
tableUsers[] allusers =
_database.ReadUsers(tableUsers.UserType.User,"",false);
_AllUsers = allusers;
}
catch(Exception e)
{
LogException(fnName, e);
Invoke(new OnThreadExceptionDelegate(OnThreadException), new object[]
{e});
}
finally
{
_ReadUsersThread = null;
}
}// function
I'm writing an internal mail system module for my project.
There may be several thousand 'users' on site, and I need the whole list for
filtering, so I decided to load up the users in the main form of the module
in a background thread
I used to read in this list the first time it was required, but as there's
usually a "dead" period between opening the main form and actually needing
the list it seemed a good idea to do it this way to speed things up.
Is that bad practice?
I also dont like what Ive done in the following.
"while (_ReadUsersThread != null)Thread.Sleep(0);"
I don't THINK that _ReadUsersThread could remain viable forever as it's set
to null on exit of the thread method, and this is one place I'd need a
synchronous read of the database anyway, but I think while loops like that
are bad.
I also think that I should be using a Lock when I access _ReadUsersThread,
but not sure what I should lock.
Would someone comment please on a better way of doing it?
private void MessageCentreInboxForm_Load(object sender, System.EventArgs e)
{
_ReadUsersThread = new Thread(new ThreadStart(ReadUsersThreadMethod));
_ReadUsersThread.IsBackground = true;
_ReadUsersThread.Start();
}
// property getter for AllUsers
private tableUsers[] AllUsers
{
get
{
string fnName = "AllUsers:get";
// Wait for _ReadUsersThread to finish if it's
// already reading from the database
while (_ReadUsersThread != null)Thread.Sleep(0);
if (_AllUsers == null)
{
try
{
_AllUsers = _database.ReadUsers(tableUsers.UserType.User,"",false);
}
catch(Exception e)
{
LogException(fnName, e);
frmWarning.ShowError(this, e);
}
}
return _AllUsers;
}
}
private void ReadUsersThreadMethod()
{
string fnName = "ReadUsersThreadMethod";
try
{
tableUsers[] allusers =
_database.ReadUsers(tableUsers.UserType.User,"",false);
_AllUsers = allusers;
}
catch(Exception e)
{
LogException(fnName, e);
Invoke(new OnThreadExceptionDelegate(OnThreadException), new object[]
{e});
}
finally
{
_ReadUsersThread = null;
}
}// function