using-statement

returning in the middle of a using block

会有一股神秘感。 提交于 2019-12-17 04:54:49
问题 Something like: using (IDisposable disposable = GetSomeDisposable()) { //..... //...... return Stg(); } I believe it is not a proper place for a return statement, is it? 回答1: As several others have pointed out in general this is not a problem. The only case it will cause you issues is if you return in the middle of a using statement and additionally return the in using variable. But then again, this would also cause you issues even if you didn't return and simply kept a reference to a

Return a Disposable object for use in a using block

旧城冷巷雨未停 提交于 2019-12-14 03:57:25
问题 How do I return a disposable object in my function to ensure that it works properly within a using block? In my function, I want to act on the disposable object and also account for errors, which complicates this. I have something similar to the following code so far: DBHandle GetDB() { /* // I can't do this, because the using block would dispose of my object within this function using( var db = DatabaseObj.GetHandle() ) { db.Open(); return db; } */ var db = DatabaseObj.GetHandle(); try { db

using statement on IDisposable object - delay of calling Dispose method

我是研究僧i 提交于 2019-12-12 08:47:43
问题 As describe this article, about usage of using on IDisposable objects, it says one interesting words: ...using block, the Dispose method is automatically called sometime after the block ends. (It may not be immediate; it depends on the CLR.) Interesting here is " It may not be immediate; it depends on the CLR ". Can anyone provide more details on this? Because we have some strange situations where it seems that on code using(new MyDisposable()) {...}, after end of block } it does NOT

HttpClient in using statement causes Task cancelled

你。 提交于 2019-12-12 07:14:04
问题 I created a FileResult : IHttpActionResult webapi return type for my api calls. The FileResult downloads a file from another url and then returns the stream to the client. Initially my code had a using statement like below: public async Task<HttpResponseMessage> ExecuteAsync(CancellationToken cancellationToken) { try { HttpResponseMessage response; using (var httpClient = new HttpClient()) { response = new HttpResponseMessage(HttpStatusCode.OK) { Content = new System.Net.Http.StreamContent(

Bitmap and picturebox cause out of memory exception

拟墨画扇 提交于 2019-12-12 02:15:17
问题 I am trying to create an application that shows the online trains in picturebox . but my application spends a lot of memory and sometimes i got the Out of memory exception and sometimes my trains Disappears from the picturebox . To draw online train first time i draw the map of trains (lines ,stations ,...) on picturebox with size x=A and y=b after that i create another picturebox with the same size and put the second picturebox on first picturebox using this code: pictureBoxonlineTrain

C#: “using” directive and try/catch block

♀尐吖头ヾ 提交于 2019-12-11 20:19:10
问题 I know how to use try/catch block in case of database calls and know how to use "using" directive in context of using try/finally construction as well. But, can I mix them? I mean when I use "using" directive can I use try/catch construction as well because I still need to handle possible errors? 回答1: Of course you can do it: using (var con = new SomeConnection()) { try { // do some stuff } catch (SomeException ex) { // error handling } } using is translated by the compiler into a try.

Using statement with directoryservices

南笙酒味 提交于 2019-12-11 14:53:47
问题 Could you help me and tell if im using the "using statement" correctly in my directoryservice function that gets distingushed name from my Active Directory. I want to dispose and close objects correctly. Code: Public Function GetObjectDistinguishedName(ByVal objClass As objectClass, _ ByVal returnValue As returnType, _ ByVal objName As String, ByVal LdapDomain As String, _ Optional ByVal includeLdapPrefix As Boolean = True) As String Dim distinguishedName As String = String.Empty Dim

using a SqlConnection in C++ / .NET

社会主义新天地 提交于 2019-12-11 10:59:34
问题 When I go to the MSDN page for the SqlConnection class, it only shows examples in C# and VB. Why doesn't MSDN show a C++ example? I am particularly interested in how a C++ example would get around the lack of the using keyword in C++. 回答1: Hmmm... after reading What is the Managed C++ equivalent to the C# using statement it seems that the equivalent of: using (SqlConnection connection = new SqlConnection(connectionString)) { // SqlCommand and so... } is actually: { SqlConnection conn

Multiple using block c#

試著忘記壹切 提交于 2019-12-11 01:06:31
问题 I am working on application in which i need to access database. Use of using statement is good because "using" statement is to ensure that the object is always disposed correctly, and it doesn't require explicit code to ensure that this happens. So i am little bit confused where to use "using" and where to not. public int route(Route r) { try { using (SqlConnection con = new SqlConnection(connectionString)) { using(SqlCommand com = new SqlCommand("",con)) { using (SqlDataReader sdr = com

Are all disposable objects instantiated within a using block disposed?

喜欢而已 提交于 2019-12-10 12:40:56
问题 This is a question I have asked myself many times in the past as I nested using statements 5 deep. Reading the docs and finding no mention either way regarding other disposables instantiated within the block I decided it was a good Q for SO archives. Consider this: using (var conn = new SqlConnection()) { var conn2 = new SqlConnection(); } // is conn2 disposed? 回答1: No they are not. Only the set of variables explicitly listed in the using clause will be automatically disposed. 回答2: Obviously