|
Hi,
I try to understand what is sync block index in reference type -
I google it and still did not find anything that explain it in way i understand.
Can someone please explain what is it ?
Thanks.
|
|
|
|
|
It's a very scary and 'under-the-hood' kind of thing. It appears to be an integer that is added to all (or some, sources don't agree) objects and has something to do with locking and the GC.
Before I go about unearthing huge PDF's containing detailed descriptions, is there there anything in particular that you'd like to know about it?
|
|
|
|
|
|
The SyncBlock is part of the .NET type internal structures (namely the OBJECTREF structure), and primarily has to do with thread synchronization. Generally, a SyncBlock is not created for classes, and the index # will be 0. If you use the lock() statement, then the CLR will create a SyncBlock entry for the current type, and associate it with the current AppDomain. When the lock block is entered, the CLR will create a Monitor object and stick it in the SyncBlock, along with the classes hash code (the AppDomainID and hash code are intended to support COM interop, and are not really used for pure managed code). The SyncBlock will then stay alive for the lifetime of its associated object and appdomain.
The SyncBlock index is a 1-based index into something called the Syncblk Entry Table, which is an indirection into the actual SyncBlock List which contains pointers to each SyncBlock instance. The reason for the multiple levels of indirection are intended to support the GC, which can freely move objects (such as SyncBlocks) around in memory. Each object instance itself contains a numeric 1-based index into the Syncblk Entry Table rather than a direct pointer so that the GC IS free to move SyncBlocks around in memory without requireing too much overhead to maintain type system integrity.
|
|
|
|
|
|
One difficulty with the normal exception system is that while it allows applications to use catch exceptions from which other exceptions are inherited, the normal exception hierarchy is based upon what type of problem occurred, rather than upon what the problem 'means'. An TimeoutException that occurs in one circumstance should probably be handled totally differently from one that occurs in another.
Although .Net allows one to define a custom hierarchy for one's own exceptions, each exception class needs to repeat the code for the four standard New() methods, and so creating a significant number of classes can be a nuisance.
One approach which would seem to hold some promise would be for an application to express its own exceptions using generic classes, something like:
Class GenEx
Inherits Exception
... New() methods here
End Class
Class GenEx(of T)
Inherits GenEx
... New() methods here
End Class
Class GenEx(of T, U)
Inherits GenEx(of T)
... New() methods here
End Class
Class GenEx(of T, U, V)
Inherits GenEx(of T, U)
... New() methods here
End Class
etc. if more nesting is desired.
Then if an object of type MyThing wants to throw an exception for a timeout condition, it can throw a New GenEx(of MyThing, TimeoutException) which can be caught by a Catch of either GenEx, GenEx(of MyThing), or GenEx(of MyThing, TimeoutException).
Things get a little clunky if MyThing is itself an inherited class; catch statements for GenEx(of MyThingsParent) won't catch a throw of type GenEx(of MyThing). In designing the throw statements for MyThing, however, one could throw a GenEx(of MyThingsParent, MyThing, TimeoutException) [though that would unfortunately only get snagged by a Catch of GenEx(of MyThingsParent) and not one of GenEx(of MyThingsParent, TimeoutException)] or a GenEx(of MyThingsParent, MyThing, TimeoutException) [ignoring the actual type of MyThing].
Has anyone else tried anything at all similar to this approach? With what effects (good or bad)?
Pro: it would facilitate a more useful exception hierarchy based upon the type of object trying to do something than upon the nature of the result.
Con: unless one had a procedure for cataloging the exceptions one was actually using, it could turn into a real mess.
Any thoughts?
|
|
|
|
|
supercat9 wrote: clever idea or bad idea
Or possibly both.
I dunno, I just tried the syntax in C# and got no errors, so it seems valid.
I just haven't yet thought of a use for it.
If you could come up with a situation in which to use it, you may be more convincing.
|
|
|
|
|
I just haven't yet thought of a use for it.
Well, one use is to reduce the amount of copy/pasted code for custom exceptions. Not in and of itself sufficient reason to use a new pattern, though if using a wider range of exceptions is helpful and the reduced copy/paste requirement would encourages such practices, that could be a good thing.
More generally, it seems to me that there are many situations where a routine's caller will sometimes want to know the specifics of something that went wrong, and others where the caller's concern is simply whether or not it worked. For example, if I send a request for information to another machine and I don't get a valid reply, there are a number of things that could have gone wrong (note that the protocol is designed for communicating with an embeded system via serial or TCP, so it includes its own sequence numbers, checksums, etc. to allow for the possibility of dropped or garbled bytes):
- Connection attempt failed at transport layer
- Connection died at transport layer
- No response received
- Garbage packet received
- Valid-seeming packet received, but with wrong sequence number
- Valid packet received indicating remote end received out-of-sequence packet
- Valid packet: "Command unknown"
- Valid packet: "Credentials refused"
- Valid packet: "Command invalid in this context"
- Valid packet, but not the expected type
- Valid packet, but not the expected length
In some cases, if a command/reply exchange fails, it won't really matter why, and it will make sense to just catch a generic 'something went wrong while exchanging packets' exception. In others, different exception causes should be handled differently. At that level (send/receive one packet), basic inheritance would suffice, though copying/pasting code for all the different exceptions would be annoying.
At a higher level, though, the effective meaning of the above errors may depend upon what the system was doing when it occurred. If certain exceptions happen on certain commands, for example, the system should probably try to re-establish synchronization with the remote machine as soon as possible. In other case, that wouldn't be helpful.
Thinking about it, perhaps the best trick wouldn't be to use nested generic exceptions, but rather to take advantage of the 'When' clause in VB.net; if a custom exception class contains an integer with a bitmask enumeration type, then an exception handler could test for any combination of flags without requiring any hierarchical relationship. Maybe that would be the best approach. I wonder if there would be any adverse performance consequences.
|
|
|
|
|
In this particular example, I would use one or maybe two exceptions (one for 1-4, another for 5-10) with an extra field that can store an enum value defining the error that occurred. Unless you find yourself with situations where you want to catch one type of error and not another, there's no need to make different exception types. Even then, you could catch the single exception type, handle it if you can, and rethrow it if you cannot.
|
|
|
|
|
I think this entire thread is an case for "everybody prod microsoft into putting the CLR exception filtering support into c#" :P
|
|
|
|
|
supercat9 wrote: clever idea or bad idea
A finer blend of both. For some cases, it wouldn't be meaningful to have unneeded exception clauses and for code readability purposes, you can club them into generic.
Vasudevan Deepak Kumar
Personal Homepage Tech Gossips
The woods are lovely, dark and deep,
But I have promises to keep,
And miles to go before I sleep,
And miles to go before I sleep!
|
|
|
|
|
Hi,
I have created 5 SSIS packages. All these are password protected. One of the packages is Driver package that calls Other packages based on some settings in the config file.
Till now I was executing these packages using a job in SQL server. But now I want to execute packages from VB.net code.
Please suggest how can I execute SSIS package from VB.NET.
Any sample code would be very helpul.
Thanks In Advance
CodeManiac
xxxxxxxxxx
xxxxxxxxxx
|
|
|
|
|
|
Pick one forum and stick with it. Cross posting won't get you an answer any faster.
only two letters away from being an asset
|
|
|
|
|
Brilliant...thanks for the help Mark.
|
|
|
|
|
imnotso# wrote: Brilliant...thanks for the help Mark.
Your sarcasm aside, he was actually being helpful. If you irritate people on the forums (by cross-posting, for example) then you are not likely to get any help at all. People don't like being pestered.
|
|
|
|
|
Again...very helpful, it was on 2 forums for 30 mins. How very dreadful.
|
|
|
|
|
imnotso# wrote: Again...very helpful, it was on 2 forums for 30 mins. How very dreadful.
It is simply my observation of forum attitudes over many years. Feel free to ignore the advice if you wish, however it won't change general attitudes to certain behaviour.
|
|
|
|
|
Thanks, I saw the problem, removed the other version and now await any advice regarding the original issue. Thanks for your concern.
|
|
|
|
|
Hi,
I am developing desktop application in vb.net using .netframework 2.0.
In this development I am using one DLL which was developed in .netframework1.1. All of sudden i am getting the message as "The Specified module could not found.( The specified module could not be found. ("Exception from HRESULT: 0x8007007E" when i trying to run the DLL related code.
The same code it is working in other mahines.I have same .netframeworks compares to other computers which were application running sucessfully.
Even I did repair of visiual studio 2005 but no use.
Please let me know any thoughts on this problem
Regards,
Santhosh
modified on Thursday, February 5, 2009 3:02 AM
|
|
|
|
|
Hi guys,
Im wondering if its possible to SOMEHOW convert a class that inherits from an abstract base generic class and to convert the subclassed instance back up to it parent??? It seems it can be done for some reason. Here is my scenario:
Public MustInherit Class EntityCollectionBase(Of TEntity As {EntityBase, IEntity})
Public Class BillingPeriodCollection Inherits EntityCollectionBase(Of BillingPeriodEntity)
End Class
NOTE : BillingPeriodEntity inherits from EntityBase (which implements IEntity)
So if i have code such as the following:
Dim bpc As New BillingPeriodCollection()
LoadCollection(CType(bpc, EntityCollectionBase(Of EntityBase))) *** THIS WONT COMPILE DAMN IT!!!
Private Sub LoadCollection(ByVal collection As EntityCollectionBase(Of EntityBase))
End Sub
My question is this : Why doesnt this damn thing compile? All im trying to do is take a specific instance of a generic typed collection, and simply cast it back up since the type being stored is actually a subclass of the parent (ie. BillingPeriod inherits EntityBase).... Just as BillingPeriodCollection inherits EntityCollectionBase....
It would seem VERY viable that this should be done.... For example if i had a generic list of FORD cars and that inherits from CAR, surely its possible to cast a list of FORDS back up to a list of CARS... That way i can then handle all models at the CAR level generically.... which is what im trying to do in my real app based on code above...
|
|
|
|
|
This has to do with the concepts of covariance and contra variance. There are some nice articles explaining how this is being planned for interfaces in the next version of the framework, but right now you can't do that sort of thing.
To see what could happen if you were allowed to do the kind of cast you wanted, I'll show you a simpler example that has the same problem.
Dim myList As New List(Of String)
Private Sub AddToList(ByVal collection As List(Of Object))
collection.Add(New Object())
End Sub
'what should happen when you call this line?
AddToList(CType(myList, List(Of Object))
'or if the previous line works, what about here
Dim lastLength As Integer = myList(myList.Count - 1).Length
The restriction is that the compiler must be able to perform compile-time verification that the only things going into and coming out of a generic class are the generic type specified when the instance was created (as opposed to Arrays, which do run-time verification and throw exceptions instead). This means you cannot convert a List(Of String) into a List(Of Object) because there is no good way for the compiler to know what you will do with the list once you convert it.
|
|
|
|
|
LoadCollection(CType(bpc, EntityCollectionBase(Of EntityBase)))
Why are you casting to a collection of EntityBase? If your goal is to have a LoadCollection function which can handle different things that inherit from type EntityBase, you should use:
Sub LoadCollection(of T as EntityBase)(thing as EntityCollectionBase(of T))
...
End Sub
LoadCollection(of BillingPeriodEntry)(bpc)
That way the compiler will know in the LoadCollection routine that the items of bpc can be acted upon based on their inheritance from EntityBase, but that only items of type BillingPeriodEntry (a subclass of entity base) can be put into the collection.
|
|
|
|
|
Hi all,
I've searched high and low and even found a few code project articles but nothing in C# and nothing that really does what I need which is to write some code to find a particular day in a month given an occurrence (which could be first, second, third, fourth or last) and a day type or name (which could be "weekday", "Monday", "Tuesday" ... "Sunday") and a number of months (1-12).
I'm doing this as part of replicating the Outlook calendar recurrence dialog so users of my app can select custom dates to run reports. So, for example, they might want to run one report on the first weekday of each month, another report on the third Wednesday of every two months and maybe another one on the last Friday of each 3 months.
My only starting position is that I have today's date which will always be the date the report has just run... from that I need to work out any combination of the above so I can then schedule the next occurrence of the report.
I've tried doing it with some sort of formula but nothing I've tried works so at present I'm adding the months to today's date, setting the day to 1 then simply looping though testing each day until I find what I want - but it's pretty clumsy to be honest and getting the last occurrence is pretty tricky too. Are there folks out there that have better math brains than me that would know how to do this in a formula?
All help and suggestions would be very welcome.
Mike
|
|
|
|
|
Hi,
you need the DateTime class. For any given date it allows you:
- to move back to the first of the month (new DateTime(year, month, 1))
- to advance by a number of months (AddMonths)
- to figure out the day of the week (DayOfWeek: 0=sunday,... 6=saturday)
- to advance by a number of days (AddDays), possibly a number depending on the day of week you have
and the day of week you want.
That should suffice to solve whatever problem you have.
Luc Pattyn [Forum Guidelines] [My Articles]
- before you ask a question here, search CodeProject, then Google
- the quality and detail of your question reflects on the effectiveness of the help you are likely to get
- use the code block button (PRE tags) to preserve formatting when showing multi-line code snippets
|
|
|
|
|