473,802 Members | 1,937 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

how to use Using

cj
I'm looking at using Using to try an plug a suspected memory leak. I've
already started using dispose for everything that implements it but no
luck. I have several questions about Using

Can I nest using?

I take it using only applies to resources that it was given as
parameters and will not help with those dimmed inside a using block. Right?

I guess I'm still supposed to close and dispose sql connections and
close files etc before end using?
Jun 22 '07 #1
13 1387
You can nest using
You can use Using on connections, files and streams - on anything
implementing IDispose.

HTH
Alex

"cj" <cj@nospam.nosp amwrote in message
news:ej******** ******@TK2MSFTN GP02.phx.gbl...
I'm looking at using Using to try an plug a suspected memory leak. I've
already started using dispose for everything that implements it but no
luck. I have several questions about Using

Can I nest using?

I take it using only applies to resources that it was given as parameters
and will not help with those dimmed inside a using block. Right?

I guess I'm still supposed to close and dispose sql connections and close
files etc before end using?

Jun 22 '07 #2
cj
So in this code below I should be good with the sql adapter but what
about the streamwriter? Since I dimmed it inside the using block is it
guaranteed to be cleaned up too?

Using Sql2000DesktopD ataAdapter As New
System.Data.Sql Client.SqlDataA dapter("select * from " & sourceTable,
TextBox2.Text)

Try
Sql2000DesktopD ataAdapter.Fill (myds, 0, 10, sourceTable)
Catch ex As Exception
MessageBox.Show ("Error: " & ex.Message)
Exit Sub
End Try
DataGridView1.D ataSource = myds.Tables(sou rceTable)

Dim sw As New System.IO.Strea mWriter("C:\tes t.txt", True)
sw.WriteLine("T ESTING")
sw.Close()
End Using

Or do I have to do this?

Using Sql2000DesktopD ataAdapter As New
System.Data.Sql Client.SqlDataA dapter("select * from " & sourceTable,
TextBox2.Text), sw As New System.IO.Strea mWriter("C:\tes t.txt", True)

Try
Sql2000DesktopD ataAdapter.Fill (myds, 0, 10, sourceTable)
Catch ex As Exception
MessageBox.Show ("Error: " & ex.Message)
Exit Sub
End Try
DataGridView1.D ataSource = myds.Tables(sou rceTable)

sw.WriteLine("T ESTING")
sw.Close()
End Using
cj wrote:
I'm looking at using Using to try an plug a suspected memory leak. I've
already started using dispose for everything that implements it but no
luck. I have several questions about Using

Can I nest using?

I take it using only applies to resources that it was given as
parameters and will not help with those dimmed inside a using block.
Right?

I guess I'm still supposed to close and dispose sql connections and
close files etc before end using?
Jun 22 '07 #3
cj,

How do you know that you have a memory leak. And please don't tell me that
you have seen that in the taskmanager, one look at Google gives you
thousends of responses that that says nothing.

Cor

"cj" <cj@nospam.nosp amschreef in bericht
news:%2******** ********@TK2MSF TNGP06.phx.gbl. ..
So in this code below I should be good with the sql adapter but what about
the streamwriter? Since I dimmed it inside the using block is it
guaranteed to be cleaned up too?

Using Sql2000DesktopD ataAdapter As New
System.Data.Sql Client.SqlDataA dapter("select * from " & sourceTable,
TextBox2.Text)

Try
Sql2000DesktopD ataAdapter.Fill (myds, 0, 10, sourceTable)
Catch ex As Exception
MessageBox.Show ("Error: " & ex.Message)
Exit Sub
End Try
DataGridView1.D ataSource = myds.Tables(sou rceTable)

Dim sw As New System.IO.Strea mWriter("C:\tes t.txt", True)
sw.WriteLine("T ESTING")
sw.Close()
End Using

Or do I have to do this?

Using Sql2000DesktopD ataAdapter As New
System.Data.Sql Client.SqlDataA dapter("select * from " & sourceTable,
TextBox2.Text), sw As New System.IO.Strea mWriter("C:\tes t.txt", True)

Try
Sql2000DesktopD ataAdapter.Fill (myds, 0, 10, sourceTable)
Catch ex As Exception
MessageBox.Show ("Error: " & ex.Message)
Exit Sub
End Try
DataGridView1.D ataSource = myds.Tables(sou rceTable)

sw.WriteLine("T ESTING")
sw.Close()
End Using
cj wrote:
>I'm looking at using Using to try an plug a suspected memory leak. I've
already started using dispose for everything that implements it but no
luck. I have several questions about Using

Can I nest using?

I take it using only applies to resources that it was given as parameters
and will not help with those dimmed inside a using block. Right?

