|
Is it possible for the GC to collect a static variable if it determines that no further references to it exist? For example:
class LockFile {
static FileStream lockFile;
static LockFile ()
{
lockFile = File.Open(".lock", FileMode.OpenOrCreate, FileAccess.Write, FileShare.None);
}
} If the LockFile class is being loaded somewhere in my code, could the file stream be collected before AppDomainUnload?
|
|
|
|
|
AFAIK, static variables are not garbage collected. It will be cleaned up when the application domain unloads.
|
|
|
|
|
|
If the variable is private, and not referenced in the class (except in the static constructor), would this still be the case?
Thank you.
|
|
|
|
|
I'm not sure. But why on earth would you want to do this?
A private static variable that isn't referenced anywhere. What are you using it for?
To be honest, I wouldn't be at all surprised if the compiler just removes it.
Simon
|
|
|
|
|
Well, I'm loading an unmanaged library and maintaining a SafeHandle over it. The handle must be maintained as long as the application is running, and must be released when the application is unloaded. First I thought about using GC.KeepAlive(libHandle) in some public static methods. But I'm wondering whether I really need to call GC.KeepAlive .
Thanks for your help.
|
|
|
|
|
Ahh, I see what you are trying to do.
My assumption would be that static variables do not get collected. Even if you don't currently have any references too them, because at any point, you could have a reference by using them for something as they are accessible from everywhere.
But, like I said, if private and it's not used anywhere in the class, I wouldn't be all the surprised if the compiler just removed it.
You're probably going to struggle to get an answer to this. It's quite specific. Possibly try asking on the MSDN forum for the CLR, perhaps someone in MS could give you an answer.
Alternatively, I believe the GCHandle class can be used to track object lifetime, so you could test it out.
Good luck.
Simon
|
|
|
|
|
Thanks a lot! I'll try to check with MSDN.
Meanwhile, I made this workaround, tried stopping it from within the debugger (to simulate AppDomainUnload), and it worked OK. I'm not sure how safe it is, but it seems to be working fine.
class MyApp {
private static SafeHandle handle = new MyAppSafeHandle();
public static KeepAlive() { GC.KeepAlive(handle); }
}
class MyLibSafeHandle : SafeHandle {
public MyLibSafeHandle()
: base(IntPtr.Zero, true)
{ MyApp.KeepAlive(); }
protected override ReleaseHandle() {
externRelease(handle);
MyApp.KeepAlive();
}
}
|
|
|
|
|
Hey there!
I'm kinda new to C sharp so if what I'm asking is obvious, then I'm sorry.
I'm trying to use "WebRequest.RequestUri" to get the URI my program is currently crawling, it works fine as i am able to write out the URI to the console, but when i try to save the URI to a variable it goes wrong.
The code:
HttpWebRequest fetchpage = (HttpWebRequest)HttpWebRequest.Create(url);
HttpWebResponse fetchinfo = (HttpWebResponse)fetchpage.GetResponse();
//It is here it goes wrong
string URL = fetchinfo.ResponseUri;
The error: "Cannot implicitly convert type 'System.Uri' to 'string'"
How do i get it to save the URL to a variable?
|
|
|
|
|
string URL = fetchinfo.ResponseUri.ToString(); /ravi
|
|
|
|
|
ResponseUri is type of Uri class so you can't assign it to string. If you need string representation of Uri instance either call ToString() on it or use properties provided by Uri class.
|
|
|
|
|
Just use .ToString()
Good Luck
Prateek
|
|
|
|
|
|
i want to copy a file from a location to some specific location in c#?
|
|
|
|
|
Have you tried
System.IO.File.Copy("Source", "Destination"); Docs on MSDN[^]
Simon
|
|
|
|
|
Simon Stevens wrote: System.IO.File.Copy("Source", "Destination");
Hey I tried it but I got FileNotFoundException. Can u plz e-mail me full project at troll@hotmail.com ? It's very urgntz. Thanks
|
|
|
|
|
|
You should have guessed that it's not he because I said 'Thanks' while he uses[^] 'thx'
|
|
|
|
|
I spotted that. A dead give-away.
7r01z can't write out the word "thanks" without bursting. It's like trolls and sunshine...
Despite everything, the person most likely to be fooling you next is yourself.
|
|
|
|
|
|
Giorgi Dalakishvili wrote: Will it work?
Probably not.
Simon
|
|
|
|
|
thx budy, its working
Maifs
|
|
|
|
|
You need to copy a file so why don't you Google or search msdn? Is it so difficult?
|
|
|
|
|
no, i didnt find it,
thx
Maifs
|
|
|
|
|