线程安全集合的快速搜索 [英] Fast Searches of a Thread Safe Collection of Structs

查看:79
本文介绍了线程安全集合的快速搜索的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Windows窗体应用程序中,我计划拥有一组结构 - 每个结构包含一堆描述一个人的属性(例如,LastName,

FirstName ,EmployeeID,HomeAddress,ZipCode等)。因此

结构的每个实例都将描述一个人 - 并且该集合中将包含约900个实例(人)




用户必须能够通过任何属性搜索特定人员(例如,LastName,FirstName,EmployeeID,HomeAddress,ZipCode等)。所以

集合必须可以通过所包含的

结构实例的所有属性进行搜索。


请注意比较将在(1)一个字符串之间,用户是
在文本框中键入,以及(2)指定的属性(例如,LastName) -

这样的AS用户类型,表单上其他位置显示的列表将动态显示所有最接近的匹配(最接近用户的字符串

正在键入用户类型)。 br />

该集合将定期在后台线程上更新(每15分钟左右) - 因此集合必须是线程安全的。


我的问题:

1.如何通过*

属性的* * *搜索结构集合(LastName) ,FirstName,Zip等...)。当然,一次只会搜索一个属性

。我认为实现IComparable会实现工作 - 但显然这样做会让我只能搜索

中的一个属性(但我需要启用搜索所有的$

2.如何使集合线程安全?这样集合可以由后台线程更新,而用户可能正在搜索

集合中的某个人。注意:人们[在收藏中]几乎永远不会被删除 - 因此幻影匹配并不是真正的问题。


只是寻找高层次的方向在这个。


非常感谢你!


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!


推荐答案

锁定读取和写入期间的列表。使用List< struct>和Find()使用

不同的谓词代表满足您的不同搜索需求。


-

William Stacey [MVP]


" Jeff S." < A@B.COM>在留言中写道

news:%2 **************** @ TK2MSFTNGP12.phx.gbl ...

|在Windows窗体应用程序中,我计划拥有一组结构 -

每个

|其中包含一堆描述一个人的属性(例如,

LastName,

| FirstName,EmployeeID,HomeAddress,ZipCode等)。所以每个实例的

| struct将描述一个人 - 大约900个实例(人)将是

|包含在收藏中。

|

|用户必须能够通过任何

属性搜索特定的人

| (例如,LastName,FirstName,EmployeeID,HomeAddress,ZipCode等)。那么

|集合必须可以通过所包含的所有属性进行搜索

|结构实例。

|

|请注意,比较将在(1)一个字符串之间,用户是
|键入文本框,和(2)指定的属性(例如,LastName) -

|这样,当用户输入AS时,表单上其他地方显示的列表将为

|动态显示所有最接近的匹配(最接近字符串

用户

|正在键入AS用户类型)。

|

|该集合将定期在后台线程上更新(每个

15

|左右分钟) - 因此集合必须是线程安全的。

|

|我的问题:

| 1.如何通过*

|的每个*来搜索结构集合属性(LastName,FirstName,Zip等...)。当然只有一个属性

|一次将被搜索。我认为实施IComparable



|工作 - 但显然这样做会让我只能搜索一个



|属性(但我需要启用搜索所有属性)。

|

| 2.如何使集合线程安全?所以收藏品可以



|用户可能正在搜索某个人的后台线程更新

$

|集合。注意:人们[在收藏中]几乎永远不会是

|删除 - 所以幻影匹配并不是真正的问题。

|

|只是寻找高级方向。

|

|非常感谢!

|

|

|

|
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!
|
|
|
|


你几乎肯定不应该为Person类使用use结构。


你的帖子谈到了这个人正在输入的字符串。进入一个盒子 -

这听起来像是对我的增量搜索 - 如果是这样你需要对特定属性的列表进行排序

而不是使用谓词。


" William Stacey [MVP]" <无线************ @ gmail.com>在消息中写道

news:ua ************** @ TK2MSFTNGP11.phx.gbl ...
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...
在读取期间锁定列表并写道。使用List< struct>和Find()使用
不同的谓词代表来满足您的不同搜索需求。

-
William Stacey [MVP]

" Jeff S. " < A@B.COM>在消息中写道
新闻:%2 **************** @ TK2MSFTNGP12.phx.gbl ...
|在Windows窗体应用程序中,我计划拥有一组结构 -
每个
|其中包含一堆描述某人的属性(例如,姓氏,姓名,员工ID,HomeAddress,ZipCode等)。所以
的每个实例都是struct将描述一个人 - 大约900个实例(人)将是
|收藏品中包含。
|
|用户必须能够通过任何
属性搜索特定的人物(例如,LastName,FirstName,EmployeeID,HomeAddress,ZipCode等)。那么

必须可以通过所包含的
|的所有属性搜索集合结构实例。
|
|请注意,比较将在(1)用户的字符串之间进行比较输入文本框,和(2)指定的属性(例如,
LastName) -
|这样,当用户输入AS时,表单上其他地方显示的列表将为
|动态显示所有最接近的匹配(最接近字符串
用户
|正在键入用户类型)。
|
|该集合将定期在后台线程上更新
(每个
15
|或几分钟) - 因此集合必须是线程安全的。
|
|我的问题:
| 1.如何通过*
|的每个*来搜索结构集合属性(LastName,FirstName,Zip等...)。当然只有一个
属性
|一次将被搜索。我认为实施IComparable

工作 - 但显然这样做会让我只能搜索一个

属性(但我需要启用搜索所有
属性)。
|
| 2.如何使集合线程安全?这样的收藏品可以
|用户可能在
中搜索某个人时由后台线程更新集合。注意:人们[在集合中]几乎永远不会是
|删除 - 所以幻影匹配并不是真正的问题。
|
|只是在寻找高层次的方向。
|
|非常感谢!
|
|
|
|
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!
|
|
|
|



谢谢威廉......你能扩展一下吗?我不清楚[...谓词

代表......]。


-Jeff

" William Stacey [MVP]" <无线************ @ gmail.com>在消息中写道

news:ua ************** @ TK2MSFTNGP11.phx.gbl ...
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...
在读取期间锁定列表并写道。使用List< struct>和Find()使用
不同的谓词代表来满足您的不同搜索需求。

-
William Stacey [MVP]

" Jeff S. " < A@B.COM>在消息中写道
新闻:%2 **************** @ TK2MSFTNGP12.phx.gbl ...
|在Windows窗体应用程序中,我计划拥有一组结构 -
每个
|其中包含一堆描述某人的属性(例如,姓氏,姓名,员工ID,HomeAddress,ZipCode等)。所以
的每个实例都是struct将描述一个人 - 大约900个实例(人)将是
|收藏品中包含。
|
|用户必须能够通过任何
属性搜索特定的人物(例如,LastName,FirstName,EmployeeID,HomeAddress,ZipCode等)。那么

必须可以通过所包含的
|的所有属性搜索集合结构实例。
|
|请注意,比较将在(1)用户的字符串之间进行比较输入文本框,和(2)指定的属性(例如,
LastName) -
|这样,当用户输入AS时,表单上其他地方显示的列表将为
|动态显示所有最接近的匹配(最接近字符串
用户
|正在键入用户类型)。
|
|该集合将定期在后台线程上更新
(每个
15
|或几分钟) - 因此集合必须是线程安全的。
|
|我的问题:
| 1.如何通过*
|的每个*来搜索结构集合属性(LastName,FirstName,Zip等...)。当然只有一个
属性
|一次将被搜索。我认为实施IComparable

工作 - 但显然这样做会让我只能搜索一个

属性(但我需要启用搜索所有
属性)。
|
| 2.如何使集合线程安全?这样的收藏品可以
|用户可能在
中搜索某个人时由后台线程更新集合。注意:人们[在集合中]几乎永远不会是
|删除 - 所以幻影匹配并不是真正的问题。
|
|只是在寻找高层次的方向。
|
|非常感谢!
|
|
|
|
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!
|
|
|
|



这篇关于线程安全集合的快速搜索的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