Re: DataTable weirdness
From: Stephan Steiner (steiner_at_isuisse.com)
Date: 03/01/05
- Next message: Tom: "Re: DES decryption for des_enc of Tim Hudson and Eric Young"
- Previous message: vijaygparikh_at_gmail.com: "how to open default mail client with attach files"
- Next in thread: Chris R. Timmons: "Re: DataTable weirdness"
- Reply: Chris R. Timmons: "Re: DataTable weirdness"
- Messages sorted by: [ date ] [ thread ]
Date: Tue, 1 Mar 2005 14:46:30 +0100
Chris
Thanks for the link to the article (I've seen it on other occasions but
never read the entire thing).
I'm currently implementing locks into my software, but I'm wondering:
If I iterate through all the elements of a table in thread A, and at the
same time I write to a cell in the database from a thread B (the entire
write operation is encapsulated in a lock statement). The doc says DB reads
are thread safe. So, am I correct assuming that the lock in thread B will
block until the iterator is no longer used?
Come to think of it I've experienced similar conditions previously: while
iterating through the rows in a table, I find that I need to delete a row. I
can delete the row just fine inside the iteration but when I try to get the
next element via the foreach iterator, I get an exception because the
collection of lines has changed. So I ended up storing which lines I have to
delete, then after going through all lines, I deleted the lines marked for
deletion. In this case, what would an attempt to delete a record directly
within the iteration (using the lock statement) yield? Will the code in the
lock statement just be skipped because I'm currently interating through the
table rows?
Regards
Stephan
>
> If the method updating the data structure is an instance method, then
> locking "this" is what is usually done:
>
> lock(this)
> {
> ...
> myRow["timestamp"] = timestamp;
> ...
> }
>
> If the method is a static method, there will be no "this" to lock.
> In that case, the class needs to create an object to lock on:
>
> public class MyClass
> {
> private static readonly object countLock = new object();
>
> public static void UpdateMyRow()
> {
> lock(countLock)
> {
> ...
> myRow["timestamp"] = timestamp;
> ...
> }
> }
> }
>
>
> MVP Jon Skeet has written an excellent article about threading in C#:
>
> http://www.yoda.arachsys.com/csharp/threads/
>
> --
> Hope this helps.
>
> Chris.
> -------------
> C.R. Timmons Consulting, Inc.
> http://www.crtimmonsinc.com/
- Next message: Tom: "Re: DES decryption for des_enc of Tim Hudson and Eric Young"
- Previous message: vijaygparikh_at_gmail.com: "how to open default mail client with attach files"
- Next in thread: Chris R. Timmons: "Re: DataTable weirdness"
- Reply: Chris R. Timmons: "Re: DataTable weirdness"
- Messages sorted by: [ date ] [ thread ]
Relevant Pages
|