Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/21.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# NET上同一函数上的多个事件处理_C#_.net_Vb.net_Events - Fatal编程技术网

C# NET上同一函数上的多个事件处理

C# NET上同一函数上的多个事件处理,c#,.net,vb.net,events,C#,.net,Vb.net,Events,我最近与一些FileSystemWatchers一起测试,想知道事件处理在.NET中是如何工作的。例如,在VB.NET中使用以下代码: Dim fsw1, fsw2, fsw3 As New FileSystemWatcher() Private Sub fsw_Error(sender As Object, e As ErrorEventArgs) _ Handles fsw1.Error, fsw2.Error, fsw3.

我最近与一些FileSystemWatchers一起测试,想知道事件处理在.NET中是如何工作的。例如,在VB.NET中使用以下代码:

Dim fsw1, fsw2, fsw3 As New FileSystemWatcher()

Private Sub fsw_Error(sender As Object, e As ErrorEventArgs) _
                                 Handles fsw1.Error, fsw2.Error, fsw3.Error
    Dim iCont As Integer = 1
    Dim fsw As FileSystemWatcher = CType(sender, FileSystemWatcher)

    While Not fsw.EnableRaisingEvents AndAlso iCont < 120
        iCont += 1

        Try
            fsw.EnableRaisingEvents = True
            txtResultado.Clear()
        Catch
            fsw.EnableRaisingEvents = False
            Threading.Thread.Sleep(30000)
        End Try
    End While
End Sub
每次在任何FileSystemWatcher上发生错误时,都会执行该函数。是否为每个FileSystemWatcher生成不同的函数线程?它们是否可以在同时执行多个任务时相互干扰


C和VB.NET之间可能有一个不同之处?

对于哪个线程调用事件没有约束;如果您想在另一个线程中调用事件,可以在.NET中自由调用。老实说,我无法想象在不使用单独的线程或异步IO的情况下实现FileSystemWatcher,该线程或异步IO会在发现更改时调用事件,因为您不希望事件中断应用程序的正常流程

NET中的事件与调用列表一起工作,该列表在引发事件时被调用。调用事件时,在单个线程中遍历调用列表并调用每个方法。就这么简单


该功能是.NET功能,因此对于C和VB.NET来说都是一样的。您对事件处理有一个基本的错误理解。您似乎假设所有触发的事件都将发送到同一个函数,或者说发送到同一个函数实例

事实并非如此。每次引发事件时,都会调用函数fsw_error。该函数是否已经执行了10次并不重要。因此,在函数内部等待,直到引发另一个事件,这看起来像是您试图这样做,但不起作用

但是,您可以将eventhandler封装在实例化类中,并确保所有引发的事件执行都使用相同的变量范围。我不确定,如果事件是在不同的线程上触发的,我不这么认为,但如果是这种情况,你当然需要注意同步访问

Private Class MyEventHandlerHolder
   private iCont as Integer = 0;    

   Private Sub fsw_Error(sender As Object, e As ErrorEventArgs) _
                             Handles fsw1.Error, fsw2.Error, fsw3.Error

       iCont += 1

End Sub
现在,每当文件系统监视程序触发错误事件时,iCont就会增加

fsw = new FileSystemWatcher()
fsw2 = new FileSystemWatcher()
fsw.EnableRaisingEvents = true;
fsw2.EnableRaisingEvents = true;
myHandler  = new MyEventHandlerHolder();
AddHandler fsw.Onerror, AdressOf myHandler.fsw_error //not sure about the right Event Name in vb
AddHandler fsw2.Onerror, AdressOf myHandler.fsw_error 

我的vb很久以前就回来了。也许您不能将实例的方法作为处理程序。但需要一个静态或单例处理程序类。

当FileSystemWatchers引发事件时,应在线程池线程上调用该处理程序。这些事件可能相互干扰,因为长时间处理一个FileSystemWatcher引发的事件可能会延迟处理其中一个FileSystemWatcher引发的另一个事件。在这种程度上,您可以在事件处理程序中使用专用线程来执行冗长的阻塞工作


VB.NET和C之间应该没有区别。

我在这里没有看到任何线程或多个文件系统观察者-所以我想知道你在说什么…@DanielHilgarth文件系统观察者是fsw1、fsw2和fsw3。为什么会有反对票?这个问题是关于同一个函数在事件中的多次执行。代码就是一个例子。Dim fsw1、fsw2、fsw3作为新的FileSystemWatcher将是多个FileSystemWatcher,@Daniel Hilgarth;如果我遵循,多线程将是@SysDragon感觉到的那些可能与多个FileSystemWatchers引发的事件相关的线程。