473,386 Members | 1,820 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

exchange of object reference that is used along with "using" keyword

Hello all

I have a thread that performs some simple I/O within a loop that runs on
a separate thread. I used the using keyword to ensure the writer's disposal:
using (writer)
{
while (myCondition)
{
if (buffer.Count >= this.bufferThreshold)
{
//do some I/O
}
}
}

As requirements have changed, I have to exchange my StreamWriter from
time to time which happens outside of this loop. I can easily close the
current writer and exchange the writer reference:
public void ExchangeFiles()
{
//pause I/O loop
this.Pause();

//close currently used file
writer.Close();

//replace writer reference
writer = new StreamWriter(...);

//continue I/O loop
this.Continue();
}

So far so good, but I don't know whether the using-statement that was
initialized with the original StreamReader reference still works
properly (or I'm messing things up by exchaning the reference). Unit
tests still run nicely but I would like to hear your opinion on this. I
guess I will be save with a try/finally block but if using is ok, I'll
keep the current solution.

Thanks for your advice

Philipp

Nov 16 '05 #1
4 1316
Philipp,

From what I can see, this is bad practice, because you are unaware of
the state of the reader when you try and use it. Granted, all you are doing
is closing it (which will not be a problem), but if you were going to do
other things, then I would definitely say that this is an issue.

Also, if you are sharing the reader with other methods outside of the
using block, I would not place the reader in the using block, because then
you have to make sure that the reader is not closed, and open a new one if
you want to use it. If anything, I would just create the reader in the
local method and then use that.

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Philipp Sumi" <no****@123456spam.com> wrote in message
news:uL**************@TK2MSFTNGP12.phx.gbl...
Hello all

I have a thread that performs some simple I/O within a loop that runs on
a separate thread. I used the using keyword to ensure the writer's disposal:

using (writer)
{
while (myCondition)
{
if (buffer.Count >= this.bufferThreshold)
{
//do some I/O
}
}
}

As requirements have changed, I have to exchange my StreamWriter from
time to time which happens outside of this loop. I can easily close the
current writer and exchange the writer reference:
public void ExchangeFiles()
{
//pause I/O loop
this.Pause();

//close currently used file
writer.Close();

//replace writer reference
writer = new StreamWriter(...);

//continue I/O loop
this.Continue();
}

So far so good, but I don't know whether the using-statement that was
initialized with the original StreamReader reference still works
properly (or I'm messing things up by exchaning the reference). Unit
tests still run nicely but I would like to hear your opinion on this. I
guess I will be save with a try/finally block but if using is ok, I'll
keep the current solution.

Thanks for your advice

Philipp

Nov 16 '05 #2
Hello Nicholas

Thanks for the fast reply :-)
From what I can see, this is bad practice, because you are unaware of
the state of the reader when you try and use it.
As a matter of fact, I am. I only use the writer within the loop of the
thread. The only other access on the writer is the exchange and before
doing so, I'm flushing the reader and interrupting the I/O thread.

But I agree that this practice may lead to issues - especially if code
changes and I (or any other developer) do not remember the using-part if
work is done on different methods. So I'll change it anyway.
Also, if you are sharing the reader with other methods outside of the
using block, I would not place the reader in the using block


I/O only occurs within the block. The only access outside this loop is
the closing of the current log and the recreation of the writer object.
So if the using statement does not try to access the formerly created
instance, it should technically work.

....but it was rather a theoretical question as I don't know how using
works behind the scene ;-)
Still, things got clearer now - thanks!

Philipp

Nov 16 '05 #3
Philipp Sumi <no****@123456spam.com> wrote:

<snip>
So far so good, but I don't know whether the using-statement that was
initialized with the original StreamReader reference still works
properly (or I'm messing things up by exchaning the reference). Unit
tests still run nicely but I would like to hear your opinion on this. I
guess I will be save with a try/finally block but if using is ok, I'll
keep the current solution.


Along with Nicholas's points:

The using statement creates a new stack variable, so the reference
which was used at the start of the using statement is the one which is
Disposed at the end. Here's a sample to show that:

using System;

class Test : IDisposable
{
static Test x = new Test("First");
string name;

Test(string name)
{
this.name = name;
}

public void Dispose()
{
Console.WriteLine ("Disposing of {0}", name);
}

static void Main()
{
using (x)
{
x = new Test("Second");
}
}
}

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #4
Hello Jon
That was a very nice explanation - thank you :-)

ph.

Nov 16 '05 #5

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

Similar topics

1
by: Mehul Patel | last post by:
Our .Net team have been pondering about using keyword. We are using streams FileStream and BufferedStream. We use using keyword at FileStream, and not BufferedStream which wraps FileStream. So...
4
by: Cybertof | last post by:
Hello, What is the difference between : - adding a new reference to a namespace within the SolutionExplorer (right click, Add Reference...) - adding a new reference with the 'using' keyword in...
10
by: Andreas Müller | last post by:
Hi all, I have a class SwitchCursor, that implements IDisposable. Is it legal to use an object that is not assigned to a reference inside a using statement like this: using(new...
7
by: Willem van Rumpt | last post by:
Hi all, coming from an unmanaged programming background, I took my time to sort out the IDisposable and finalizer patterns. Just when I thought I had it all conceptually neatly arranged, the...
14
by: MuZZy | last post by:
Hi, Lately i've been (and still am) fixing some memory leaks problems in the project i just took over when i got this new job. Among the other issues i've noticed that for localy created objects...
2
by: R.A.M. | last post by:
Hello, I have started larning C# and I have a question concerning "using (...)" keyword. For example: using (SqlConnection connection = new SqlConnection(ConnectionString)) {...
5
by: Shooter4Life8 | last post by:
for some reason when i try to use a "using" statment i get an error saying. "Name 'Using' is not declared." does that mean that i need to inheirit or import from a class? Im trying to use it...
25
by: samjnaa | last post by:
Please check for sanity and approve for posting at python-dev. In Visual Basic there is the keyword "with" which allows an object- name to be declared as governing the following statements. For...
1
by: Mike P | last post by:
I've recently started using the using keyword for my connections, but I have just seen some code where you have a using within a using, which is opening a command : using (SqlConnection _conn =...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
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,...
0
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...
0
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,...
0
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...

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.