Custom Control Libary WPF - Dispatchertimer Does Not Start VB.NET

Asked

Viewed 48 times

1

I wanted to know why of this function created in a dll for WPF application in the VS 2013 does not start the DispatcherTimer.

Code:

Public Sub Iniciar
         TIniciaAntiDebuggers = New DispatcherTimer()
            AddHandler TIniciaAntiDebuggers.Tick, AddressOf TIniciaAntiDebuggers_Tick
            TIniciaAntiDebuggers.Interval = TimeSpan.FromSeconds(2)
            TIniciaAntiDebuggers.Start()    
        End If

Event:

Private Sub TIniciaAntiDebuggers_Tick(ByVal sender As Object, ByVal e As EventArgs)
        AntiDebuggerOllyDbg()
    End Sub

I tried several ways but I can’t start the Tick Event.. Remembering that it’s not the Timer of Windows.Forms rather that of the WPF == DispatcherTimer

1 answer

0

Solution: After much I try I understood what went wrong..

The Event Tick of DispatcherTimer didn’t start because I hadn’t called a Thread of the kind STA, i made this call only on WPF Application and not on DLL...

So if you want to call the Event Tick da DLL Developed for App’s WPF you must declare the Thread STA in the Form where it calls the dll, in the proper it is only you call a normal Thread or arrow it as well as STA.

Example: WPF Aplication

Dim ThreadWPFApplication As Thread

Public Sub IniciarThread
ThreadWPFApplication =  New Thread(New ThreadStart(AddressOf IniciarDLL)
ThreadWPFApplication.SetApartmentState(ApartmentState.STA)
ThreadWPFApplication.Start()
End sub

Public Sub IniciarDLL
''Chama a DLL
End Sub 

DLL

Private WithEvents Tiniciar As DispatcherTimer
Dim IniciarThread As Thread

Public Sub New
  Me.Tiniciar = New System.Windows.Threading.DispatcherTimer
  Me.Tiniciar.Interval = TimeSpan.FromMilliseconds(2200)
  Me.Tiniciar.IsEnabled = True
End Sub

Public sub IniciarThread
''Aqui é apenas uma linha NADA MAIS, n precisa setar a **THREAD** como **STA**
IniciarThread = New Thread(New ThreadStart(AddressOf IniciarDispatcherTimer)
End Sub

Public Sub IniciarDispatcherTimer()
  Me.Tiniciar = New System.Windows.Threading.DispatcherTimer
  Me.Tiniciar.Interval = TimeSpan.FromMilliseconds(2200)
  Me.Tiniciar.Start()
End Sub

Now just call the Start Tick event D Simple no?

Browser other questions tagged

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