当前位置:  开发笔记 > 编程语言 > 正文

C#到VB.Net转换,RaiseEvent麻烦

如何解决《C#到VB.Net转换,RaiseEvent麻烦》经验,为你挑选了1个好方法。

我已经将流程创建观察器从C#转换为VB.Net,并且在转换使我遇到麻烦的代码行代码时出现问题.

这是原始的C#代码:

using System;
using System.ComponentModel;
using System.Collections;
using System.Globalization;
using System.Management;

namespace WMI.Win32
{
    public delegate void ProcessEventHandler(Win32_Process proc);
    public class ProcessWatcher : ManagementEventWatcher
    {
        // Process Events
        public event ProcessEventHandler ProcessCreated;
        public event ProcessEventHandler ProcessDeleted;
        public event ProcessEventHandler ProcessModified;

        // WMI WQL process query strings
        static readonly string WMI_OPER_EVENT_QUERY = @"SELECT * FROM 
        __InstanceOperationEvent WITHIN 1 WHERE TargetInstance ISA'Win32_Process'";
        static readonly string WMI_OPER_EVENT_QUERY_WITH_PROC =
        WMI_OPER_EVENT_QUERY + " and TargetInstance.Name = '{0}'";

    public ProcessWatcher()
    {
        Init(string.Empty);
    }
    public ProcessWatcher(string processName)
    {
        Init(processName);
    }
    private void Init(string processName)
    {
        this.Query.QueryLanguage = "WQL";
        if (string.IsNullOrEmpty(processName))
        {
            this.Query.QueryString = WMI_OPER_EVENT_QUERY;
        }
        else
        {
            this.Query.QueryString =
                string.Format(WMI_OPER_EVENT_QUERY_WITH_PROC, processName);
        }

        this.EventArrived += new EventArrivedEventHandler(watcher_EventArrived);
    }
    private void watcher_EventArrived(object sender, EventArrivedEventArgs e)
    {
        string eventType = e.NewEvent.ClassPath.ClassName;
        Win32_Process proc = new 
            Win32_Process(e.NewEvent["TargetInstance"] as ManagementBaseObject);

        switch (eventType)
        {
            case "__InstanceCreationEvent":
                if (ProcessCreated != null) ProcessCreated(proc); break;
            case "__InstanceDeletionEvent":
                if (ProcessDeleted != null) ProcessDeleted(proc); break;
            case "__InstanceModificationEvent":
                if (ProcessModified != null) ProcessModified(proc); break;
        }
    }
}

// Auto-Generated running: mgmtclassgen Win32_Process /n root\cimv2 /o WMI.Win32
// Renaming the class from Process to Win32_Process
public class Win32_Process { ... }
}

这是VB.Net的转换:

Imports System.ComponentModel
Imports System.Collections
Imports System.Globalization
Imports System.Management

Namespace WMI.Win32
Public Delegate Sub ProcessEventHandler(proc As Win32_Process)
Public Class ProcessWatcher
    Inherits ManagementEventWatcher
    ' Process Events
    Public Event ProcessCreated As ProcessEventHandler
    Public Event ProcessDeleted As ProcessEventHandler
    Public Event ProcessModified As ProcessEventHandler

    ' WMI WQL process query strings
    Shared ReadOnly WMI_OPER_EVENT_QUERY As String = "SELECT * FROM " & vbCr & vbLf & "__InstanceOperationEvent WITHIN 1 WHERE TargetInstance ISA 'Win32_Process'"
    Shared ReadOnly WMI_OPER_EVENT_QUERY_WITH_PROC As String = WMI_OPER_EVENT_QUERY + " and TargetInstance.Name = '{0}'"

    Public Sub New()
        Init(String.Empty)
    End Sub
    Public Sub New(processName As String)
        Init(processName)
    End Sub
    Private Sub Init(processName As String)
        Me.Query.QueryLanguage = "WQL"
        If String.IsNullOrEmpty(processName) Then
            Me.Query.QueryString = WMI_OPER_EVENT_QUERY
        Else
            Me.Query.QueryString = String.Format(WMI_OPER_EVENT_QUERY_WITH_PROC, processName)
        End If

        Me.EventArrived += New EventArrivedEventHandler(AddressOf watcher_EventArrived)
    End Sub
    Private Sub watcher_EventArrived(sender As Object, e As EventArrivedEventArgs)
        Dim eventType As String = e.NewEvent.ClassPath.ClassName
        Dim proc As New Win32_Process(TryCast(e.NewEvent("TargetInstance"), ManagementBaseObject))

        Select Case eventType
            Case "__InstanceCreationEvent"
                RaiseEvent ProcessCreated(proc)
                Exit Select
            Case "__InstanceDeletionEvent"
                RaiseEvent ProcessDeleted(proc)
                Exit Select
            Case "__InstanceModificationEvent"
                RaiseEvent ProcessModified(proc)
                Exit Select
        End Select
    End Sub
End Class


End Namespace

我遇到了这行代码的问题:

Me.EventArrived += New EventArrivedEventHandler(AddressOf watcher_EventArrived)

我似乎无法弄清楚如何正确转换它,我感谢任何帮助!



1> Thomas..:

这一行:

Me.EventArrived += New EventArrivedEventHandler(AddressOf watcher_EventArrived)

应该:

AddHandler Me.EventArrived , AddressOf Me.watcher_EventArrived

推荐阅读
yzh148448
这个屌丝很懒,什么也没留下!
DevBox开发工具箱 | 专业的在线开发工具网站    京公网安备 11010802040832号  |  京ICP备19059560号-6
Copyright © 1998 - 2020 DevBox.CN. All Rights Reserved devBox.cn 开发工具箱 版权所有