473,322 Members | 1,287 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,322 software developers and data experts.

Fast Searches of a Thread Safe Collection of Structs

In a Windows Forms application I plan to have a collection of structs - each
of which contains a bunch of properties describing a person (e.g., LastName,
FirstName, EmployeeID, HomeAddress, ZipCode, etc). So each instance of the
struct will describe a person - and about 900 instances (people) will be
contained in the collection.

Users must be able to search for a specific person by any of the properties
(e.g., LastName, FirstName, EmployeeID, HomeAddress, ZipCode, etc). So the
collection must be searchable by all of the properties of the contained
struct instances.

Please note that the comparison will be between (1) a string the user is
typing into a textbox, and (2) the specified property (e.g., LastName) --
such that AS the user types, a list displayed elsewhere on the form will
dynamically show ALL of the closest matches (closest to the string the user
is typing AS the user types).

The collection will periodically be updated on a background thread (every 15
or so minutes) - so the collection must be "thread safe."

MY QUESTIONS:
1. How do I make the collection of structs searchable by *each* of its
properties (LastName, FirstName, Zip, etc...). Of course only one property
at a time would be searched on. I would think implementing IComparable would
work - but apparently doing so would let me enable searching on only one of
the properties (but I need to enable searching on ALL of the properties).

2. How do I make the collection "thread safe" so that the collection can be
updated by a background thread while users may be searching for a person in
the collection. Note: people [in the collection] will almost never be
deleted - so phantom matches are not really a concern.

Just looking for high-level direction on this.

Thank you very much!


Mar 29 '06 #1
14 3799
Lock the list during reads and writes. Use List<struct> and Find() using
different Predicate delegates for your different search needs.

--
William Stacey [MVP]

"Jeff S." <A@B.COM> wrote in message
news:%2****************@TK2MSFTNGP12.phx.gbl...
| In a Windows Forms application I plan to have a collection of structs -
each
| of which contains a bunch of properties describing a person (e.g.,
LastName,
| FirstName, EmployeeID, HomeAddress, ZipCode, etc). So each instance of the
| struct will describe a person - and about 900 instances (people) will be
| contained in the collection.
|
| Users must be able to search for a specific person by any of the
properties
| (e.g., LastName, FirstName, EmployeeID, HomeAddress, ZipCode, etc). So the
| collection must be searchable by all of the properties of the contained
| struct instances.
|
| Please note that the comparison will be between (1) a string the user is
| typing into a textbox, and (2) the specified property (e.g., LastName) --
| such that AS the user types, a list displayed elsewhere on the form will
| dynamically show ALL of the closest matches (closest to the string the
user
| is typing AS the user types).
|
| The collection will periodically be updated on a background thread (every
15
| or so minutes) - so the collection must be "thread safe."
|
| MY QUESTIONS:
| 1. How do I make the collection of structs searchable by *each* of its
| properties (LastName, FirstName, Zip, etc...). Of course only one property
| at a time would be searched on. I would think implementing IComparable
would
| work - but apparently doing so would let me enable searching on only one
of
| the properties (but I need to enable searching on ALL of the properties).
|
| 2. How do I make the collection "thread safe" so that the collection can
be
| updated by a background thread while users may be searching for a person
in
| the collection. Note: people [in the collection] will almost never be
| deleted - so phantom matches are not really a concern.
|
| Just looking for high-level direction on this.
|
| Thank you very much!
|
|
|
|
Mar 29 '06 #2
And you almost certainly shouldn't be using use structs for a Person class.

Your post talks about the string that the person is "typing" into a box -
this sounds like an incremental search to me - if so you would need to sort
the list for the particular property instead of using predicates.

"William Stacey [MVP]" <wi************@gmail.com> wrote in message
news:ua**************@TK2MSFTNGP11.phx.gbl...
Lock the list during reads and writes. Use List<struct> and Find() using
different Predicate delegates for your different search needs.

--
William Stacey [MVP]

