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

1

I wanted to know why the reason for this function created in dll to WPF application in VS 2013 does not start 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 in several ways but I can not start the Event Tick. Remember that it is not the Timer of Windows.Forms and yes of WPF == DispatcherTimer

    
asked by anonymous 30.05.2015 / 21:14

1 answer

0

Solution:  After a long time I try to understand what went wrong ..

Event Tick of DispatcherTimer did not start because I had not Called a Thread of type STA , I made that call only in the WPF Application and not in the DLL ...

So if you want to call Event Tick of the DLL Developed for App's WPF you must declare the Thread STA in the Form where you call the dll, you just call a normal Thread or arrow it as STA .

Example: WPF Application

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 it's just called the StartThread Tick event. : D Simple no?

    
31.05.2015 / 04:56