|
I agree, Vikram.
regards,
George
|
|
|
|
|
Thanks John Simmons!
Can you say more about why do you think of this please -- "obfuscate the intent of code"?
regards,
George
|
|
|
|
|
John Simmons / outlaw programmer wrote: automatic-ness of memory handling
Memory handling is automatic, but resource management (file/DB/Kernel objects etc..) is not (if you don't count finalization). The garbage collector will definitely run when the app is running out of memory, but it doesn't know about other resources. Which is why relying on finalization to clean them up is a bad idea - your app might run out of handles (for example) long before the GC decides to run.
John Simmons / outlaw programmer wrote: Personally, I avoid "using" statements because I think they obfuscate the intent of code.
Really? How else do you close DB connections or file streams?
|
|
|
|
|
S. Senthil Kumar wrote: Really? How else do you close DB connections or file streams?
I'm a programmer - I write all the code.
"Why don't you tie a kerosene-soaked rag around your ankles so the ants won't climb up and eat your candy ass..." - Dale Earnhardt, 1997 ----- "...the staggering layers of obscenity in your statement make it a work of art on so many levels." - Jason Jystad, 10/26/2001
|
|
|
|
|
You never know when the GC will finalize the object. In a lot of cases, that's fine; but often it's important to know when an object is disposed.
In the case of a StreamWriter writing to a file, the file is left open until the StreamWriter is disposed. If you leave that to the GC, the file will be left open for an undefined amount of time. If your app (or another app) tries to write to that file again during that time, it will fail because the file is still in use.
Whenever you "lock" some object (file, TCP port, ...), you have to ensure that you unlock it as soon as possible, so you have to call Dispose.
Also, for some other objects there's a limit on the maximum number of objects that the GC doesn't know about (e.g. Brush/Pen allocate GDI objects, and there's a maximum of 10000 GDI objects per process). It might be possible that you hit the limit if you allocate too many such objects between two GC runs. Explicitly disposing brushes/pens solves that problem (although you already have a serious problem if you allocate that many objects; but you were more likely to run into this issue on Windows 98/ME which allowed much less GDI objects).
Also, there are objects that don't get garbage collected if you don't dispose them - e.g. if an object adds itself as a handler for an event on a long-lived object; it doesn't get garbage collected until the event handler is unregistered by the Dispose method.
|
|
|
|
|
Great Daniel!
I have a further question. If there is exception, and in both exception handler and finally block, I do not call Close/Dispose explicitly, will Dispose/Close be called automatically during exception (e.g. similar to during stack unwinding in C++)? Or we have to wait for GC to call Finalize?
regards,
George
|
|
|
|
|
George_George wrote: If there is exception, and in both exception handler and finally block, I do not call Close/Dispose explicitly, will Dispose/Close be called automatically during exception
No. Unless, of course, the control comes out of the using block before the exception is caught. Why don't you try it and see? (Just put a Console.WriteLine in the Dispose block and another in the finally block.)
Cheers,
Vikram.
The hands that help are holier than the lips that pray.
|
|
|
|
|
Sorry, Vikram!
My English is not good. I think you misunderstand my question from your reply below. I mean if I am not using "using" block, what will happen? Will Dispose be called automatically during stack unwind or something if I do not call it explicitly in exception handler block or finally block?
Vikram A Punathambekar wrote: No. Unless, of course, the control comes out of the using block before the exception is caught. Why don't you try it and see? (Just put a Console.WriteLine in the Dispose block and another in the finally block.)
regards,
George
|
|
|
|
|
No.
Cheers,
Vikram.
The hands that help are holier than the lips that pray.
|
|
|
|
|
Thanks for your clarification, Vikram!
regards,
George
|
|
|
|
|
George_George wrote: will Dispose/Close be called automatically during exception (e.g. similar to during stack unwinding in C++)? Or we have to wait for GC to call Finalize?
Dispose() won't be called automatically on exceptions. You have to do execution in try block, catch all possible exceptions, cleanup in finally. using statement is a syntactic shortcut for this pattern.
|
|
|
|
|
Thanks N a v a n e e t h,
Any comments to here? I write more details about my specific situations there. Even though I am not using "using" and since the traffic is low, I do not meet with any issues.
I would like to learn from you and let you anticipate what issues I will meet with if I do not use "using" in my situation?
http://www.codeproject.com/script/Forums/View.aspx?fid=1649&msg=2561393[^]
regards,
George
|
|
|
|
|
How i can handle Mozilla Browser event in windows application using Dotnet
|
|
|
|
|
I wanted to access methods written in C++ from C# .net .For this i created DLL(dll is win32 library) which contain method and when i add through the add refrence option it gives me following error
"A reference to D:\program\DynamicLibrary\debug\Dynamiclibrary.dll couldnt be added .please make sure
that file is accessible and that it is a valid assembly or COM component. "
Where i builded the DLL successfully even i tried using other way also where i written following code to access method from DLL
[DllImport(@"D:\Program\DynamicLibrary\Debug\DynamicLibrary.dll", EntryPoint="goal")]
public static extern int goal();
public static void Main()
{
program.goal( );
}
}
In that code also i got error saying that couldn't load DynamicLibrary.dll .
can anyone help me out in this .Please tell me what is the procedure to call C++ function from C# .net it will be very usefull .
Any help will be appreciated .Thanks in advance .
|
|
|
|
|
Have you registerred your dll?
Thanks
Laddie
Kindly rate if the answer was helpful
|
|
|
|
|
No i haven't registered it .i only built the DLL successfully .please tell me how to register it ?
|
|
|
|
|
hi
i wrote this code to define new eventArgs named ControlTextColorChangedEventArgs as follow :
class ControlTextColorChangedEventArgs : System.EventArgs
{
private Color _color;
public Color Color
{
get { return _color; }
}
public ControlTextColorChangedEventArgs(Color color)
{
this._color = color;
}
}
and use in my user control as follow :
public partial class UserControl1 : UserControl
{
public delegate void UserEventHandler(object sender, ControlTextColorChangedEventArgs e);
public event UserEventHandler ControlTextColorChanged;
private string _text;
private Color _color;
public string ControlText
{
get { return _text; }
set
{
_text = value;
this.OnPaint(new PaintEventArgs(this.CreateGraphics(), this.ClientRectangle));
}
}
public Color ControlTextColor
{
get { return _color; }
set
{
_color = value;
this.OnPaint(new PaintEventArgs(this.CreateGraphics(), this.ClientRectangle));
this.OnControlTextColorChanged(new ControlTextColorChangedEventArgs(this.ControlTextColor));
}
}
public UserControl1()
{
InitializeComponent();
this._color = Color.Black;
this.ControlText = string.Empty;
}
protected override void OnPaint(PaintEventArgs e)
{
if (this.ControlText != string.Empty)
{
e.Graphics.DrawString(this.ControlText, new Font("tahoma", 10), new SolidBrush(this.ControlTextColor), new PointF(this.Width / 2, this.Height / 2));
}
}
public virtual void OnControlTextColorChanged(EventHandling.ControlTextColorChangedEventArgs e)
{
ControlTextColorChangedEventArgs handler = ControlTextColorChanged;
if (handler != null)
{
handler(this, e);
}
}
}
but the following error has shown me :
Inconsistent accessibility: parameter type 'EventHandling.ControlTextColorChangedEventArgs' is less accessible than method 'EventHandling.UserControl1.OnControlTextColorChanged(EventHandling.ControlTextColorChangedEventArgs)'
where does my problem and how to solve it ?
thanks
|
|
|
|
|
this is because OnControlTextColorChanged is declared public, but the method's argument type isn't.
declaring ControlTextColorChangedEventArgs public will fix.
|
|
|
|
|
thanks buchstaben
my problem was solved.
thanks a lot.
|
|
|
|
|
Hi I am using browser component in my application which is in C#.net 1.1 Windows application
private AxSHDocVw.AxWebBrowser axWebBrowser1;
and i have set all the properties for it
it gives me error as
an error has occured in the script on this page.
Line: 1
Char: 1
Error: Object Expected
Code: 0
URL: http://in.msn.com/default.aspx?froo=www
do you want to continue running scripts on this page?
yes no
I have navigated by
string str1 = ComboAddress.Text;
object tmp = (object)str1;
object null_obj_str = "";
System.Object null_obj = 0;
axWebBrowser1.Navigate2(ref tmp , ref null_obj, ref null_obj, ref null_obj_str, ref null_obj_str);
how do i go about with this problem and i want allow popups also?
can any 1 help?
|
|
|
|
|
in .net 2 webbrowser control, there is a property named "ScriptErrorsSurppressed". maybe there is similar in AxWebBrowser?
|
|
|
|
|
When i tried to update the contents of a table in my database using oledbcommand i am getting the "Could not save; currently locked by another user" exception.
I tried closing the connection other places where ever it is used.
But still the same problem is coming? what should i do? Please help me?
|
|
|
|
|
I assume this is an access db ? How many connections does your code have ? Why does it have more than one ?
Christian Graus
Please read this if you don't understand the answer I've given you
"also I don't think "TranslateOneToTwoBillion OneHundredAndFortySevenMillion FourHundredAndEightyThreeThousand SixHundredAndFortySeven()" is a very good choice for a function name" - SpacixOne ( offering help to someone who really needed it ) ( spaces added for the benefit of people running at < 1280x1024 )
|
|
|
|
|
Thanks for the reply,
Exactly i am using access. Actually my code calls internally some services which are also using the same connection and upto my knowledge they are getting closed as soon as the service use is finished.
Or otherwise is there any way to view all the connections which are open?
|
|
|
|
|
If u r the only one using that db than surely there's a place left in your code where db connection left open.
|
|
|
|