"Jeff S." <A@B.COM> wrote in message
news:%2****************@TK2MSFTNGP12.phx.gbl...
| In a Windows Forms application I plan to have a collection of structs -
each
| of which contains a bunch of properties describing a person (e.g.,
LastName,
| FirstName, EmployeeID, HomeAddress, ZipCode, etc). So each instance of
the
| struct will describe a person - and about 900 instances (people) will be
| contained in the collection.
|
| Users must be able to search for a specific person by any of the
properties
| (e.g., LastName, FirstName, EmployeeID, HomeAddress, ZipCode, etc). So
the
| collection must be searchable by all of the properties of the contained
| struct instances.
|
| Please note that the comparison will be between (1) a string the user is
| typing into a textbox, and (2) the specified property (e.g.,
LastName) --
| such that AS the user types, a list displayed elsewhere on the form will
| dynamically show ALL of the closest matches (closest to the string the
user
| is typing AS the user types).
|
| The collection will periodically be updated on a background thread
(every
15
| or so minutes) - so the collection must be "thread safe."
|
| MY QUESTIONS:
| 1. How do I make the collection of structs searchable by *each* of its
| properties (LastName, FirstName, Zip, etc...). Of course only one
property
| at a time would be searched on. I would think implementing IComparable
would
| work - but apparently doing so would let me enable searching on only one
of
| the properties (but I need to enable searching on ALL of the
properties).
|
| 2. How do I make the collection "thread safe" so that the collection can
be
| updated by a background thread while users may be searching for a person
in
| the collection. Note: people [in the collection] will almost never be
| deleted - so phantom matches are not really a concern.
|
| Just looking for high-level direction on this.
|
| Thank you very much!
|
|
|
|

Mar 29 '06 #3
Thanks William... can you expand just a bit? I'm not clear on [... Predicate
delegates... ].

-Jeff
"William Stacey [MVP]" <wi************@gmail.com> wrote in message
news:ua**************@TK2MSFTNGP11.phx.gbl...
Lock the list during reads and writes. Use List<struct> and Find() using
different Predicate delegates for your different search needs.

--
William Stacey [MVP]

"Jeff S." <A@B.COM> wrote in message
news:%2****************@TK2MSFTNGP12.phx.gbl...
| In a Windows Forms application I plan to have a collection of structs -
each
| of which contains a bunch of properties describing a person (e.g.,
LastName,
| FirstName, EmployeeID, HomeAddress, ZipCode, etc). So each instance of
the
| struct will describe a person - and about 900 instances (people) will be
| contained in the collection.
|
| Users must be able to search for a specific person by any of the
properties
| (e.g., LastName, FirstName, EmployeeID, HomeAddress, ZipCode, etc). So
the
| collection must be searchable by all of the properties of the contained
| struct instances.
|
| Please note that the comparison will be between (1) a string the user is
| typing into a textbox, and (2) the specified property (e.g.,
LastName) --
| such that AS the user types, a list displayed elsewhere on the form will
| dynamically show ALL of the closest matches (closest to the string the
user
| is typing AS the user types).
|
| The collection will periodically be updated on a background thread
(every
15
| or so minutes) - so the collection must be "thread safe."
|
| MY QUESTIONS:
| 1. How do I make the collection of structs searchable by *each* of its
| properties (LastName, FirstName, Zip, etc...). Of course only one
property
| at a time would be searched on. I would think implementing IComparable
would
| work - but apparently doing so would let me enable searching on only one
of
| the properties (but I need to enable searching on ALL of the
properties).
|
| 2. How do I make the collection "thread safe" so that the collection can
be
| updated by a background thread while users may be searching for a person
in
| the collection. Note: people [in the collection] will almost never be
| deleted - so phantom matches are not really a concern.
|
| Just looking for high-level direction on this.
|
| Thank you very much!
|
|
|
|

Mar 29 '06 #4
Jeff S. <A@B.COM> wrote:
Thanks William... can you expand just a bit? I'm not clear on [... Predicate
delegates... ].


