|
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
|
|
|
|
|
Hello all i tried the code below but bot working plz help.....URGENT
string[] drivelist = Environment.GetLogicalDrives();
for (int i = 0; i < drivelist.Length; i++)
{
System.IO.DriveInfo di = new System.IO.DriveInfo(drivelist[i]);
System.IO.DirectoryInfo dirInfo = di.RootDirectory;
System.IO.DirectoryInfo[] dirInfos = dirInfo.GetDirectories("*.*");
foreach (System.IO.DirectoryInfo d in dirInfos)
{
string s = d.Name;
try
{
string[] files = Directory.GetFiles(drivelist[i] + d.Name, "csc.exe",SearchOption.AllDirectories);
int n = files.Length;
if (n == 1)
MessageBox.Show(d.Name);
break;
}
catch (Exception)
{ }
}
}
Tony j sebastian
|
|
|
|
|
tonyjsebastian1 wrote: catch (Exception)
{ }
And how do you expect the code to work?
I guess some directory Permission exception is thrown. Certain directories in Windows drive are forbidden even for Admin.
Use recursion instead of
<br />
Directory.GetFiles(drivelist[i] + d.Name, "csc.exe",SearchOption.AllDirectories);<br />
Wait I answered this question in your previous thread. Did you even care to try that way!
-------------------------------------------
It's code that drives you - Shyam
|
|
|
|
|
tonyjsebastian1 wrote: .....URGENT
Yeah, probably![^]
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
|
|
|
|
|
If you want to get csc.exe, it is located in the Framework directory (usually C:\Windows\Microsoft.NET\Framework\[version]).
You can get that directory by calling System.Runtime.InteropServices.RuntimeEnvironment.GetRuntimeDirectory()[^]
If you want to get the Framework directory for a version different from the one running your application, the operation can be more tricky: you must dllimport GetRequestedRuntimeInfo()[^]
Edit: I have seen just now your double post, if you had waited a little more (and maybe if you did some Google search) you'd get your answer in the right thread in a more clear way...
|
|
|
|
|
hello Mikro1980
let me introduce my self as Tony.
i am very thankful to u Mikro for giving me the answer what i was exactly looking for the past 2 days.
Thanks & Regards
Tony J Sebastian
|
|
|
|
|
hello all
I wish to get the path of the file named csc.exe from the system.
To do this the drives in the system are not known.. so first find the drives(c,d,e etc......) in the system and perform a search???
is thr any alternative exists.... like getting the path from the windows registry.... for the specified file.. by some search
How to implement this using C# code??
Urgent plz HELP ====
TJS
|
|
|
|
|
Yes, you can query Registry to get the path to your current Windows directory. From there, you can move to Microsoft.NET/Framework/v (version number)
I guess you can get both the information (of version number and windows directory) using System.Environment rather than querying the registry. Just check once
tonyjsebastian1 wrote: Urgent plz HELP
If you stopped using that, google would stop indexing us on those words
-------------------------------------------
It's code that drives you - Shyam
|
|
|
|
|
Start with @"%SystemRoot%\microsoft.net\framework\"
Remember that there are several versions of csc.exe and you likely want the newest one.
|
|
|
|
|
Hi,
I developed a windows application.Now after installation it should be shown in system tray beside date & time. Then on the right click of this i have to show some options like enable/disable.
Iam new to c#.Kindly help me on this.
Thanks to all
bye
|
|
|
|