开发者

Getting C# DLL events from VB.Net

开发者 https://www.devze.com 2023-02-14 01:06 出处:网络
I\'d like to be able to access theevents in a C# DLL so I can display a Progress Bar as the file analysis is carried out - the DLL is written by a third party using VB.Net.

I'd like to be able to access the events in a C# DLL so I can display a Progress Bar as the file analysis is carried out - the DLL is written by a third party using VB.Net.

Details

C# FileAnaysis.DLL

Contains a public class FileManager which contains 2 public subs

public void ProgAnalysis(string fileName) 
开发者_运维知识库
public void ProgAnalysis(string fileName, ProgressChangedEventHandler progressChangedEventHandler, RunWorkerCompletedEventHandler runWorkerCompletedEventHandler) 

How do I access the events generated by second sub in Vb.Net?


Considering the method accepts a ProgressChangedEventHandler and a RunWorkerCompletedEventHandler, I'd wager you use delegates of these types and pass them to the method; then it will call them at the appropriate points.

For example, the following methods match the signatures of these delegate types:

' Matches ProgressChangedEventHandler '
Sub OnProgressChanged(ByVal sender As Object, ByVal e As ProgressChangedEventArgs)
    ' Do something. '
End Sub

' Matches RunWorkerCompletedEventHandler '
Sub OnRunWorkerCompleted(ByVal sender As Object, ByVal e As RunWorkerCompletedEventArgs)
    ' Do someting. '
End Sub

Given the above methods, you could pass them to ProgAnalysis like this:

ProgAnalysis("fileName", _
             AddressOf OnProgressChanged, _
             AddressOf OnRunWorkerCompleted)


You need to pass in methods that match those delegates. If I had to guess I'd say it's using a BackgroundWorker behind the scenes.

So you could do something like this.

void Main {
     ProgAnalysis(@"c:\test.txt", 
         delegate(object sender, ProgressChangedEventArgs e) {
             // Do something
         },
         delegate(object sender, RunWorkerCompletedEventArgs e) {
             // Do something else
         });
}

Just guessing about the delegate signatures. Of course you could use lambdas or separate methods instead of anonymous delegates, depending on exactly what you need to do.

It doesn't matter that the library was written in VB.NET.

0

精彩评论

暂无评论...
验证码 换一张
取 消