I guess I'm still supposed to close and dispose sql connections and close
files etc before end using?

Jun 22 '07 #4
cj,
>
How do you know that you have a memory leak. And please don't tell me
that you have seen that in the taskmanager, one look at Google gives
you thousends of responses that that says nothing.

Cor
On a very slightly related point....

Cor... I'm curious... I have observed processes in Taskmanager (in this case
not .Net but Ruby) which appeared to eternally use greater and greater quantities
of memory (presumably)as time goes on.

I can understand that due to garbage collection issues (in either Ruby or
..Net) that taskmanager's absolute concept of memory usage for a given process
can be way off.

However, when memory keeps going up and up and up, it becomes fairly clear
that something is up.

Now granted, I have no Idea if this is what the OP is experiencing, but doesn't
this mean that TaskManager isn't entirely useless in detecting at least the
existence of a problem?

--
Rory

Jun 22 '07 #5
Use nested Using:

Using sw As New System.IO.Strea mWriter("C:\tes t.txt", True)
sw.WriteLine("T ESTING")
sw.Close()
end using

I don't remember if sw is IDisposable, but compiler will tell you

HTH

"cj" <cj@nospam.nosp amwrote in message
news:%2******** ********@TK2MSF TNGP06.phx.gbl. ..
So in this code below I should be good with the sql adapter but what about
the streamwriter? Since I dimmed it inside the using block is it
guaranteed to be cleaned up too?

Using Sql2000DesktopD ataAdapter As New
System.Data.Sql Client.SqlDataA dapter("select * from " & sourceTable,
TextBox2.Text)

Try
Sql2000DesktopD ataAdapter.Fill (myds, 0, 10, sourceTable)
Catch ex As Exception
MessageBox.Show ("Error: " & ex.Message)
Exit Sub
End Try
DataGridView1.D ataSource = myds.Tables(sou rceTable)

Dim sw As New System.IO.Strea mWriter("C:\tes t.txt", True)
sw.WriteLine("T ESTING")
sw.Close()
End Using

Or do I have to do this?

Using Sql2000DesktopD ataAdapter As New
System.Data.Sql Client.SqlDataA dapter("select * from " & sourceTable,
TextBox2.Text), sw As New System.IO.Strea mWriter("C:\tes t.txt", True)

Try
Sql2000DesktopD ataAdapter.Fill (myds, 0, 10, sourceTable)
Catch ex As Exception
MessageBox.Show ("Error: " & ex.Message)
Exit Sub
End Try
DataGridView1.D ataSource = myds.Tables(sou rceTable)

sw.WriteLine("T ESTING")
sw.Close()
End Using
cj wrote:
>I'm looking at using Using to try an plug a suspected memory leak. I've
already started using dispose for everything that implements it but no
luck. I have several questions about Using

Can I nest using?

I take it using only applies to resources that it was given as parameters
and will not help with those dimmed inside a using block. Right?

I guess I'm still supposed to close and dispose sql connections and close
files etc before end using?

Jun 22 '07 #6
"cj" <cj@nospam.nosp amschrieb:
Can I nest using?
Yes. You can even write 'Using a, b, c, d ... End Using' instead of more
than one nested 'Using' block.

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://dotnet.mvps.org/dotnet/faqs/>

Jun 23 '07 #7
On Jun 22, 2:14 pm, cj <c...@nospam.no spamwrote:
I'm looking at using Using to try an plug a suspected memory leak. I've
already started using dispose for everything that implements it but no
luck. I have several questions about Using

Can I nest using?
Sure. Not a problem. You can also put all your IDisposable objects
in one using.
I take it using only applies to resources that it was given as
parameters and will not help with those dimmed inside a using block. Right?
Yep.
I guess I'm still supposed to close and dispose sql connections and
close files etc before end using?
Nope. Close is simply a call to there internal clean up methods. In
other words, there is no difference between calling close and
dispose. Close is just more natural symantically...

--
Tom Shelton

Jun 23 '07 #8
Rory,

I have the idea that Willy has written about this subject the most in dept.

http://groups.google.com/group/micro...kmanager+willy

Have a look at it,

Cor

"Rory Becker" <Ro********@new sgroup.nospamsc hreef in bericht
news:b0******** *************** ***@msnews.micr osoft.com...
>cj,

How do you know that you have a memory leak. And please don't tell me
that you have seen that in the taskmanager, one look at Google gives
you thousends of responses that that says nothing.

Cor

On a very slightly related point....

Cor... I'm curious... I have observed processes in Taskmanager (in this
case not .Net but Ruby) which appeared to eternally use greater and
greater quantities of memory (presumably)as time goes on.
I can understand that due to garbage collection issues (in either Ruby or
.Net) that taskmanager's absolute concept of memory usage for a given
process can be way off.

