如何使用使用 [英] how to use Using

查看:41
本文介绍了如何使用使用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Using来尝试插件可疑的内存泄漏。我已经开始使用dispose来实现它的所有东西,但没有

运气。我有几个关于使用的问题


我可以使用嵌套吗?

我使用它只适用于它作为
参数并没有帮助那些在使用块内变暗的参数。对吗?


我想在结束使用之前我仍然应该关闭并处理sql连接和

关闭文件等?

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?

推荐答案

你可以使用

进行嵌套你可以在连接,文件和流上使用 - 在任何东西上实现IDispose



HTH

Alex


" cj" < cj@nospam.nospamwrote in message

news:ej ************** @ TK2MSFTNGP02.phx.gbl ...
You can nest using
You can use Using on connections, files and streams - on anything
implementing IDispose.

HTH
Alex

"cj" <cj@nospam.nospamwrote in message
news:ej**************@TK2MSFTNGP02.phx.gbl...

我正在使用Using来尝试插件可疑的内存泄漏。我已经开始使用dispose来实现它的所有东西,但没有

运气。我有几个关于使用的问题


我可以使用嵌套吗?

我使用它只适用于作为参数给出的资源

并不会帮助那些在使用区域内变暗的人。对吗?


我想在结束使用之前我仍然应该关闭并处理sql连接并关闭

文件等?
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?



所以在下面这段代码中我应该对sql适配器很好但是关于streamwriter的是什么

?因为我在使用区域内调暗它是否

也保证要清理?


使用Sql2000DesktopDataAdapter作为新的

系统.Data.SqlClient.SqlDataAdapter(" select * from"& sourceTable,

TextBox2.Text)


尝试

Sql2000DesktopDataAdapter.Fill(myds,0,10,sourceTable)

Catch ex As Exception

MessageBox.Show(" Error:"& ex.Message)< br $>
退出Sub

结束尝试

DataGridView1.DataSource = myds.Tables(sourceTable)


Dim sw As New System.IO.StreamWriter(" C:\ test.txt",True)

sw.WriteLine(" TESTING")

sw.Close ()

结束使用


或者我必须这样做吗?


使用Sql2000DesktopDataAdapter As New

System.Data.SqlClient.SqlDataAdapter(" select * from"& sourceTable,

TextBox2.Text),sw As New System.IO.StreamWriter(" C : \\ test.txt",True)


尝试

Sql2000DesktopDataAdapter.Fill(myds,0,10,sourceTable)

Catch ex As Exception

MessageBox.Show(" Error:" &安培; ex.Message)

退出Sub

结束尝试

DataGridView1.DataSource = myds.Tables(sourceTable)


sw.WriteLine(" TESTING")

sw.Close()

结束使用

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 Sql2000DesktopDataAdapter As New
System.Data.SqlClient.SqlDataAdapter("select * from " & sourceTable,
TextBox2.Text)

Try
Sql2000DesktopDataAdapter.Fill(myds, 0, 10, sourceTable)
Catch ex As Exception
MessageBox.Show("Error: " & ex.Message)
Exit Sub
End Try
DataGridView1.DataSource = myds.Tables(sourceTable)

Dim sw As New System.IO.StreamWriter("C:\test.txt", True)
sw.WriteLine("TESTING")
sw.Close()
End Using

Or do I have to do this?

Using Sql2000DesktopDataAdapter As New
System.Data.SqlClient.SqlDataAdapter("select * from " & sourceTable,
TextBox2.Text), sw As New System.IO.StreamWriter("C:\test.txt", True)

Try
Sql2000DesktopDataAdapter.Fill(myds, 0, 10, sourceTable)
Catch ex As Exception
MessageBox.Show("Error: " & ex.Message)
Exit Sub
End Try
DataGridView1.DataSource = myds.Tables(sourceTable)

sw.WriteLine("TESTING")
sw.Close()
End Using
cj wrote:

我正在使用Using来尝试插件可疑的内存泄漏。我已经开始使用dispose来实现它的所有东西,但没有

运气。我有几个关于使用的问题


我可以使用嵌套吗?

我使用它只适用于它作为
参数并没有帮助那些在使用区域内变暗的东西。

对吗?


我想我仍然应该在结束使用之前关闭并配置sql连接和

关闭文件等?
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?


cj,


你怎么知道你有内存泄漏。请不要告诉我你已经在任务管理器中看到了这一点,一看谷歌就会给你

响应的答案,但没有说明。


Cor


" cj" < cj@nospam.nospamschreef in bericht

news:%2 **************** @ TK2MSFTNGP06.phx.gbl ...
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.nospamschreef in bericht
news:%2****************@TK2MSFTNGP06.phx.gbl...

所以在下面这段代码中,我应该对sql适配器很好,但是什么关于

的算盘?因为我在使用区域内调暗它是否

也保证要清理?


使用Sql2000DesktopDataAdapter作为新的

系统.Data.SqlClient.SqlDataAdapter(" select * from"& sourceTable,

TextBox2.Text)


尝试

Sql2000DesktopDataAdapter.Fill(myds,0,10,sourceTable)

Catch ex As Exception

MessageBox.Show(" Error:"& ex.Message)< br $>
退出Sub

结束尝试

DataGridView1.DataSource = myds.Tables(sourceTable)


Dim sw As New System.IO.StreamWriter(" C:\ test.txt",True)

sw.WriteLine(" TESTING")

sw.Close ()

结束使用


或者我必须这样做吗?


使用Sql2000DesktopDataAdapter As New

S. ystem.Data.SqlClient.SqlDataAdapter(" select * from" &安培; sourceTable,

TextBox2.Text),sw As New System.IO.StreamWriter(" C:\ test.txt",True)


试试

Sql2000DesktopDataAdapter.Fill(myds,0,10,sourceTable)

Catch ex As Exception

MessageBox.Show(" Error:" & ex.Message)

退出Sub

结束尝试

DataGridView1.DataSource = myds.Tables(sourceTable)


sw.WriteLine(" TESTING")

sw.Close()

结束使用


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 Sql2000DesktopDataAdapter As New
System.Data.SqlClient.SqlDataAdapter("select * from " & sourceTable,
TextBox2.Text)

Try
Sql2000DesktopDataAdapter.Fill(myds, 0, 10, sourceTable)
Catch ex As Exception
MessageBox.Show("Error: " & ex.Message)
Exit Sub
End Try
DataGridView1.DataSource = myds.Tables(sourceTable)

Dim sw As New System.IO.StreamWriter("C:\test.txt", True)
sw.WriteLine("TESTING")
sw.Close()
End Using

Or do I have to do this?

Using Sql2000DesktopDataAdapter As New
System.Data.SqlClient.SqlDataAdapter("select * from " & sourceTable,
TextBox2.Text), sw As New System.IO.StreamWriter("C:\test.txt", True)

Try
Sql2000DesktopDataAdapter.Fill(myds, 0, 10, sourceTable)
Catch ex As Exception
MessageBox.Show("Error: " & ex.Message)
Exit Sub
End Try
DataGridView1.DataSource = myds.Tables(sourceTable)

sw.WriteLine("TESTING")
sw.Close()
End Using
cj wrote:

>我正在使用Using来尝试插件可疑的内存泄漏。我已经开始使用dispose来实现它的所有功能但没有运气。我有几个关于使用的问题

我可以使用嵌套吗?

我使用它只适用于作为参数给出的资源
并且无法帮助那些在使用区域内变暗的。对吗?

我想在结束使用之前我仍然应该关闭并处理sql连接并关闭
文件等?
>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?



这篇关于如何使用使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