|
I have a procject in which i am using a custom webclient.
I have following event _WebClient.DocumentComplete which i can register and it works.
Now if I want to unregister the first event handler and assign a new event handler I gives me error because I can't use
_WebClient.DocumentComplete = null;
Can some 1 tell me how to unregister the event
o O º(`'·.,(`'·., ☆,.·''),.·'')º O o°
»·'"`»* *☆ t4ure4n ☆* *«·'"`«
°o O º(,.·''(,.·'' ☆`'·.,)`'·.,)º O o°
|
|
|
|
|
When you add an event handler you use +=
to remove one you use -=
_WebClient.DocumentComplete -= new EventHandler(yourEventHandlerNameHere);
Hope that helps.
Ben
|
|
|
|
|
Hi,
you can undo
someEvent+=new SomeEventHandler(myHandler);
with the following magical line:
someEvent-=new SomeEventHandler(myHandler);
The magic is you create a new object to remove an old (but very similar) object !
|
|
|
|
|
Luc Pattyn wrote: The magic is you create a new object to remove an old (but very similar) object !
I'd always thought you needed to save the old one to unregister it. Learn something new everyday.
How does this work if buggy code results in the same handler being registered twice?
--
You have to explain to them [VB coders] what you mean by "typed". their first response is likely to be something like, "Of course my code is typed. Do you think i magically project it onto the screen with the power of my mind?" --- John Simmons / outlaw programmer
|
|
|
|
|
Dont know, havent seen it spec'd, so I can only suggest you try it.
I can confirm if you register twice, you execute twice (that is a common bug, that
harms performance and is not always apparent).
|
|
|
|
|
dan neely wrote: How does this work if buggy code results in the same handler being registered twice?
Its ugly. You basically get the same handler invoked twice on that event.. I've had to track down so many of these 'event leaks'..
|
|
|
|
|
Hi Dan,
I ran a little test:
private int clicks=0;
private int handlers=0;
Button btn=new Button();
public override void Run(int arg) {
doClick();
addHandler();
doClick();
addHandler();
doClick();
removeHandler();
doClick();
removeHandler();
doClick();
removeHandler();
doClick();
}
void doClick() {
log("CLICK");
btn.PerformClick();
}
void addHandler() {
handlers++;
log("ADD HANDLER "+handlers);
btn.Click+=new EventHandler(btn_Click);
}
void removeHandler() {
log("REMOVE HANDLER "+handlers);
handlers--;
try {btn.Click-=new EventHandler(btn_Click);
} catch { log("failed to remove handler"); }
}
void btn_Click(object sender, EventArgs e) {
clicks++;
log(" got click "+clicks);
}
and it resulted in the following log:
15.109 CLICK
15.125 ADD HANDLER 1
15.140 CLICK
15.156 got click 1
15.203 ADD HANDLER 2
15.218 CLICK
15.296 got click 2
15.312 got click 3
15.328 REMOVE HANDLER 2
15.343 CLICK
15.343 got click 4
15.437 REMOVE HANDLER 1
15.468 CLICK
15.484 REMOVE HANDLER 0
15.593 CLICK
So each -= effectively removes exactly one matching delegate if it finds one,
and does not throw otherwise.
|
|
|
|
|
Hi Dan,
FYI: I just finised turning this subject into a short article. You may want to skim it.
Regards
|
|
|
|
|
Hello Luc,
Luc Pattyn wrote: The magic is you create a new object to remove an old (but very similar) object !
I remember that Scott Dorman once pointed out that it is even possible without creating an new object (I think only in .Net>1.1)
Here is the thread![^]
P.S.: This was my Nr.:1000 post.
All the best,
Martin
|
|
|
|
|
Hi Martin,
I did remember that thread; I havent tried it yet, I still tend to write code that
runs also on 1.1
Martin# wrote: This was my Nr.:1000 post
Aha, that explains a few things. Now take that holiday !
|
|
|
|
|
Luc Pattyn wrote: Aha, that explains a few things. Now take that holiday !
All the best,
Martin
|
|
|
|
|
Have you tried using
_WebClient.DocumentComplete -= <your assigned="" event="" handler="" to="" be="" removed="">;
Is that what you are talking about?
|
|
|
|
|
I have a quick question.
I have a program, and I need every instance of the program to have it's own uniqure ID. (E.G the first one opened has and ID of 12345, the second one has an ID of 74982, etc.)
At the moment the only way I can see of doing this is getting the process ID.
Is this the best way of doing it or are there other, better, ways of generating a unique ID?
- Munty
|
|
|
|
|
Why not just use the random number generator and use the current system time as the seed generator. This ought to guarantee a unique sequence of numbers all the time. Or generate a GUID
|
|
|
|
|
Process ID is just fine; it is unique in that no two processes can be running simultaneously
on the same Windows PC unless their processIDs are different.
Why bother to try and come up with some other unique number ?
|
|
|
|
|
yup,if it is working with single process class which gurantee to be unique, then y to use time or random class.
Regards
Chintan
www.visharadsoft.com
(Nothing is so purify as KNOWLEDGE)
|
|
|
|
|
Alrighty then, I guess I'll just stick with the Process ID.
Thansk All.
- Munty
|
|
|
|
|
Apologies in advance that may not be a C# issue, but perhaps .NET, NTFS, XP-SP2 or even hardware. You tell me.
I have sucessffuly ran breath-first and depth-first versions of the following C# snippet on different hardware as a precursor to noodling around with databases, SQLite, etc. Seems simple enough. Makes 1M diretcories as in D:dataset/II/JJ/KK where II, JJ, and KK are are 100 nodes each. On my developmemt machine (xp-sp2, 2GB, stable) I have this repeatable condition where when I get to about i = 67 (670K dirs created) the program slows down and then the system becomes unresponsive enough that I must reboot! No chance to use the VS debugger.
I quick format the D: drive before each operation, so the dir construction in this program is being performed on a clean FS with a new MFT. I watch the memory usage (doesn't use much). I've monitored the heap (doesn't use much). I'm perplexed with this system-specific issue; as it is one of the few times (for me) that a user-space program has caused so much trouble. Thoughs on what to try next appreacited. Or pointers to a more appropriate forum. (Yes, I know I should try/catch a possible exception at CreateDirectory; I doubt that is the issue). Thanks!
namespace make_dir_tree
{
class Program
{
static void Main(string[] args)
{
string path;
Stopwatch watch = new Stopwatch();
watch.Start();
for (int i = 0; i < 100; i++)
for (int j = 0; j < 100; j++)
{
for (int k = 0; k < 100; k++)
{
path = String.Format("D:/dataset/{0:D2}/{1:D2}/{2:D2}", i, j, k);
Directory.CreateDirectory(path);
}
Console.WriteLine("i:{0} j:{1}",i,j);
}
watch.Stop();
Console.WriteLine("Elapsed: {0}", watch.Elapsed);
Console.WriteLine("In milliseconds: {0}", watch.ElapsedMilliseconds);
}
}
}
|
|
|
|
|
Hi,
you MUST use try-catch for operations that MIGHT fail due to external circumstances,
since they WILL fail sooner or later.
How big is your D: drive ?
In your case, my best guess (and the try-catch would have shown you already) is
your D: drive is full. Each directory takes at least one cluster, cluster size depends
on partition size, but is at least one sector (normally 0.5KB).
When the disk is full, you get an IOException. I dont know what you did not to get it,
maybe running in a separate thread ??
|
|
|
|
|
OK, I'll add the try/catch around the CreateDirectory, and report back
It shouldn't fail, but it could.
In this case the D drive is a "just formated" 1TB drive with nothing else on it.
This 1M node directory structure usually takes about 1 GB in NTFS.
-ss42
|
|
|
|
|
NTFS issue. When you create a file or folder, it has to create a unique 8.3 format name ('short name') for compatibility with 16-bit DOS and Windows applications. It has to search the directory to ensure that it is unique, which takes longer the more files you have in the folder. It works best if you have distinguishing information at the beginning of the filename so that there are few clashes.
You can disable this feature globally in Windows by setting the following key
[HKEY_LOCAL_MACHINE\SYSTEM\CURRENTCONTROLSET\CONTROL\FileSystem\Ntfs]
"Disable8dot3NameCreation"=dword:1 However, you should only do this on systems used for a fixed purpose and with careful testing.
|
|
|
|
|
In my project i am using a data grid. I have have one button to delete records(rows) in the Grid. If i drag the mouse accross the rows on the grid, it selects all the rows. when clicked delete button it deletes the pointed record.
Is there any facility to restrict the user from selecting multiple rows or what event fired when user select multiple rows?
|
|
|
|
|
Hi Guys,
I want to know how can I detect that the internet connection on my machine is idle.
Can you hint me with any code snippet, .net class or something like that
Thanks in advance
Mahmoud Zidan myBlog[^]
|
|
|
|
|
hope this will give an Idea.
SSK.
|
|
|
|
|