Threading Part 2

  • Thread starter Charles A. Lackman
  • Start date
C

Charles A. Lackman

Hello and thank you for your assistance.

I have attempted to accomplish what I need using delegates with no success.
i.e.

//Button Click//
Dim PollThread As Threading.Thread
PollThread = New Threading.Thread(AddressOf PollThreadAddress)
PollThread.Start()
End Sub

Private Sub PollThreadAddress()
frm1PollDatabase.TopMost = True
frm1PollDatabase.ShowDialog()
End Sub

//Button Click inside frm1PollDatabase that raises an event inside the
Parent Form//
Delegate Sub FromMyPage()
Dim MyDeleg As FromMyPage

Private Sub ApplyMyPage
MyDeleg = New FromMyPage(AddressOf DelegateFromMyPage)
MyDeleg.Invoke()
End Sub

Private Sub DelegateFromMyPage()
frm1Modify.showdialog
End Sub

** frm1PollDatabase is created inside a new thread and must always be on
top, which works fine. But when the user wants to send that data from this
form to a new form (frm1Modify) the form is displayed but frm1PollDatabase
becomes disabled because of the Showdialog method. I cannot use show for
this form to be displayed. If the original thread (Main) displays this form
wont it prevent this problem? The above code did not keep frm1PollDatabase
from being disabled.

Thanks Again,
Chuck
 
J

John Saunders

Charles A. Lackman said:
Hello and thank you for your assistance.

I have attempted to accomplish what I need using delegates with no
success.
i.e.

//Button Click//
Dim PollThread As Threading.Thread
PollThread = New Threading.Thread(AddressOf PollThreadAddress)
PollThread.Start()
End Sub

Private Sub PollThreadAddress()
frm1PollDatabase.TopMost = True
frm1PollDatabase.ShowDialog()
End Sub

//Button Click inside frm1PollDatabase that raises an event inside the
Parent Form//
Delegate Sub FromMyPage()
Dim MyDeleg As FromMyPage

Private Sub ApplyMyPage
MyDeleg = New FromMyPage(AddressOf DelegateFromMyPage)
MyDeleg.Invoke()
End Sub

Private Sub DelegateFromMyPage()
frm1Modify.showdialog
End Sub

** frm1PollDatabase is created inside a new thread and must always be on
top, which works fine. But when the user wants to send that data from
this
form to a new form (frm1Modify) the form is displayed but frm1PollDatabase
becomes disabled because of the Showdialog method. I cannot use show for
this form to be displayed. If the original thread (Main) displays this
form
wont it prevent this problem? The above code did not keep
frm1PollDatabase
from being disabled.

All of your UI code needs to be on one thread. That's probably the problem.

John Saunders
 
I

Ian Griffiths [C# MVP]

You seem to be breaking the golden rule of threading in Windows Forms
applications: never touch UI objects from any thread other than the thread
on which they were created.

This article discusses how to use multiple threads safely (i.e. without
breaking this rule) in a Windows Forms application:

http://msdn.microsoft.com/msdnmag/issues/03/02/Multithreading/

There's also a good discussion here:

http://www.yoda.arachsys.com/csharp/threads/winforms.shtml

In fact if you plan on doing any multithreaded development in Windows Forms,
I'd recommend you read all of Jon Skeet's threading articles:

http://www.yoda.arachsys.com/csharp/threads/

You really need to understand pretty much all of the topics he covers in
these articles before you'll be able to write reliable multithreaded code in
..NET.
 
C

Cor Ligthert

Charles,

Did I ever showed you this sample of my.

Before you become confuse about it, be aware this that it shows two the same
forms (form2), one with multithreading and one without.

Normally I send only the Google link, however I have the idea Google has
since today shorten his newsgroup service and is it not possible anymore to
show only one message.

\\\needs on form 1 one button and three textboxes
Private WithEvents frm1 As Form2
Private Delegate Sub Frm1Handler(ByVal message As String)
Private WithEvents frm2 As Form2
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()
frm2 = New Form2
frm2.itstop = Me.Top
frm2.itsleft = Me.Left + 400
frm2.Text = "In own thread"
AddHandler frm1.ready, AddressOf Frm2Ready
frm2.Show()
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 frm2ready(ByVal message As String)
TextBox3.Text = message
frm2.Dispose()
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
 
H

Herfried K. Wagner [MVP]

Cor Ligthert said:
Normally I send only the Google link, however I have the idea Google has
since today shorten his newsgroup service and is it not possible anymore
to show only one message.

That's still possible:

Click the "Show original" link of the message, for example:

<URL:http://groups-beta.google.com/group...anguages.vb/msg/84779a2037f1cae6?dmode=source>

Then remove the "?dmode=source" part.

Notice that these links have one drawback: The URL will change in future
when the new groups interface will be released, so URLs will certainly
break. Another alternative is using the German interface for Google Groups
which is still the "old" version.
 

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

Similar Threads


Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,007
Latest member
obedient dusk

Latest Threads

Top