Reputation: 305
Which is the proper way to add the Changed Event?
public static void WatchFileForChange()
{
FileSystemWatcher fsw = new FileSystemWatcher();
fsw.Path = Path.Join(System.Configuration.ConfigurationManager.AppSettings["AccessDBFolder"]);
fsw.Changed += new FileSystemEventHandler(UpdateMDBChange);
fsw.Filter = "*.mdb";
fsw.NotifyFilter = NotifyFilters.FileName | NotifyFilters.LastWrite;
fsw.EnableRaisingEvents = true;
fsw.IncludeSubdirectories = true;
}
private static void UpdateMDBChange(object sender, FileSystemEventArgs e)
{
//https://stackoverflow.com/a/3042963/6067603
DateTime lastWriteTime = File.GetLastWriteTime(e.FullPath);
if (lastWriteTime.Ticks - lastRead.Ticks > 100000)
{
TestClient().Wait();
lastRead = lastWriteTime;
}
}
Is it fsw.Changed += new FileSystemEventHandler(UpdateMDBChange);
or fsw.Changed += UpdateMDBChange;
Both ways seem to fine. But I don't know if there's a technical issue that could occur. Or if a certain way is frowned upon.
Upvotes: 0
Views: 115
Reputation: 8522
They're identical under the hood.
Here's the disassembly:
ed.MyEvent += Ed_MyEvent;
00007FFDF3D05F22 mov rcx,7FFDF3DD4F00h
00007FFDF3D05F2C call CORINFO_HELP_NEWSFAST (07FFE5384AF00h)
00007FFDF3D05F31 mov qword ptr [rbp+28h],rax
00007FFDF3D05F35 mov r8,offset CLRStub[MethodDescPrestub]@7ffdf3cfc768 (07FFDF3CFC768h)
00007FFDF3D05F3F mov rcx,qword ptr [rbp+28h]
00007FFDF3D05F43 xor edx,edx
00007FFDF3D05F45 mov r9,offset CLRStub[StubLinkStub]@7ffdf3bcd070 (07FFDF3BCD070h)
00007FFDF3D05F4F call Method stub for: System.MulticastDelegate.CtorOpened(System.Object, IntPtr, IntPtr) (07FFDF3CF4568h)
00007FFDF3D05F54 mov rcx,qword ptr [rbp+38h]
00007FFDF3D05F58 mov rdx,qword ptr [rbp+28h]
00007FFDF3D05F5C cmp dword ptr [rcx],ecx
00007FFDF3D05F5E call CLRStub[MethodDescPrestub]@7ffdf3d059b8 (07FFDF3D059B8h)
00007FFDF3D05F63 nop
ed.MyEvent += new dlgEvent(Ed_MyEvent);
00007FFDF3D05F64 mov rcx,7FFDF3DD4F00h
00007FFDF3D05F6E call CORINFO_HELP_NEWSFAST (07FFE5384AF00h)
00007FFDF3D05F73 mov qword ptr [rbp+20h],rax
00007FFDF3D05F77 mov r8,offset CLRStub[MethodDescPrestub]@7ffdf3cfc768 (07FFDF3CFC768h)
00007FFDF3D05F81 mov rcx,qword ptr [rbp+20h]
00007FFDF3D05F85 xor edx,edx
00007FFDF3D05F87 mov r9,offset CLRStub[StubLinkStub]@7ffdf3bcd070 (07FFDF3BCD070h)
00007FFDF3D05F91 call Method stub for: System.MulticastDelegate.CtorOpened(System.Object, IntPtr, IntPtr) (07FFDF3CF4568h)
00007FFDF3D05F96 mov rcx,qword ptr [rbp+38h]
00007FFDF3D05F9A mov rdx,qword ptr [rbp+20h]
00007FFDF3D05F9E cmp dword ptr [rcx],ecx
00007FFDF3D05FA0 call CLRStub[MethodDescPrestub]@7ffdf3d059b8 (07FFDF3D059B8h)
00007FFDF3D05FA5 nop
}
I don't pretend to know what all those instructions are but you can see they're the same.
So the answer to your question is, "it doesn't matter".
Upvotes: 5
Reputation: 11201
This is called method group conversion. It allows you to replace the more verbose
fsw.Changed += new FileSystemEventHandler(UpdateMDBChange);
by directly supplying the method name
fsw.Changed += UpdateMDBChange;
Basically, it's syntactic sugar, a feature added later on so you could type less. The type of the event handler can be inferred and doesn't need to be declared explicitly.
Upvotes: 4