Paint event don't Work

G

Guest

Hi :
I have created a Class where this contains a timer , each time the timer
control been activate or its interval time has elpased a new window must
appear and deseapear ,, ( com kind of alarm) ,, I created another class
(form) that is instanced when the peiod time from my Class A is executed ..
Why the event Method DO not fires !! ? , Why if I Instance the Class from
another Form it Works , but When I Instance The Class From My Class Don't
Work.

Tank Very Much for your help ...
 
J

Jon Skeet [C# MVP]

Josue Avila Mendoza said:
Hi :
I have created a Class where this contains a timer , each time the timer
control been activate or its interval time has elpased a new window must
appear and deseapear ,, ( com kind of alarm) ,, I created another class
(form) that is instanced when the peiod time from my Class A is executed ..
Why the event Method DO not fires !! ? , Why if I Instance the Class from
another Form it Works , but When I Instance The Class From My Class Don't
Work.

Tank Very Much for your help ...

Could you post a short but complete program which demonstrates the
problem?

See http://www.pobox.com/~skeet/csharp/complete.html for details of
what I mean by that.
 
S

Stoitcho Goutsev \(100\) [C# MVP]

What kind of timer do you use? If you use something different than
System.Windows.Forms.Timer it is most likely the timer event is executed in
a worker thread that is not UI (doesn't run a message pump) in this case the
new form won't receive WM_PAINT, thus won't redraw.

What you need to do is to marshal code execution using Control.Invoke to the
UI thread that shows the main form.
 
G

Guest

Hi Stoitcho
Actually Im Using Threading.Timer In order to execute the Sub that shows
de Window in other Thread ..
You said i have to Use control.invoke() ? how can i use it ?
 
G

Guest

Hi
It is not posible for me to acces into the link ; i Think is Firewall issues
...
But Look this is a part from my class code ..

Public Class AlertsEngine
Private LCCollectionAlerts As Alerts
Private LCDCAlertEngine As DCAlertEngine = New DCAlertEngine
Private InstanceSearcher As New TimerCallback(AddressOf Me.Search)
Private LCInterval As Integer = 8000
Private StateTimer As Timer
Public Event showAlert(ByVal strAlertMessage As String)


Sub New(ByVal ObjAlertClct As Alerts)
Me.LCCollectionAlerts = ObjAlertClct
End Sub

Public Function Start()
Me.StateTimer = New Timer(InstanceSearcher, Nothing, LCInterval, 250)
StateTimer.Change(250, LCInterval)
End Function

Public Function [Stop]()
If Not Me.StateTimer Is Nothing Then
StateTimer.Change(0, 0)
End If
End Function


Private Sub Search(ByVal Stateinfo As Object)

Dim TmpAlert As Alert
Dim DrRowAlerToAcv As DataRow

For Each TmpAlert In Me.LCCollectionAlerts
Dim TmpAlertSubRows As DataTable = TmpAlert.GetRecordsInAlertsOn()

For Each DrRowAlerToAcv In TmpAlertSubRows.Rows
Dim strAlertMsg As String =
TmpAlert.AlertMessage.ToString(DrRowAlerToAcv)

Dim NewAlrtfrm As AlertSpace = New AlertSpace("Testin
Alert...", 3000)
NewAlrtfrm.ShowInTaskbar = False
NewAlrtfrm.Show()




RaiseEvent showAlert(strAlertMsg)
Call FlagRecord(TmpAlert, DrRowAlerToAcv)
Next
Next

End Sub

End Class



Into the Search Sub you may see that im instancing the form AlertSpace :

Dim NewAlrtfrm As AlertSpace = New AlertSpace("Testin Alert...", 3000)

When this line is executed: The form is showed however, the paint events
(form.paint) and (panel1.paint) ; (im using a paint object into to) are not
executed ..

=( Thank you very much for your help ; and any advice you can give..
 
S

Stoitcho Goutsev \(100\)

Josue,

You cannot show a form like this. The thread that executes the event is not
an UI thread. You need to either create a UI thread or create the form in
the main UI thread.
Starting a message pump in the thread where timer event is fired is not a
good idea because this thread is borrowed from the thread pool where the
threads are limited number, thus using the main thread is better approach.

To create the form in the main thread you need to have reference to some
control from the main UI (say reference to the form). Basically this work as
follows (I write this in c# because I'm not very comfortable with the VB
syntax.

void CreateAForm(Param1, Param2, ....)
{
if(aControl.InvokeRequired)
{
SomeDelegate someDelegate = new SomeDelegate(CreateAForm)
// aControl is a reference to a control from the main UI
aControl.Invoke(someDelegate, new object[]{param1, param2, ....});
return; //NOTE: This return is very important
}

<All the code for creating, initializing and showing the new form>
}


--
HTH
Stoitcho Goutsev (100)



Josue Avila Mendoza said:
Hi
It is not posible for me to acces into the link ; i Think is Firewall
issues
..
But Look this is a part from my class code ..

Public Class AlertsEngine
Private LCCollectionAlerts As Alerts
Private LCDCAlertEngine As DCAlertEngine = New DCAlertEngine
Private InstanceSearcher As New TimerCallback(AddressOf Me.Search)
Private LCInterval As Integer = 8000
Private StateTimer As Timer
Public Event showAlert(ByVal strAlertMessage As String)


Sub New(ByVal ObjAlertClct As Alerts)
Me.LCCollectionAlerts = ObjAlertClct
End Sub

Public Function Start()
Me.StateTimer = New Timer(InstanceSearcher, Nothing, LCInterval,
250)
StateTimer.Change(250, LCInterval)
End Function

Public Function [Stop]()
If Not Me.StateTimer Is Nothing Then
StateTimer.Change(0, 0)
End If
End Function


Private Sub Search(ByVal Stateinfo As Object)

Dim TmpAlert As Alert
Dim DrRowAlerToAcv As DataRow

For Each TmpAlert In Me.LCCollectionAlerts
Dim TmpAlertSubRows As DataTable =
TmpAlert.GetRecordsInAlertsOn()

For Each DrRowAlerToAcv In TmpAlertSubRows.Rows
Dim strAlertMsg As String =
TmpAlert.AlertMessage.ToString(DrRowAlerToAcv)

Dim NewAlrtfrm As AlertSpace = New AlertSpace("Testin
Alert...", 3000)
NewAlrtfrm.ShowInTaskbar = False
NewAlrtfrm.Show()




RaiseEvent showAlert(strAlertMsg)
Call FlagRecord(TmpAlert, DrRowAlerToAcv)
Next
Next

End Sub

End Class



Into the Search Sub you may see that im instancing the form AlertSpace :

Dim NewAlrtfrm As AlertSpace = New AlertSpace("Testin Alert...", 3000)

When this line is executed: The form is showed however, the paint events
(form.paint) and (panel1.paint) ; (im using a paint object into to) are
not
executed ..

=( Thank you very much for your help ; and any advice you can give..
 
S

Stoitcho Goutsev \(100\)

Actually there is a easier solutions than the one I already suggested. I
just forgot to mention them with my last post.

1. Use System.Windows.Forms.Timer if timer accuracy is not an issue for you.

2. Use System.Timers.Timer instead of System.Threading.Timer. Timers.Timer
has a property - SynchronizingObject that you can set with a reference to a
control form the main UI and it will marshal the event for execution in the
UI thread for you.


--
HTH
Stoitcho Goutsev (100)

Stoitcho Goutsev (100) said:
Josue,

You cannot show a form like this. The thread that executes the event is
not an UI thread. You need to either create a UI thread or create the form
in the main UI thread.
Starting a message pump in the thread where timer event is fired is not a
good idea because this thread is borrowed from the thread pool where the
threads are limited number, thus using the main thread is better approach.

To create the form in the main thread you need to have reference to some
control from the main UI (say reference to the form). Basically this work
as follows (I write this in c# because I'm not very comfortable with the
VB syntax.

void CreateAForm(Param1, Param2, ....)
{
if(aControl.InvokeRequired)
{
SomeDelegate someDelegate = new SomeDelegate(CreateAForm)
// aControl is a reference to a control from the main UI
aControl.Invoke(someDelegate, new object[]{param1, param2, ....});
return; //NOTE: This return is very important
}

<All the code for creating, initializing and showing the new form>
}


--
HTH
Stoitcho Goutsev (100)



Josue Avila Mendoza said:
Hi
It is not posible for me to acces into the link ; i Think is Firewall
issues
..
But Look this is a part from my class code ..

Public Class AlertsEngine
Private LCCollectionAlerts As Alerts
Private LCDCAlertEngine As DCAlertEngine = New DCAlertEngine
Private InstanceSearcher As New TimerCallback(AddressOf Me.Search)
Private LCInterval As Integer = 8000
Private StateTimer As Timer
Public Event showAlert(ByVal strAlertMessage As String)


Sub New(ByVal ObjAlertClct As Alerts)
Me.LCCollectionAlerts = ObjAlertClct
End Sub

Public Function Start()
Me.StateTimer = New Timer(InstanceSearcher, Nothing, LCInterval,
250)
StateTimer.Change(250, LCInterval)
End Function

Public Function [Stop]()
If Not Me.StateTimer Is Nothing Then
StateTimer.Change(0, 0)
End If
End Function


Private Sub Search(ByVal Stateinfo As Object)

Dim TmpAlert As Alert
Dim DrRowAlerToAcv As DataRow

For Each TmpAlert In Me.LCCollectionAlerts
Dim TmpAlertSubRows As DataTable =
TmpAlert.GetRecordsInAlertsOn()

For Each DrRowAlerToAcv In TmpAlertSubRows.Rows
Dim strAlertMsg As String =
TmpAlert.AlertMessage.ToString(DrRowAlerToAcv)

Dim NewAlrtfrm As AlertSpace = New AlertSpace("Testin
Alert...", 3000)
NewAlrtfrm.ShowInTaskbar = False
NewAlrtfrm.Show()




RaiseEvent showAlert(strAlertMsg)
Call FlagRecord(TmpAlert, DrRowAlerToAcv)
Next
Next

End Sub

End Class



Into the Search Sub you may see that im instancing the form AlertSpace :

Dim NewAlrtfrm As AlertSpace = New AlertSpace("Testin Alert...", 3000)

When this line is executed: The form is showed however, the paint events
(form.paint) and (panel1.paint) ; (im using a paint object into to) are
not
executed ..

=( Thank you very much for your help ; and any advice you can give..
 
G

Guest

Hi Stoitcho
Finally I got the solution.. tks for your help , it was really a big Hand ..

Josue





Stoitcho Goutsev (100) said:
Actually there is a easier solutions than the one I already suggested. I
just forgot to mention them with my last post.

1. Use System.Windows.Forms.Timer if timer accuracy is not an issue for you.

2. Use System.Timers.Timer instead of System.Threading.Timer. Timers.Timer
has a property - SynchronizingObject that you can set with a reference to a
control form the main UI and it will marshal the event for execution in the
UI thread for you.


--
HTH
Stoitcho Goutsev (100)

Stoitcho Goutsev (100) said:
Josue,

You cannot show a form like this. The thread that executes the event is
not an UI thread. You need to either create a UI thread or create the form
in the main UI thread.
Starting a message pump in the thread where timer event is fired is not a
good idea because this thread is borrowed from the thread pool where the
threads are limited number, thus using the main thread is better approach.

To create the form in the main thread you need to have reference to some
control from the main UI (say reference to the form). Basically this work
as follows (I write this in c# because I'm not very comfortable with the
VB syntax.

void CreateAForm(Param1, Param2, ....)
{
if(aControl.InvokeRequired)
{
SomeDelegate someDelegate = new SomeDelegate(CreateAForm)
// aControl is a reference to a control from the main UI
aControl.Invoke(someDelegate, new object[]{param1, param2, ....});
return; //NOTE: This return is very important
}

<All the code for creating, initializing and showing the new form>
}


--
HTH
Stoitcho Goutsev (100)



Josue Avila Mendoza said:
Hi
It is not posible for me to acces into the link ; i Think is Firewall
issues
..
But Look this is a part from my class code ..

Public Class AlertsEngine
Private LCCollectionAlerts As Alerts
Private LCDCAlertEngine As DCAlertEngine = New DCAlertEngine
Private InstanceSearcher As New TimerCallback(AddressOf Me.Search)
Private LCInterval As Integer = 8000
Private StateTimer As Timer
Public Event showAlert(ByVal strAlertMessage As String)


Sub New(ByVal ObjAlertClct As Alerts)
Me.LCCollectionAlerts = ObjAlertClct
End Sub

Public Function Start()
Me.StateTimer = New Timer(InstanceSearcher, Nothing, LCInterval,
250)
StateTimer.Change(250, LCInterval)
End Function

Public Function [Stop]()
If Not Me.StateTimer Is Nothing Then
StateTimer.Change(0, 0)
End If
End Function


Private Sub Search(ByVal Stateinfo As Object)

Dim TmpAlert As Alert
Dim DrRowAlerToAcv As DataRow

For Each TmpAlert In Me.LCCollectionAlerts
Dim TmpAlertSubRows As DataTable =
TmpAlert.GetRecordsInAlertsOn()

For Each DrRowAlerToAcv In TmpAlertSubRows.Rows
Dim strAlertMsg As String =
TmpAlert.AlertMessage.ToString(DrRowAlerToAcv)

Dim NewAlrtfrm As AlertSpace = New AlertSpace("Testin
Alert...", 3000)
NewAlrtfrm.ShowInTaskbar = False
NewAlrtfrm.Show()




RaiseEvent showAlert(strAlertMsg)
Call FlagRecord(TmpAlert, DrRowAlerToAcv)
Next
Next

End Sub

End Class



Into the Search Sub you may see that im instancing the form AlertSpace :

Dim NewAlrtfrm As AlertSpace = New AlertSpace("Testin Alert...", 3000)

When this line is executed: The form is showed however, the paint events
(form.paint) and (panel1.paint) ; (im using a paint object into to) are
not
executed ..

=( Thank you very much for your help ; and any advice you can give..
 

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

Top