Just to check before too much detail is gone into - are you using .NET
2.0 or 1.1? You won't be able to use List<T> if you're only using 1.1.

Also, do you *have* to use structs? Why are you using structs rather
than classes?

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Mar 29 '06 #5
RE:
<< are you using 2.0 or 1.1?>>
I could develop in either. Preference is for 2.0 although preliminary work
has been done in 1.1 (I'll be migrating soon and this particular issue may
cause me to go to 2.0 immediately)... This also means I haven't used
generics yet and I haven't done any non trivial work yet in 2.0.

RE:
<<do you *have* to use structs? Why are you using structs rather than
classes?>>
I was using structs simply because they are lighter weight than classes. All
I really need here is "a bunch of properties that identify a person" and my
thinking was that a struct would be sufficient. That's all. I could just as
easily implement with classes or ?? (this thing is still in the design phase
and I haven't written much code for it yet.

Thanks!


"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message
news:MP************************@msnews.microsoft.c om...
Jeff S. <A@B.COM> wrote:
Thanks William... can you expand just a bit? I'm not clear on [...
Predicate
delegates... ].


Just to check before too much detail is gone into - are you using .NET
2.0 or 1.1? You won't be able to use List<T> if you're only using 1.1.

Also, do you *have* to use structs? Why are you using structs rather
than classes?

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too

