Sending events to main app from a class

  • Thread starter Thread starter Terry Olsen
  • Start date Start date
T

Terry Olsen

I'm writing a class module (to be compiled to .dll). I have a couple of
questions...

1. Is there a way, inside the class, to repeatedly check a value (timer is
not available here), and
2. trigger an event that the main app would catch when the value is true?
 
Hi Terry,

Here is the one way to do that...
Hope it heps
Do reply

Thanks and Regard
Sakharam Phapale


Public Class Class1

Public Event myClassEvent(ByVal Value As Boolean)
Private m_IsVariableTrue As Boolean = False
Private myThread As Threading.Thread

Public Property myVariable() As Boolean
Get
Return m_IsVariableTrue
End Get
Set(ByVal Value As Boolean)
m_IsVariableTrue = Value
End Set
End Property

Public Sub New()
myThread = New Threading.Thread(AddressOf CheckValue)
myThread.Start()
End Sub

Private Sub CheckValue()
While True
If m_IsVariableTrue = True Then
RaiseEvent myClassEvent(m_IsVariableTrue)
End If
End While
End Sub

Protected Overrides Sub Finalize()
myThread.Abort()
MyBase.Finalize()
End Sub

End Class




Public Class Form1
Inherits System.Windows.Forms.Form

#Region " Windows Form Designer generated code "

Private WithEvents ObjClass1 As Class1

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
ObjClass1 = New Class1()
Timer1.Enabled = True
Timer1.Interval = 3000
End Sub

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load

End Sub

Private Sub ObjClass1_myClassEvent(ByVal Value As Boolean) Handles
ObjClass1.myClassEvent
MsgBox("Value changed as True")
End Sub

Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button2.Click
Timer1.Enabled = False
ObjClass1 = Nothing
End Sub

Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Timer1.Tick

If ObjClass1 Is Nothing Then Exit Sub

If ObjClass1.myVariable = True Then
ObjClass1.myVariable = False
Else
ObjClass1.myVariable = True
End If
End Sub

End Class
 
Terry,
1. Is there a way, inside the class, to repeatedly check a value (timer is
not available here), and

This makes me curious, why is there no timer available?

Cor
 
If a timer is available in this instance, I don't know how to access it.
The only way I know how to access the timer is to drag the timer object onto
a windows form. Since I'm working without a form, no timer...
 
Going off Sakharam's example (without the timer), you should be able to
throw a Thread.Sleep method in the CheckValue subroutine, so the thread
sleeps for a given time, checks the value, and if it matches, raises the
event which can be handled by whatever main class initiated this one.

-Jason
 
Hi Terry,

I have tried using the WHILE loop, you can also check for the values at
certain interval or frequency by using thread.sleep(); as mentioned in
Jason's email as follows:

"Going off Sakharam's example (without the timer), you should be able to
throw a Thread.Sleep method in the CheckValue subroutine, so the thread
sleeps for a given time, checks the value, and if it matches, raises the
event which can be handled by whatever main class initiated this one."

Try at your end and do let me know.

Thanks and regards,
Sakharam Phapale
 
I'm writing a class module (to be compiled to .dll). I have a couple of
questions...

1. Is there a way, inside the class, to repeatedly check a value (timer is
not available here), and
2. trigger an event that the main app would catch when the value is true?

There are other timers in the .NET framework... Take a look at
System.Timers.Timer or System.Threading.Timer. Either of these should
handle your needs...
 
Terry,

When you are used to drag components from the toolbox you can as well use
the component class to create a class.

That goes by opening the solution explorer, rightclick on the solution,
choose for add new and than choose for the component, give it a name and
enter.

Than you can from the toolbox (the component part) the timer from there
about which Tom is talking. You are than busy as with a form.

For the rest I saw some answers, so when you have more questions, please
reply?

Cor
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top