using访问数据库时,我有很多块.我想知道 - 如果必须在使用块中抛出异常,是否仍然会处理必要的资源,即使未到达块的末尾?或者我需要在catch块中手动关闭它们吗?
如果线程在通过退出using块释放的对象中运行方法,会发生什么?
例:
using (SomeObject obj = new SomeObject ())
{
obj.param = 10 ;
Thread newThread = new Thread(() => { obj.Work(); });
newThread.Start();
}
...
obj.Work()正在一个新线程上运行,但是obj是一个IDisposable对象,当using块退出时,它通常会被释放.如果线程在使用块结束后继续运行会发生什么?只有在线程完成后,对象才会被释放吗?或者线程会破裂吗?
谢谢.
我们正在研究C#中的编码模式,我们希望在其中使用带有特殊类的"using"子句,Dispose()根据"using"主体是正常退出还是异常退出,其方法会有不同的含义.
据我所知,CLR会跟踪当前正在处理的异常,直到它被"catch"处理程序占用为止.但是,这些信息是否以任何方式公开以供代码访问并不完全清楚.你知道它是否,如果是,如何访问它?
例如:
using (var x = new MyObject())
{
x.DoSomething();
x.DoMoreThings();
}
class MyObject : IDisposable
{
public void Dispose()
{
if (ExceptionIsBeingHandled)
Rollback();
else
Commit();
}
}
Run Code Online (Sandbox Code Playgroud)
这几乎看起来像System.Transactions.TransactionScope,除了成功/失败不是通过调用确定x.Complete(),而是基于using身体是否正常退出.
HttpWebReponse实现IDisposable接口,但为什么没有Dispose方法.它只包含Close方法.using这个类仍然可以使用模式吗?
因此,当退出using块时,using语句会自动调用正在"使用"的对象上的dispose方法,对吗?
但这何时有必要/有益?
例如,假设您有这种方法:
public void DoSomething()
{
using (Font font1 = new Font("Arial", 10.0f))
{
// Draw some text here
}
}
Run Code Online (Sandbox Code Playgroud)
是否有必要在此处使用using语句,因为该对象是在方法中创建的?当方法退出时,不会丢弃Font对象?
或者在方法退出后,Dispose方法是否会在另一个时间运行?
例如,如果方法是这样的:
public void DoSomething()
{
Font font1 = new Font("Arial", 10.0f);
// Draw some text here
}
// Is everything disposed or cleared after the method has finished running?
Run Code Online (Sandbox Code Playgroud) 我有一个方法,里面有一个try/catch/finaly块.在try块中,我声明SqlDataReader如下:
SqlDataReader aReader = null;
aReader = aCommand.ExecuteReader();
Run Code Online (Sandbox Code Playgroud)
在finally块中,手动处理的对象是在类级别设置的对象.因此,在该方法的对象,其实现IDisposable,如SqlDataReader上述,他们得到自动处理的?Close()在aReader执行while循环后调用,以获取阅读器的内容(应该是Dispose()调用的内容Close()).如果没有调用Close(),当方法完成或对象超出范围时,是否会自动关闭/处置此对象?
编辑:我知道这个using声明,但有些情况令我感到困惑.
在我的DAL中,我写这样的查询:
using(SQLConnection conn = "connection string here")
{
SQLCommand cmd = new ("sql query", conn);
// execute it blah blah
}
Run Code Online (Sandbox Code Playgroud)
现在我发现我没有明确关闭SQLCommand对象.现在我知道'using'块将处理SQLConnection对象,但是这还会处理SQLCommand对象吗?如果不是我有一个严重的问题.我必须在SQLCommand上使用'使用'成千上万行代码,或者在数百种方法上执行cmd.Close().请告诉我,如果使用或关闭命令将提供更好的Web应用程序内存管理?
再次重构一些代码.在其中一个ASP.NET页面中看到其中一些内容:
using (TextBox txtBox = e.Row.Cells[1].FindControl("txtBox") as TextBox)
{
}
Run Code Online (Sandbox Code Playgroud)
没有必要处理txtBox,因为它只是对现有控件的引用.并且您根本不希望控制处置.我甚至不确定这是不是有害的 - 就像它似乎要求不恰当地处理基础控制一样(尽管我还没有看到它以这种方式使用的任何不良影响).
为什么C#不允许将变量从using块传递给函数作为ref或out?
这是我的代码:
using (Form s = new Form())
{
doSomthing(ref s);
}
Run Code Online (Sandbox Code Playgroud)
函数在使用块结束之前结束,为什么C#不允许我s作为ref或out参数传递?
我正在浏览WildMagic 5引擎(www.geometrictools.com),其中Vector <>类继承自Tuple <>类,该类具有特定大小的数组,名为mTuple[](由模板参数设置).到目前为止这么好,没什么特别的.但是在Vector类中,我看到以下内容:
protected:
using Tuple<4,Real>::mTuple;
Run Code Online (Sandbox Code Playgroud)
现在我知道该using关键字用于正确继承重载方法.在这种情况下,我总是假设变量可用于派生类而无需键入上面的代码.以上是必要的吗?或者只是为了让事情更清楚?
using-statement ×10
c# ×9
.net ×3
asp.net ×2
idisposable ×2
ado.net ×1
c++ ×1
inheritance ×1
using ×1