Mar 29 '06 #6
Jeff S. <A@B.COM> wrote:
RE:
<< are you using 2.0 or 1.1?>>
I could develop in either. Preference is for 2.0 although preliminary work
has been done in 1.1 (I'll be migrating soon and this particular issue may
cause me to go to 2.0 immediately)... This also means I haven't used
generics yet and I haven't done any non trivial work yet in 2.0.
Using generics in .NET 2.0 would mean you could avoid boxing
performance overheads using generic collections, if you do end up using
a value type.
RE:
<<do you *have* to use structs? Why are you using structs rather than
classes?>>
I was using structs simply because they are lighter weight than classes.
Actually, they're often heavier. Any time you're passing round a struct
which has more than a few properties, that's going to heavier than
using a class. Value types can often be harder to work with, too.
All I really need here is "a bunch of properties that identify a
person" and my thinking was that a struct would be sufficient. That's
all. I could just as easily implement with classes or ?? (this thing
is still in the design phase and I haven't written much code for it
yet.


I *strongly* suggest that you learn more about .NET and C# before going
any further. A decent understanding of the difference between value
types and reference types is absolutely crucial to programming in .NET,
for a start.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Mar 29 '06 #7
RE:
<< I *strongly* suggest that you learn more about .NET and C# before going
any further>>
I *certainly* agree with you that I should learn more about .NET - but your
recommendation is so vague as to be not helpful. My OP was specifically NOT
asking for code samples or otherwise for others to do my work BECAUSE I was
wanting high-level guidance that would help me to focus my learning in a way
that is relevant to the current project. That's how a lot of learning
happens - I'm sure you've likely done the same many times over your career.

The response by William Stacey was pretty much the sort of guidance I was
looking for - but I was just wanting a little bit more, like what a
Predicate delegate is. Before I asked him to clarify I googled it and found
nothing apparently relevant... then went to my 3.0 books and didn't find
anything helpful... thus my followup post. I know what a delegates is - just
not sure of the "Predicate" delegate part of his recommendation. I hope that
doesn't make me too dumb to be helped.

-Jeff


"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message
news:MP************************@msnews.microsoft.c om...
Jeff S. <A@B.COM> wrote:
RE:
<< are you using 2.0 or 1.1?>>
I could develop in either. Preference is for 2.0 although preliminary
work
has been done in 1.1 (I'll be migrating soon and this particular issue
may
cause me to go to 2.0 immediately)... This also means I haven't used
generics yet and I haven't done any non trivial work yet in 2.0.


Using generics in .NET 2.0 would mean you could avoid boxing
performance overheads using generic collections, if you do end up using
a value type.
RE:
<<do you *have* to use structs? Why are you using structs rather than
classes?>>
I was using structs simply because they are lighter weight than classes.


Actually, they're often heavier. Any time you're passing round a struct
which has more than a few properties, that's going to heavier than
using a class. Value types can often be harder to work with, too.
All I really need here is "a bunch of properties that identify a
person" and my thinking was that a struct would be sufficient. That's
all. I could just as easily implement with classes or ?? (this thing
is still in the design phase and I haven't written much code for it
yet.


I *strongly* suggest that you learn more about .NET and C# before going
any further. A decent understanding of the difference between value
types and reference types is absolutely crucial to programming in .NET,
for a start.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too

Mar 29 '06 #8
Jeff S. <A@B.COM> wrote:
<< I *strongly* suggest that you learn more about .NET and C# before going
any further>>
I *certainly* agree with you that I should learn more about .NET - but your
recommendation is so vague as to be not helpful.
Okay, here's a more concrete recommendation: read a book or tutorial
about C# and .NET which starts from a beginner's perspective. Pay close
attention to chapters which describe the differences between value
types and reference types, and parameter passing, too.
My OP was specifically NOT
asking for code samples or otherwise for others to do my work BECAUSE I was
wanting high-level guidance that would help me to focus my learning in a way
that is relevant to the current project. That's how a lot of learning
happens - I'm sure you've likely done the same many times over your career.
Well, I usually haven't tried to embark on a real-life project without
a reasonable understanding of the basics of what I'm using. It's fine
to use projects to learn, but I wouldn't try it with a real world
production project, which I assumed your question was about (given the
very specific nature of it). Threading is a relatively advanced topic,
for instance - so one would hope that someone asking a question
involving threading (as yours did) would have a good grounding in the
basics.
The response by William Stacey was pretty much the sort of guidance I was
looking for - but I was just wanting a little bit more, like what a
Predicate delegate is. Before I asked him to clarify I googled it and found
nothing apparently relevant... then went to my 3.0 books and didn't find
anything helpful... thus my followup post. I know what a delegates is - just
not sure of the "Predicate" delegate part of his recommendation. I hope that
doesn't make me too dumb to be helped.


You're not too dumb to be helped at all - but without a good
understanding of the fundamentals of the framework, I don't think it's
a good idea to continue with a real project.

Put it this way - asking about delegates without understanding value
types is a bit like someone asking for good examples of the subjunctive
mood without understanding the difference between a verb and a noun. I
*could* give you examples of implementing predicates using anonymous
methods etc, but the chances of you properly understanding those
examples without a sound knowledge of the basics are very slim. In
short, I don't think it would be helpful in the long run.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Mar 29 '06 #9
Do you have the MSDN help installed? Look in the List<T>.Find method help.
What version of VS are you using?

--
William Stacey [MVP]
Mar 29 '06 #10
I agree with Jon for this reason - if you get an example of how to do the
sort you will be doing ONE thing right (and even then you could have done it
the hard way - it would just be slower) but if you if you continue to
misunderstand when it is appropriate to use a struct you will be doing MANY
things wrong and they will manifest themselves throughout your as they will
be used in multiple places whereas the sorting is only done in a single
method.
Mar 29 '06 #11
> I was using structs simply because they are lighter weight than classes.

I will add my voice to the chorus: use classes not structs in this
case. There are occasions when it is appropriate to use structs instead
of classes "...because they are lighter weight..." but these occasions
are very, very rare indeed. The perception that structs are nothing
more than "classes lite" is just dead wrong. Fortunately, most new
programmers who insist on forging ahead with structs find this out "the
hard way." Unfortunately some of them then conclude that C# is junk,
rather than realizing that failing at pounding in a nail with a
screwdriver does not mean that screwdrivers are useless.
Users must be able to search for a specific person by any of the properties (e.g., LastName, FirstName, EmployeeID, HomeAddress, ZipCode, etc).


I would approach this as follows.

Check into the Comparer class and the IComparer interface that it
implements. These would allow you to write generic searching code that
takes a Comparer and a Person as its arguments and finds the Person or
Persons from the list that "compare equal" to the given Person based on
the results of your particular Comparer. This also allows you to search
based on any combination of the given properties in one simple
operation, simply by varying the Comparer.

You could also build more sophisticated Comparers that allow for
wildcard searches, "LIKE" searches, and other things like that.

Whatever you eventually decide to do, make Person a class. If you want
to know exactly why, try searching this newsgroup for "struct vs class"
or words to that effect. The topic comes up often here, frequently
raised by frustrated programmers who don't understand why C# is acting
so weird. :)

Mar 29 '06 #12
Just a quick thought, but have you looked at the ADO classes for
performance, vs. a struct that implement IComparable? With what you are
describing as a disconnected data source, the filter expressions should
make the overall task easier, but I don't know what if any implications
it would have as far as thread locking is concerned.

I have been impressed with ADO.NET's performance in scenarios like this.
Worth checking I supposed. Let us know what you find.

Regards

John Parrish
Mar 30 '06 #13
Careful on the attitude.. the line between berating and mentoring might
seem fine, but it never is to the recipient of either. While I agree
with your sentiments, I think as an MVP you might do a better job at
encouragement. Perhaps spend a little more time explaining, otherwise,
perhaps not say anything at all.
Mar 30 '06 #14
John Parrish <as*@me.com> wrote:
Careful on the attitude.. the line between berating and mentoring might
seem fine, but it never is to the recipient of either. While I agree
with your sentiments, I think as an MVP you might do a better job at
encouragement. Perhaps spend a little more time explaining, otherwise,
perhaps not say anything at all.


If the OP shows some interest in really learning the differences
between value types and reference types, I'll be very happy to help.
However, I believe that explaining something advanced when we *know*
the basics are missing is a bad idea - in my experience it only leads
to more questions which could easily be avoided by learning those
basics first. I've seen it time and time again in newsgroups (it was
worse on the Java ones).

