Detect generated event in another class

Asked

Viewed 285 times

4

I have a class (in VB) that returns an event of TimeOut, that is, if time runs out it returns an event content with a string with the data I need using Raiseevent. How can I treat this event in the main class that is in C#. I tried using delegate but could not. Thanks.

Code:

Public Class Dados
    Public Event _TrateTimeOut(ByVal dados As Dados)

    Private Sub MonitoraTimeOut()
        If TimeOut Is Nothing Then

            TimeOut = New Timers.Timer
            TimeOut.Interval = 10000
            AddHandler TimeOut.Elapsed, AddressOf TrateTimeOut
            TimeOut.Start()
        Else
            TimeOut.Stop()
            TimeOut.Start()
        End If

    End Sub

    Private Sub TrateTimeOut()
        RaiseEvent _TrateTimeOut(Me)
    End Sub
End Class
  • You can ask your question an excerpt of code explaining how this solution works?

  • Code added!!

  • From what I understand, _TrateTimeOut() is declared somewhere. Where?

  • At the beginning of the Data class with: Public Event _Tratetimeout(Byval Data)

  • You can add it to the question too, please?

  • Added, you have an idea how I can do ?! :)

  • Well, the C# class needs to have an event with the following prototype: private void FuncaoQueTrataOEvento(object sender, EventArgs e), and at startup you need to instantiate Dados and pass the function inside the class Dados. I’ll try an answer.

  • I tried that, but the Monitor() is not started.

Show 3 more comments

2 answers

2

Friend, In the comments you complain about the event denting in an infinite loop and in your question you have the following passage: "if time breaks it returns an event content with a string with the data I need using Raiseevent"

It will pop yes, every 10 seconds and indefinitely. But it seems like you want to be once only.

I would use the Gypsy version, optimally:

public class ConsumidorComponenteVBZao { public ConsumidorComponenteVBZao() { var dados = new Dados(); dados._TrateTimeOut += FuncaoQueTrataOEvento; } private void FuncaoQueTrataOEvento(object sender, EventArgs e) { // Faça aqui o tratamento de dados, e tal. } }

And in your Vbzão, use a Thread and not a timer to trigger events at each interval.

0

I don’t know your class C#, but I’ll assume something like that:

public class ClasseEmCSharp {
    public Dados dados = new Dados();
    public delegate void EventHandler(object sender, EventArgs e);

    public ClasseEmCSharp() {
        dados._TrateTimeOut += new EventHandler(FuncaoQueTrataOEvento);
    }

    private void FuncaoQueTrataOEvento(object sender, EventArgs e) 
    {
        // Faça aqui o tratamento de dados, e tal.
    }
}

It’s a little weird the treatment, but that’s basically it.

  • I got it in parts using the following code: data. _Tratetimeout += new data. _Treatmentouteventhandler(Shutdown); no enentato the method I created called Shutdown(), is in an infinite loop.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.