However, when memory keeps going up and up and up, it becomes fairly clear
that something is up.

Now granted, I have no Idea if this is what the OP is experiencing, but
doesn't this mean that TaskManager isn't entirely useless in detecting at
least the existence of a problem?

--
Rory

Jun 23 '07 #9
cj
Thanks! One little followup. When you say I don't have to call
closing, is that because end using calls the cleanup method?

Tom Shelton wrote:
On Jun 22, 2:14 pm, cj <c...@nospam.no spamwrote:
>I'm looking at using Using to try an plug a suspected memory leak. I've
already started using dispose for everything that implements it but no
luck. I have several questions about Using

Can I nest using?

Sure. Not a problem. You can also put all your IDisposable objects
in one using.
>I take it using only applies to resources that it was given as
parameters and will not help with those dimmed inside a using block. Right?

Yep.
>I guess I'm still supposed to close and dispose sql connections and
close files etc before end using?

Nope. Close is simply a call to there internal clean up methods. In
other words, there is no difference between calling close and
dispose. Close is just more natural symantically...

--
Tom Shelton
Jun 25 '07 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
5719
by: Enos Meroka | last post by:
Hallo, I am a student doing my project in the university.. I have been trying to compile the program using HP -UX aCC compiler, however I keep on getting the following errors. ================================================================= Error 19: "CORBAManagerMessages.h", line 4 # Unexpected 'std'. using std::string; ^^^
3
2167
by: Mike L | last post by:
Should the command call "using" be before or after my namespace? **AFTER** namespace DataGridBrowser { using System; using System.Drawing; using System.Drawing.Drawing2D; using System.Collections;
3
2444
by: xzzy | last post by:
I was wondering why we have to have using System.Data using System.Configuration using etc.... why are they not all lumped into one 'using'? In other words, is there a best way to use project classes with 'using' meaning
14
5807
by: pmud | last post by:
Hi, I need to use an Excel Sheet in ASP.NET application so that the users can enter (copy, paste ) large number of rows in this Excel Sheet. Also, Whatever the USER ENETRS needs to go to the SQL DATABASE, probably by the click of a button. Is this possible? & what is the BEST APPROACH for doing this? & also if any links are there do tell those to me too coz I have no idea how to go about doing it.
8
2416
by: acb | last post by:
Hi, I wrote a DLL Component (using Visual Studio 2005) and managed to include it into a C# Console application. I am now trying to include this component into a Web project. I copy the DLL into the bin directory but am not able to progress. Can anyone please guide me to an online tutorial on the subject. Thanks,
0
2209
by: Metal2You | last post by:
I'm working on an ASP.NET 2.0 application in Visual Studio 2005 that accesses a Sybase database back end. We're using Sybase SQL Anywhere 9.0.2.3228. I have installed and registered the Sybase .NET 2.0 DataProvider (iAnywhere.Data.AsaClient.dll) into the GAC so it can be used in the ProviderName property of a SQLDataSource and loads properly at run time. The application I'm writing is a bit more complex than the example I'm about to...
10
1975
by: mg | last post by:
I'm migrating from VB6 and have a question about using 'Using' and the best way to use it. Here is a example of a small bit of code: dbConx("open") Using CN Dim CMD As New OleDbCommand(sSQL, CN) Dim DR As OleDbDataReader = CMD.ExecuteReader()
0
2577
by: Eugene Anthony | last post by:
The problem with my coding is that despite removing the records stored in the array list, the rptPages repeater control is still visible. The rptPages repeater control displayes the navigation link (1,2,3 so on). The code can be found in SubscriptionCart.aspx.cs. Default.aspx ------------
3
8303
by: JDeats | last post by:
I have some .NET 1.1 code that utilizes this technique for encrypting and decrypting a file. http://support.microsoft.com/kb/307010 In .NET 2.0 this approach is not fully supported (a .NET 2.0 build with these methods, will appear to encrypt and decrypt, but the resulting decrypted file will be corrupted. I tried encrypting a .bmp file and then decrypting, the resulting decrypted file under .NET 2.0 is garbage, the .NET 1.1 build works...
6
5170
by: =?Utf-8?B?U2hhd24gU2VzbmE=?= | last post by:
Greetings! I was researching AJAX to provide a solution to displaying status messages while a long process executed. I found several examples online and was able to use their code to get a quick application working. However, when attempting to implement the solution, the AJAX calls weren't updating the screen like the examples were and seemed not to fire until after the long running process had completed. I found the only real...
0
9699
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9562
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10538
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10305
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
10063
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7598
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5494
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4270
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3792
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.