By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,203 Members | 1,549 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,203 IT Pros & Developers. It's quick & easy.

Threading Part 2

P: n/a
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

Jul 21 '05 #1
Share this Question
Share on Google+
4 Replies


P: n/a
"Charles A. Lackman" <Ch*****@CreateItSoftware.net> wrote in message
news:Oy**************@TK2MSFTNGP12.phx.gbl...
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
Jul 21 '05 #2

P: n/a
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/is...ultithreading/

There's also a good discussion here:

http://www.yoda.arachsys.com/csharp/...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.

--
Ian Griffiths - http://www.interact-sw.co.uk/iangblog/
DevelopMentor - http://www.develop.com/

"Charles A. Lackman" wrote:
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.

Jul 21 '05 #3

P: n/a
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

Jul 21 '05 #4

P: n/a
"Cor Ligthert" <no************@planet.nl> schrieb:
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/microsoft.public.dotnet.languages.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.

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://dotnet.mvps.org/dotnet/faqs/>

Jul 21 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.