|
i'm glad to hear i'm not completely wrong..
this sure have saved me a headache..
|
|
|
|
|
Always at your service...
Regards
Thomas
www.thomas-weller.de
Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning. Programmer - an organism that turns coffee into software.
|
|
|
|
|
Hello everyone,
I have a static class which wraps a file stream object. And I want to implement the Dispose pattern. I define an uninitialize method in the static class which call Dispose explicitly with parameter value true. Here is my code,
My questions are,
1. Normally there should be a destructor in a class which calls Dispose with parameter value false, but for a static class, there is no concept like destructor, how to implement calling Dispose with false parameter?
2. When the Dispose method without parameter will be called?
3. Is it correct to call GC.SuppressFinalize(this)? Since for a static class, there is no "this" object?
4. Correct to define disposed as static field?
5. Are there anything wrong with my code?
static private StreamWriter currentLogStream;
private static bool disposed = false;
public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}
private static void Dispose(bool disposing)
{
if (false == disposed)
{
if (disposing)
{
currentLogStream.Dispose();
}
}
disposed = true;
}
public static void Uninitialize()
{
Dispose(true);
}
thanks in advance,
George
|
|
|
|
|
|
No, leppie! But Dispose(bool) is defined as static in my sample, how could I call it from destructor?
regards,
George
|
|
|
|
|
George_George wrote: But Dispose(bool) is defined as static in my sample, how could I call it from destructor?
If it is static, you cant! But why have it static? It does not really make sense to me
xacc.ide - now with TabsToSpaces support IronScheme - 1.0 beta 1 - out now! ((lambda (x) `((lambda (x) ,x) ',x)) '`((lambda (x) ,x) ',x))
|
|
|
|
|
Sorry, leppie! My mistake before. The class is singleton -- only once instance is created. Here is my whole code. Could you help to review whether my code is correct?
Thanks!
BTW: compile pass, seems we can call static method Dispose(bool) from destructor?
class Logger : IDisposable
{
private static bool disposed = false;
public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}
~Logger()
{
Dispose(false);
}
private static void Dispose(bool disposing)
{
if (false == disposed)
{
if (disposing)
{
currentLogStream.Dispose();
}
}
disposed = true;
}
public static void Uninitialize()
{
Dispose(true);
}
}
regards,
George
|
|
|
|
|
George_George wrote: he class is singleton -- only once instance is created.
So you still have an instance, use it! I think you are abusing the singleton pattern a bit
xacc.ide - now with TabsToSpaces support IronScheme - 1.0 beta 1 - out now! ((lambda (x) `((lambda (x) ,x) ',x)) '`((lambda (x) ,x) ',x))
|
|
|
|
|
|
It still looks fishy to me
Make all those 'static' members, private 'instance' member. As your instance will be a singleton, any instance members of it will also be a singleton (conceptually).
xacc.ide - now with TabsToSpaces support IronScheme - 1.0 beta 1 - out now! ((lambda (x) `((lambda (x) ,x) ',x)) '`((lambda (x) ,x) ',x))
|
|
|
|
|
Thanks leppie,
Could you show me your code please, which you think is better than my current code?
regards,
George
|
|
|
|
|
When you say "destructor" are you talking about writing a finalizer (~ClassName) method? If so, then you generally don't need to do this. Just because you implement Dispose does not mean you need to implement a finalizer.
The Dispose() method is called by another programmer when they are done using your object.
Yes, you normally would call GC.SuppressFinalizer(this) in your Dispose method. In the case of a static class there is no "this" object. Normally static classes do not hold any internal state and would not actually need to do any object disposal of their own. In your case, since this is a singleton class you will have some internal state however you are implementing the dispose pattern because you are managing another disposable class. In this case you actually don't need to implement the full dispose pattern and can get away with doing everything in the simple Dispose() method.
Again, normally the disposed field is an instance field as the pattern really only applies to non-static classes.
There are a few things wrong, ignoring the fact that this is a static class. If it were not a static class, it should be implmented as:
public class StreamWrapper : IDisposable
{
private StreamWriter currentLogStream;
private bool disposed;
public void Dispose()
{
this.Uninitialize();
}
protected virtual void Dispose(bool disposing)
{
if (!disposed)
{
if (disposing)
{
if (currentLogStream != null)
{
currentLogStream.Dispose();
}
}
}
disposed = true;
}
public void Uninitalize()
{
Dispose(true);
GC.SuppressFinalize(this);
}
} Take a look at this article[^] for more details on how to implement dispose properly.
Scott Dorman Microsoft® MVP - Visual C# | MCPD
President - Tampa Bay IASA
[ Blog][ Articles][ Forum Guidelines] Hey, hey, hey. Don't be mean. We don't have to be mean because, remember, no matter where you go, there you are. - Buckaroo Banzai
|
|
|
|
|
Thanks Scott,
1.
"If so, then you generally don't need to do this" -- confused. Do you mean there is no need to call Dispose method in finalizer? If so, I disagree and in MSDN Dispose pattern, in finalizer dispose method is called with false value parameter.
2.
You mentioned "Normally static classes do not hold any internal state and would not actually need to do any object disposal of their own." -- confused. It is common to have a static class hold a member variable which pointed to disposable object instance, like stream. In your points, there is no need to dispose the wrapped disposable object instance?
3.
In my situation, the initialize method has to be static.
I have fixed some issues you mentioned and posted my new code below. Could you help to review whether my code is correct? Any potential issues?
class Logger : IDisposable
{
private static bool disposed = false;
private static StreamWriter logStream;
public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}
~Logger()
{
Dispose(false);
}
private static void Dispose(bool disposing)
{
if (false == disposed)
{
if (disposing)
{
LogStream.Dispose();
LogStream = null;
}
}
disposed = true;
}
public static void Uninitialize()
{
Dispose(true);
GC.SuppressFinalize(l ogStream);
}
}
regards,
George
|
|
|
|
|
1. I am talking about writing a finalizer. In your class this would be a method named ~Logger(). There is generally no need to write a finalizer, however you should always implement IDisposable if your object maintains unmanaged resources, derives from something that implements IDisposable, or otherwise maintains disposable objects.
2. You are confused with the difference between a static class and a singleton. A singleton is a design pattern that ensures there is ever only one instance of the class. Singletons are not static classes. (Your code also does not implement a static class either...simply a "normal" class with static members. There is a difference.)
3. Again, you are confusing a static class and a singleton. In this scenario, you need to forget about using static anything.
Here is a more complete example of using a singleton:
public sealed class Logger : IDisposable
{
private static Logger instance;
private StreamWriter logStream;
private bool disposed;
private static readonly object syncLock = new object();
private Logger()
{
}
public void Dispose()
{
this.Uninitialize();
}
protected virtual void Dispose(bool disposing)
{
if (!disposed)
{
if (disposing)
{
if (logStream != null)
{
logStream.Dispose();
logStream = null;
}
}
}
disposed = true;
}
public void Uninitalize()
{
Dispose(true);
GC.SuppressFinalize(this);
}
public static Logger Instance
{
get
{
lock(syncLock)
{
if (instance == null)
{
instance = new Logger();
}
}
return instance;
}
}
} This shows how you would use the singleton object. The actual class is sealed with a private constructor, so the only way to get it to do anything meaningful is to access it through the static Instance property. This will return a new instance of the Logger class if one hasn't already been created; if one has been created it will return that instance
Logger.Instance.Uninitalize();
Scott Dorman Microsoft® MVP - Visual C# | MCPD
President - Tampa Bay IASA
[ Blog][ Articles][ Forum Guidelines] Hey, hey, hey. Don't be mean. We don't have to be mean because, remember, no matter where you go, there you are. - Buckaroo Banzai
|
|
|
|
|
Hi all,
Any one suggest me how to edit my url and submit the page on button click with out using Response.redirect.
for eg:
www.mysite.com/default.aspx
this is my page, on button click the page should submit and reload, the url should look like www.mysite.com/default.aspx?step=1 (no response.redirect, because i am losing the viewstate even though i set the EnableViewState="true")
Thank you
Know is Drop, Unknown is Ocean
|
|
|
|
|
You'll probably get better answers over in the ASP.NET forum[^].
Simon
|
|
|
|
|
Hello,
What you are asking is to change to url, while doing a postback event.
I'm sorry, this can't happen in a fashionly manner. When you use a redirection (or a hyperlink -- so whenever you modify the url), you will lost the viewstate.
Why is that so? Because viewstate is stored in a form value. It is actually stored into a hidden field of the page. If you change the url, the data in this field is lost because you are causing a page change, not a page postback event.
You should post here what you want to do, we can help you manage the situation in another way.
One suggestion here could be that you use the ViewState object instead of a query string parameter to manage the current step of your wizard.
|
|
|
|
|
Hi All
Im trying to connect to connect to exchange using webservice. Im trying to find out is there any other way to connect to exhcnage rather than the webservice. I wrote the the webservice for exchange but im having any luck connecting to exchange even when i install the certificate (SSL) that is required.
Thanks in advance
|
|
|
|
|
hi
i m using
System.IO.File.Copy()
to copy files. But i want my files should copy more fast. Is there any way to do this.
thanks in advance...
|
|
|
|
|
There is no single alternative that is guaranteed to be faster. You have to take advantage of the specific sitation that you have to find an alternative that uses the hardware more efficiently.
For example, are the drives two separate physical units, so that it makes sense to read and write simultaneously? Do you have a few large files or a lot of small files?
What is the physical connection to the drives? ATA? SATA? USB? Firewire? USB and Firewire have limited bandwidth, so you can't get more than about 30 MiB/s.
Despite everything, the person most likely to be fooling you next is yourself.
|
|
|
|
|
I M copying files from Usb to my hard drive and having both small and large file in the directory.
|
|
|
|
|
Work out what is causing the bottleneck.
1) CPU - Unlikely.
2) Drive speed - possibly.
2a) Read drive speed.
2b) Write drive speed.
3) Transfer speed - possibly.
3a) IDE/SATA?
3b) Sharing the same cable?
3c) Over a network?
3c.i ) Wifi?
3c.ii ) LAN?
3c.iii) Internet?
3c.iv ) Proxy?
3c.iv ) Protocol?
Then improve the bottle neck area.
Assuming you're not transferring over a network, your bottle neck is either drive access speeds, or cable transfer speeds. Assuming the drives aren't sharing the same cable and you are already using SATA, your only option is to buy faster drives.
Simon
|
|
|
|
|
sumit7034 wrote: System.IO.File.Copy()
My guess is M$ would have already written optimized code for file copy there. Maybe you can call system level routines using VC++ to address the issue in a fine-grained level but that would need some research.
-------------------------------------------
It's code that drives you - Shyam
|
|
|
|
|
Hi Guys,
is there an easy way to show a linklable in a messagebox?
Many Thanks,
Phil
|
|
|
|
|
Not sure, but there are many ways to create a custom MessageBox.
Google and see if this[^] can help.
I died as a mineral and became a plant,
I died as plant and rose to animal,
I died as animal and I was Man.
Why should I fear? When was I less by dying?
-- Rumi[^]
My blog
|
|
|
|