events -- child/parent

  • Thread starter Thread starter Dayne
  • Start date Start date
Dayne said:
Can a Parent thread catch a event send by a child thread?

There is no particular parent/child relationship between threads, and
events always execute on the thread which raises them. However, you can
marshal delegate calls across threads using things like Control.Invoke,
Control.BeginInvoke etc. Perhaps you could tell us more about what
you're trying to do?
 
I am writing a multiform form windows application that should time-out if
there is not user input after some predefined time. I have implemented the
time-out feature using a "worker" thread with a callback function. The
"callback" function should create a form letting the user know when he was
idling and it offers 2 options to the user -- one to go back to the last
page of the parent thread or go to the start of the program.The problem I
have been having is that once I call the worker thread there is not way go
back to starting page of the parent thread. Please help!
 
I am writing a multiform form windows application that should time-out
if there is not user input after some predefined time. I have
implemented the time-out feature using a "worker" thread with a callback
function. The "callback" function should create a form letting the user
know when he was idling and it offers 2 options to the user -- one to go
back to the last page of the parent thread or go to the start of the
program.The problem I have been having is that once I call the worker
thread there is not way go back to starting page of the parent thread.
Please help!
 
Dayne said:
I am writing a multiform form windows application that should
time-out if there is not user input after some predefined time. I
have implemented the time-out feature using a "worker" thread with a
callback function. The "callback" function should create a form
letting the user know when he was idling and it offers 2 options to
the user -- one to go back to the last page of the parent thread or
go to the start of the program.The problem I have been having is that
once I call the worker thread there is not way go back to starting
page of the parent thread. Please help!

Well, you can use Control.Invoke to invoke a method on the UI thread.
I'm not sure what you mean by "starting page of the parent thread" - I
suggest you separate out your threading issues from your UI issues, as
they sound fairly distinct. Use Control.Invoke to invoke a delegate on
the UI thread though.

See http://www.pobox.com/~skeet/csharp/threads/winforms.shtml
 
Dayne,

4 minutes ago I have sent this changed sample to the newsgroup languages
VBNet

Have a look or try this sample (I changed it a little bit in the previous
text so
there can be errors).

\\\needs on form 1 one button and a textbox
Private WithEvents frm1 As Form2
Private Delegate Sub Frm1Handler(ByVal message As String)
Private MyThread As System.Threading.Thread
Private Sub Form1_Load(ByVal sender As Object, _
ByVal e As System.EventArgs) Handles MyBase.Load
Dim timer1 As New System.Windows.Forms.Timer
AddHandler timer1.Tick, AddressOf mytimer1
TextBox1.Text = "0"
timer1.Enabled = True
timer1.Interval = 400
Dim timer2 As New System.Windows.Forms.Timer
End Sub
Private Sub mytimer1(ByVal sender As Object, _
ByVal e As System.EventArgs)
TextBox1.Text = (CInt(TextBox1.Text) + 1).ToString
DirectCast(sender, System.Windows.Forms.Timer).Enabled = True
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles Button1.Click
frm1 = New Form2
frm1.itstop = Me.Top
frm1.itsleft = Me.Left + 200
AddHandler frm1.ready, AddressOf Frm1Ready
frm1.Text = "Extra thread"
MyThread = New System.Threading.Thread(AddressOf frm1.Show)
MyThread.Start()
End Sub
Private Sub Frm1Ready(ByVal message As String)
Me.BeginInvoke(New Frm1Handler(AddressOf Frm1HandlerSub), New
Object() {message})
End Sub
Private Sub Frm1HandlerSub(ByVal message As String)
TextBox2.Text = message
frm1.Close()
MyThread.Abort()
End Sub
Private Sub Form1_Closing(ByVal sender As Object, _
ByVal e As System.ComponentModel.CancelEventArgs) Handles MyBase.Closing
MyThread.Abort()
End Sub
///
\\\Needs a form2 with one textbox
Friend Event ready(ByVal message As String)
Friend itstop As Integer
Friend itsleft As Integer
Private Sub Form2_Activated(ByVal sender As Object, _
ByVal e As System.EventArgs) Handles MyBase.Activated
Me.Left = itsleft
Me.Top = itstop
Me.BringToFront()
Dim timenext As DateTime = Now.Add(TimeSpan.FromSeconds(10))
Do While timenext > Now
TextBox1.Text = Now.TimeOfDay.ToString
Application.DoEvents() 'to show the time
Threading.Thread.Sleep(50)
Me.Opacity -= 0.004
Loop
RaiseEvent ready(Now.TimeOfDay.ToString)
End Sub
Private Sub Form2_Closing(ByVal sender As Object, ByVal _
e As System.ComponentModel.CancelEventArgs) Handles MyBase.Closing
e.Cancel = True
End Sub
///
I hope this helps a little bit?

Cor


Dayne said:
I am writing a multiform form windows application that should time-out if
there is not user input after some predefined time. I have implemented the
time-out feature using a "worker" thread with a callback function. The
"callback" function should create a form letting the user know when he was
idling and it offers 2 options to the user -- one to go back to the last
page of the parent thread or go to the start of the program.The problem I
have been having is that once I call the worker thread there is not way go
back to starting page of the parent thread. Please help!
 
Back
Top