|
I'm implementing some code as a separate class/DLL as part of an ASP.NET project. The original code used a"SyncLock Me"..."End SyncLock" block around the calls to an instance of theCryptoStream object.
The IDE complains about the use of "SyncLock Me" in the class, stating that "'Me' is valid only within an instance method."
What would be the proper way of implementing the SyncLock block within a DLL?
Richard | | RichardR Tuesday, August 05, 2008 7:48 PM | Well, without actually seeing the code.... The problem is most likely that the methods you've moved the code to are Shared. You can use Me in a Shared function/sub because there is no implicit object instance passed to these methods. Now, I generally avoid the whole synclock me thing anyway - since often you have locks in more then one unrelated method. And if thats the case, your needlessly locking execution of other functions. It's fine if all the methods are working with the same resource however. In your case, I would create a shared member object and then lock on that. Something like:
| ClassTheClass |
| PrivateReadOnlySharedlockAsObject=NewObject() |
|
| .... |
|
| PublicSubSomeMethod() |
| SyncLock(lock) |
| 'DoCoolStuff |
| EndSyncLock |
| EndSub |
|
| PublicSubSomeOtherMethod() |
| SyncLock(lock) |
| 'Domorecoolstuff |
| EndSyncLock |
| EndSub |
| EndClass | HTH
Tom Shelton - Marked As Answer byRiquel_DongModeratorTuesday, August 12, 2008 4:25 AM
-
| | Tom Shelton Tuesday, August 05, 2008 8:49 PM | Oops... my mind didn't recognized what my eyes saw. The Share attribute somehow got specified on the function declaration and I just didn't notice.After deleting the Share attribute, the IDE doesn't gripe about the SyncLock.
And to answer your other question, I'm implementing it as a member of a class.
If you want to look at the code, it's basicallya trimmed down version of the VB sample posted at http://www.obviex.com/samples/EncryptionWithSalt.aspx. It was the DecryptToBytes function that the Shared attribute got added to.
Thanks for your help,
Richard
- Marked As Answer byRiquel_DongModeratorTuesday, August 12, 2008 4:25 AM
-
| | RichardR Thursday, August 07, 2008 2:04 PM | Well, without actually seeing the code.... The problem is most likely that the methods you've moved the code to are Shared. You can use Me in a Shared function/sub because there is no implicit object instance passed to these methods. Now, I generally avoid the whole synclock me thing anyway - since often you have locks in more then one unrelated method. And if thats the case, your needlessly locking execution of other functions. It's fine if all the methods are working with the same resource however. In your case, I would create a shared member object and then lock on that. Something like:
| ClassTheClass |
| PrivateReadOnlySharedlockAsObject=NewObject() |
|
| .... |
|
| PublicSubSomeMethod() |
| SyncLock(lock) |
| 'DoCoolStuff |
| EndSyncLock |
| EndSub |
|
| PublicSubSomeOtherMethod() |
| SyncLock(lock) |
| 'Domorecoolstuff |
| EndSyncLock |
| EndSub |
| EndClass | HTH
Tom Shelton - Marked As Answer byRiquel_DongModeratorTuesday, August 12, 2008 4:25 AM
-
| | Tom Shelton Tuesday, August 05, 2008 8:49 PM | Tom,
Thanks for the reply. I do have my doubts if I really need to worry about it too. It's mainly just that the original code had it.
I'm not doing any multi-threading in the ASP.NET app that will be utilizing the library. And unless I'm wrong, I don't need to worry that there willhopefully be manypeople hitting the site simultaneously, and that various pages will be invoking their own instances of the library.
For what it's worth,here's the actual block of code:
| SyncLockMe |
| DimcryptoStreamAsCryptoStream |
| cryptoStream=NewCryptoStream(memoryStream,_ |
| decryptor,_ |
| CryptoStreamMode.Read) |
| decryptedByteCount=cryptoStream.Read(decryptedBytes,0,decryptedBytes.Length) |
| memoryStream.Close() |
| cryptoStream.Close() |
| EndSyncLock |
| Richard | | RichardR Wednesday, August 06, 2008 10:37 PM | Richard... To really help we need to see the context that the block is in. Can you post the entire method? Is it a member of aclass or a module? Like I said, it appears as if you are either in a module or a shared method of a class from the error....
Tom Shelton | | Tom Shelton Thursday, August 07, 2008 5:19 AM | Oops... my mind didn't recognized what my eyes saw. The Share attribute somehow got specified on the function declaration and I just didn't notice.After deleting the Share attribute, the IDE doesn't gripe about the SyncLock.
And to answer your other question, I'm implementing it as a member of a class.
If you want to look at the code, it's basicallya trimmed down version of the VB sample posted at http://www.obviex.com/samples/EncryptionWithSalt.aspx. It was the DecryptToBytes function that the Shared attribute got added to.
Thanks for your help,
Richard
- Marked As Answer byRiquel_DongModeratorTuesday, August 12, 2008 4:25 AM
-
| | RichardR Thursday, August 07, 2008 2:04 PM |
|