|
I went for a job interview a while ago and failed on this question. I have researched it but can not find a definitive answer. Can someone point me to definitive documentation which clearly explains disposing of objects which have subscribed or registered events.
If the GC attempts to collect ClassA and finds it subscribes to an event of ClassB. Will ClassA be removed from memory?
If the GC attempts to collect ClassB and finds ClassA subscribes to an event of ClassB. Will ClassB be removed from memory?
I was not aware of this issue but the person interviewing me seemed concerned. At the end of the day, I don't care, I'd just like to find out what the truth is regarding eventing and disposing of objects.
"You get that on the big jobs."
|
|
|
|
|
The question to ask yourself is: who is referencing whom?
In both cases it is classB's event member that holds a reference to classA.
Robert Croll wrote: If the GC attempts to collect ClassB and finds ClassA subscribes to an event of ClassB. Will ClassB be removed from memory?
the subscription does not influence the life of classB, so it can be collected if it isn't alive anymore.
Robert Croll wrote: If the GC attempts to collect ClassA and finds it subscribes to an event of ClassB. Will ClassA be removed from memory?
the subscription keeps classA alive for as long as classB is alive.
Luc Pattyn [Forum Guidelines] [My Articles] Nil Volentibus Arduum
Please use <PRE> tags for code snippets, they preserve indentation, improve readability, and make me actually look at the code.
|
|
|
|
|
Robert Croll wrote: If the GC attempts to collect ClassA and finds it subscribes to an event of ClassB. Will ClassA be removed from memory?
If the GC attempts to collect ClassB and finds ClassA subscribes to an event of ClassB. Will ClassB be removed from memory?
That depends. Can they be reached by following references from "any" variable? If not, both will be removed from memory - if they're not static classes, that is
Robert Croll wrote: I was not aware of this issue but the person interviewing me seemed concerned
..next time, ask for the answer and why he choose that particular question.
It wouldn't make much sense to keep the answer a secret; you can find a huge amount of documentation with Google - it would merely prove that you interviewer knows how to differentiate between a correct answer (like Luc's) and a technical correct but completely useless answer (like mine).
..and you wanna know why he choose that question. Did they have a history of leaking things? It puts the interviewer in a defending position, giving you a few seconds to breathe and to collect your mind
I are Troll
|
|
|
|
|
Thanks Eddy,
Both you and Luc have helped. The reason I asked was you also find a lot of misinformation on Google as well. But you have pointed me in the right direction. I think I'll be implementing weak events in future.
BTW I like your approach to interview questions. I'll have to remember that
"You get that on the big jobs."
|
|
|
|
|
This is the reason if u register explicit events handlers at start , you should release them at end.
|
|
|
|
|
Im trying to work with the Listview groups, and I added of course my groups, but im curious why "Default" keeps showing at the top, anyone know why?
|
|
|
|
|
Herboren wrote: im curious why "Default" keeps showing at the top
Probably because it's not part of the Groups collection;
All items should be assigned to groups before they are displayed. Any items that are not assigned to a group will appear in the default group, which has the header label "DefaultGroup{0}". The default group is not contained in the Groups collection, and cannot be altered. It is primarily useful in debugging to ensure that all items have been properly added to groups.
I are Troll
|
|
|
|
|
Hi Experts,
in an application I have business objects with properties, some of which are enumerations. To edit those properties, I inserted a PropertyGrid.
Now I have to localize the UI and therefore the enumeration name and its values, too.
The enumeration name already works well implementing ICustomTypeDescriptor.
For the enumeration values, the tool of choice seems to be deriving a class from TypeConverter. I did so and overrode
- CanConvertFrom
- ConvertFrom (just calling base class, at the moment)
- CanConvertTo
- ConvertTo
The initial value now is translated as requested. But the DropDown list doesn't drop down any longer. There is no small arrow pointing down when clicking on that initial value, as I was used to.
Do I have to override yet another method (something like GetMeThosePossibleValues(), maybe)?
Or does anyone have another idea?
Ciao,
luker
-- Modified Friday, February 4, 2011 6:11 AM
|
|
|
|
|
lukeer wrote: But the DropDown list doesn't drop down any longer.
Did you decorate your property with the TypeConverter-attribute, pointing to your customized converter?
I are Troll
|
|
|
|
|
Yes, I did. And since then it is that the standard entry is being translated but the arrow button indicating a drop down list is missing.
I can get the arrow button back by decorating with an additional UITypeEditor. That UITypeEditor's GetEditStyle() method is to return UITypeEditorEditStyle.DropDown regardless of its context.
But it won't drop down anyways even with that arrow button visible.
And another dropdown, this one without TypeConverter doesn't drop down either. Since this sounds confusing even to me, maybe the following table can help:
| UITypeEditor | No UITypeEditor
-----------------+----------------------------------+-----------------------------
TypeConverter | Arrow Button without | No arrow button
| function | at all
-----------------|----------------------------------+-----------------------------
No TypeConverter | Arrow Button without | DropDown works, but
| function (Function can | without Translation
| be achieved by |
| UITypeEditor.GetEditStyle() |
| returning |
| UITypeEditorEditStyle.None) |
Can anyone please explain this behaviour?
Ciao,
luker
|
|
|
|
|
lukeer wrote: Yes, I did. And since then it is that the standard entry is being translated but the arrow button indicating a drop down list is missing.
Aight, we'll need some code. Can you post a short snippet that displays this behavior?
lukeer wrote: but the arrow button indicating a drop down list is missing.
Sounds like it recognizes the GetStandardValuesSupported (returning true, to indicate that the converter has a list with options to choose from) and not the GetStandardValues method that provides the actual list. If you post your converter here, we'll have a look
lukeer wrote: I can get the arrow button back by decorating with an additional UITypeEditor. That UITypeEditor's GetEditStyle() method is to return UITypeEditorEditStyle.DropDown regardless of its context.
I'd remove the code for the UITupeEditor and focus on the TypeConverter. You should be able to get a dropdown-list using the TypeConverter, if the TypeConverter can be found and the correct property is decorated.
lukeer wrote: And another dropdown, this one without TypeConverter doesn't drop down either.
That sounds logical; nor the UITypeEditor nor the TypeConverter can give it a list with values, hence a dropdown would always be empty.
In the meantime, you might want to check out the walkthrough on MSDN[^]. The second example sounds like what you're building, you might find something if you compare it with your own code.
I are Troll
|
|
|
|
|
Here is my TypeConverter code. As you will see neither GetStandardValuesSupported nor GetStandardValues are overridden yet. Would that be necessary?
public class TranslationTypeConverter : System.ComponentModel.TypeConverter
{
public override bool CanConvertTo(ITypeDescriptorContext context, Type destinationType)
{
if (destinationType.Equals(typeof(string)))
return (true);
return base.CanConvertTo(context, destinationType);
}
public override object ConvertTo(ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, Type destinationType)
{
if (destinationType.Equals(typeof(string)))
{
Type typeToConvert = context.PropertyDescriptor.PropertyType;
string tableName = typeToConvert.DeclaringType.FullName;
System.Resources.ResourceManager rm = new System.Resources.ResourceManager(tableName, typeToConvert.Assembly);
string key = typeToConvert.Name + "_" + value.ToString();
string convertedString = rm.GetString(key);
if (
convertedString == null
|| convertedString.Length <= 0
)
convertedString = value.ToString();
return (convertedString);
}
return base.ConvertTo(context, culture, value, destinationType);
}
public override bool CanConvertFrom(ITypeDescriptorContext context, Type sourceType)
{
if (sourceType.Equals(typeof(string)))
return (true);
return base.CanConvertFrom(context, sourceType);
}
public override object ConvertFrom(ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value)
{
return (base.ConvertFrom(context, culture, value));
}
}
Ciao,
luker
|
|
|
|
|
lukeer wrote: As you will see neither GetStandardValuesSupported nor GetStandardValues are overridden yet. Would that be necessary?
Yup. The first one explains that it has default values (should return 'true') and that it requires a dropdown-arrow to open the list with those default values. The second method should provide that list with values.
Without those methods, the typeconverter won't show a dropdownlist, as it doesn't know what to display
I are Troll
|
|
|
|
|
What I don't understand is that by not overriding GetStandardValuesSupported and GetStandardValues I suspect the base class to take care of them.
Why doesn't the base class make PropertyGrid show a drop down list while without applying my derived class at all it works?
Ciao,
luker
|
|
|
|
|
lukeer wrote: What I don't understand is that by not overriding GetStandardValuesSupported and GetStandardValues I suspect the base class to take care of them.
If you want a dropdownlist, you'll need to override them. Check the example on the MSDN-page that I linked.
lukeer wrote: Why doesn't the base class make PropertyGrid show a drop down list while without applying my derived class at all it works?
Because a property-grid is intelligent, and it knows that it should use an EnumConverter[^] if nothing is provided
I are Troll
|
|
|
|
|
Thanks a lot.
With overriding the following 6 methods
GetStandardValuesSupported()<br />
GetStandardValues()<br />
CanConvertFrom()<br />
ConvertFrom()<br />
CanConvertTo()<br />
ConvertTo()
everything works as expected.
Ciao,
luker
|
|
|
|
|
Folks
Engaging in a project as a newbie in soundscan, encountered problems while implementing this simple piece of code. It's my appreciation if you lead me through this dense un-documented forest by microsoft !!
P.S.1 : Please introduce me a cook-book or any useful tutorial or documentation in directsound what i couldn't find it anywhere!!!
P.S.2:You can also find the code here:http://www.4shared.com/get/j0J8g6wA/SSM-VBNET-DirectSound.html[^]
Tnx .
#Region "References"
Imports Microsoft.DirectX
Imports Microsoft.DirectX.DirectSound
Imports System.Threading
#End Region
Friend Class frm_main
...
#Region "Command Button"
Private Sub cmd_capture_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmd_capture.Click
Try
If Not devices_waveformat() Then Return
DSBuffer.Start(False)
Thread.Sleep((DSSamples / DSFrequency) * 1000)
'Containing Input Data
Dim DSArray As Array = DSBuffer.Read(0, Type.GetType("Integer"), LockFlag.FromWriteCursor, DSFormatArray)
DSBuffer.Stop()
Catch ex As Exception
Debug.WriteLine(ex.Message)
Debug.WriteLine(ex.ToString)
End Try
End Sub
#End Region
...
End Class
|
|
|
|
|
A quick internet search for this gave me over 25,000 hits.
If you cannot find anything in that lot to help you, how on earth do you expect anyone here to be able to?
Henry Minute
Do not read medical books! You could die of a misprint. - Mark Twain
Girl: (staring) "Why do you need an icy cucumber?"
“I want to report a fraud. The government is lying to us all.”
I wouldn't let CG touch my Abacus!
When you're wrestling a gorilla, you don't stop when you're tired, you stop when the gorilla is.
|
|
|
|
|
Hello everyone.
Sorry for my english but it isn't my first language. I'm goint to try to explain the best as i can.
My Problem is:
I'm building a project using entity framework. The data base is generate by a edmx(database project). To implement business logic i use stored procedures, but i only con map the to parent entity funcions. I can't map stored procedures to an associated table.
Thanks
|
|
|
|
|
You might stand more chance of getting an answer if you ask this in the Database Forum[^].
If you do, point out that you have asked it here already but are asking there because I suggested that you should. (Otherwise someone might shout at you. )
Henry Minute
Do not read medical books! You could die of a misprint. - Mark Twain
Girl: (staring) "Why do you need an icy cucumber?"
“I want to report a fraud. The government is lying to us all.”
I wouldn't let CG touch my Abacus!
When you're wrestling a gorilla, you don't stop when you're tired, you stop when the gorilla is.
|
|
|
|
|
|
I am trying to add the Application version (not the Installer version) to the
Welcome box using CustomActionData in VB.NET to create a variable in a field
such as /MY_CA="[MyCustomVersion]", but need to know how to extract and display
such a value at install time, ie where would the following line go?
Context.Properties.Item("MY_CA") = "Version 4002"
in order to populate the first line in the Welcome text property such as
"...will guide you through the installation of[ProductName] [MyCustomVersion]"
|
|
|
|
|
There is a debate going on at work right now. Basically we are going to be creating some new tier objects; for the sake of simplicity, just assume basic stuff like a business tier, data tier, etc). The business tier will have objects like a Orders object (that will return order info) a customer object (that will return customers), etc, etc.
Is it typical for someone to want to create tier objects as an executable vs. using .dlls? What would be the pros to using an executable in a tier instead of a .dll?
Also, can you pass back collections, datasets, etc from an .exe to another .exe? Admittedly, I've never tried.
The proposed architecture:
Client.exe
would call a tierobject.exe
would call a datalayer.dll
I can think of many reasons why I would think doing this would be a bad idea, but I am also trying to remain open-minded.
Thanks for any insights to the gurus out here.
"There's no such thing as a stupid question, only stupid people." - Mr. Garrison
|
|
|
|
|
Your business model objects will be in a separate project. As an stand alone exe, it will have nothing to offer. No Main method, no UI etc. I see no point in making it an exe.
Jon_Boy wrote: Also, can you pass back collections, datasets, etc from an .exe to another .exe? Admittedly, I've never tried
Yes. If they are public or are returned by some public method yes. You will just need to add reference to that exe in calling project.
|
|
|
|
|
With .NET, you can use an EXE just like a DLL... So there's nothing stopping you from doing it this way.
Of course, there doesn't seem to be much of a point to it, in your situation.
I actually did something like that, but my situation was a bit different... It went something like this:
ScriptingInterface.exe
Application.exe
BusinessLayer.dll
...
For normal use, you would just run the Application.exe... But there were certain tasks, intended to run once a day or in special situations, that were located in the Application.exe (Though they mostly wrapped routines and models from the business layers). Initially, they were being triggered from menu options, but eventually we wanted to automate them by hooking them up to a scheduler...
So basically, the ScriptingInterface.exe would reference Application.exe, and use command line arguments to perform one of those menu-driven tasks, without actually opening up the GUI.
C:\> ScriptingInterface.exe Maintenance DailyRoutines RunSomething
translated to...
namespace My.Namespace.Maintenance
{
public static class DailyRoutines
{
public static void RunSomething()
{
}
}
}
So, there are scenarios where you might want to use an EXE like a library, but for a business layer? Probably no point.
|
|
|
|
|