473,402 Members | 2,053 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,402 software developers and data experts.

Background reading of large array of records

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,"",fa lse);
}
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,"",fa lse);
_AllUsers = allusers;
}
catch(Exception e)
{
LogException(fnName, e);
Invoke(new OnThreadExceptionDelegate(OnThreadException), new object[]
{e});
}
finally
{
_ReadUsersThread = null;
}
}// function
Aug 15 '07 #1
4 1990


"Claire" wrote:
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,"",fa lse);
}
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,"",fa lse);
_AllUsers = allusers;
}
catch(Exception e)
{
LogException(fnName, e);
Invoke(new OnThreadExceptionDelegate(OnThreadException), new object[]
{e});
}
finally
{
_ReadUsersThread = null;
}
}// function
Hello Claire,

i read the code and ther a lot to improve in the cod, first lets start with
architecture, do we want the system to load the user manually (property get )
or useing cache memory loading the user, listen to event of user changes in
the data base and update the user cache storage, the code try to solve some
of the issue, but there is no listen or caching mecahnisem (it should be
simple cache), also try to figure out whther you can listen to the data bse
changes and reflect the changes to the cache.

Second.
'While' its a bad code, commonly used in naive script to eait to somethings,
there are some ways to handle this, In case you will implement a simple
cache, so have a static sync object will solve this, so in case the static
sync object is not locked, so check the cache and get the data, saperate the
data implementation from the using the data.
Another way, use monitor, like lock.
Another way, wait call back, the event method, in case the thread complete
you can listen to event (from the cahce) and read the data.
There alot of ways to implement this situation, check the wab (lot of
examples).

Its bad to write this, because its a hell gate, after long time you can
forget this code and you can get bugs whcih are very hard to find occurred by
the sleep(0).
------- while (_ReadUsersThread != null) Thread.Sleep(0); ---------

Hope it will help to you.

Yaron karni
http://dotnetbible.blogspot.com/

Aug 15 '07 #2
Hello Claire,
>
i read the code and ther a lot to improve in the cod, first lets start
with
architecture, do we want the system to load the user manually (property
get )
or useing cache memory loading the user, listen to event of user changes
in
the data base and update the user cache storage, the code try to solve
some
of the issue, but there is no listen or caching mecahnisem (it should be
simple cache), also try to figure out whther you can listen to the data
bse
changes and reflect the changes to the cache.
Hello Yaron
I'd love to do something like this for the project, keep a cache of users
somewhere.
Would u point me in the general direction of what I should be looking at to
"listen" for changes on a database. This is the first database application
I've ever written.
I'm using ADO ( IDbConnection,IDbDataAdapter etc) , desktop application.
(Must work with MySQL 5.0 and/or Microsoft SQL server engines)
Aug 15 '07 #3
You may loook at the following link
http://www.codeproject.com/dotnet/usingado3.asp

hope it will help, in our company we develop a platform called AIS
(www.attunity.com) which have the ability (one on many abilities) to notify
on every data base data changed.
--
Sincerely
Yaron Karni
http://dotnetbible.blogspot.com/
"Claire" wrote:
Hello Claire,

i read the code and ther a lot to improve in the cod, first lets start
with
architecture, do we want the system to load the user manually (property
get )
or useing cache memory loading the user, listen to event of user changes
in
the data base and update the user cache storage, the code try to solve
some
of the issue, but there is no listen or caching mecahnisem (it should be
simple cache), also try to figure out whther you can listen to the data
bse
changes and reflect the changes to the cache.

Hello Yaron
I'd love to do something like this for the project, keep a cache of users
somewhere.
Would u point me in the general direction of what I should be looking at to
"listen" for changes on a database. This is the first database application
I've ever written.
I'm using ADO ( IDbConnection,IDbDataAdapter etc) , desktop application.
(Must work with MySQL 5.0 and/or Microsoft SQL server engines)
Aug 15 '07 #4
Claire wrote:
[...]
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?
No. It's a fine practice. Good, even.
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.
Well, the main reason the loop is bad is that it means that the AllUsers
method will now block until the thread exits. There are other thing bad
about that sort of thing, but the blocking basically negates whatever
benefit you had to putting the work in a background thread in the first
place.

Yes, it's slightly better than just calling the code directly, since at
least you can get it going ahead of time. But you ought to have a
design that does not make the main UI thread block at all.
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?
You should not have to look at the thread instance value at all.

On your main form, you should have some logic that does something
appropriate until the data is available. Display a placeholder value or
some sort of "not ready" label until the background thread is done.

For the background thread itself, my suggestion is that you use the
BackgroundWorker class, and make sure you use it as intended. That is,
subscribe to the events it contains for the purpose of notification of
its progress and completion. This will ensure that all of your handling
of the thread's state still happens on your main UI thread, avoiding any
need to lock or otherwise synchronize the data.

The synchronization will happen naturally by you ensuring that you only
ever access things on the main thread when they are known to be valid
and not in use by the background thread. Use the BackgroundWorker's
RunWorkerCompleted event to set state that tells you what's valid and
what's not and when you might be able to update the UI to reflect the
completion of the background worker. This means, for one, being able to
handle the AllUsers property returning null, and doing something
appropriate with that null value (like displaying the placeholder, as
mentioned above).

Pete
Aug 15 '07 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: Shahid Juma | last post by:
Hi, I have a text file which I would like to read from the end and display only a certain number of records. Is there any way of doing this? Thanks, Shahid
4
by: Matthew Crema | last post by:
Hello, Say I have 1000 text files and each is a list of 32768 integers. I have written a C program to read this data into a large matrix. I am using fopen in combination with fscanf to read...
7
by: John Dann | last post by:
I'm trying to read some binary data from a file created by another program. I know the binary file format but can't change or control the format. The binary data is organised such that it should...
23
by: shank | last post by:
I have the below code found on an ASP site. <% arrName = Split(Request("TextArea"),",") %> <% For i = LBound(arrName) To UBound(arrName) Response.Write "ID: " & arrName(i) & "<br>" Next %>
7
by: jccorreu | last post by:
I've got to read info from multiple files that will be given to me. I know the format and what the data is. The thing is each time we run the program we may be using a differnt number of files,...
2
by: GeoUK | last post by:
Hi All, New member here with a bit of problem. I have read the FAQ's and searched text books but still cannot solve the problem that I have. As part of a course I am doing at University I had...
1
by: alokw | last post by:
Hi everyone, Here's my problem. I'd like to revamp my web site, and I have this idea. I want to create essentially a border around the screen of about 100 pixels of just black. Heres where...
7
by: ianenis.tiryaki | last post by:
well i got this assignment which i dont even have a clue what i am supposed to do. it is about reading me data from the file and load them into a parallel array here is the question: Step (1) ...
10
by: laredotornado | last post by:
Hi, I'm using PHP 5. I have a function that takes a little while to run, and I don't want the user to have to wait while it does. Is there a way to run this function in the background? ...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.