I dare say I could have expressed it better though...

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Mar 30 '06 #15

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

Similar topics

2
by: David | last post by:
Hi guys I have in my application many collections (HashTable and ArrayList)loaded with configuration data, that means they are loaded at startup and then they are accessed only for read-only...
6
by: NutJob | last post by:
Hello all, I'm aware that in Python an object is cleared for garbage collection as soon as the last reference to it disappears. Normally this is fine. However, in my current project I'm creating...
3
by: Grandma Wilkerson | last post by:
Hi, The documentation states that enumeration through a collection is inherently NOT thread-safe, since a thread which added/removed an item from said collection could screw up the thread that...
1
by: Ryan Liu | last post by:
Hashtable.GetEnumerator is not thread safe, but what about I write I loop go though Hashtable.Values, is that thread saft? Thanks a lot!
7
by: Alexander Walker | last post by:
Hello I want to get the value of a property of a control from a thread other than the thread the control was created on, as far as I can see this is not the same as invoking an operation on a...
6
by: fniles | last post by:
I am using VB.NET 2003 and a socket control to receive and sending data to clients. As I receive data in 1 thread, I put it into an arraylist, and then I remove the data from arraylist and send it...
0
by: adubra | last post by:
Hi there, I am using a device context (DC) and a buffer to successfully draw to screen. However, when I update the DC at very high frame rate and drag the frame containing the image very quickly...
5
by: Max2006 | last post by:
Hi, Since Application collection and Page.Cache can be shared among all sessions, I wonder if they are thread safe? Thank you, Max
13
by: Henri.Chinasque | last post by:
Hi all, I am wondering about thread safety and member variables. If I have such a class: class foo { private float m_floater = 0.0; public void bar(){ m_floater = true; }
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...

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.