Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / .NET Framework / New Users / December 2004

Tip: Looking for answers? Try searching our database.

Threading Part 2

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Charles A. Lackman - 01 Dec 2004 19:48 GMT
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
John Saunders - 01 Dec 2004 21:10 GMT
> Hello and thank you for your assistance.
>
[quoted text clipped - 37 lines]
> frm1PollDatabase
> from being disabled.

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

John Saunders
Ian Griffiths [C# MVP] - 02 Dec 2004 10:02 GMT
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.

Signature

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

> Hello and thank you for your assistance.
>
[quoted text clipped - 37 lines]
> frm1PollDatabase
> from being disabled.
Cor Ligthert - 02 Dec 2004 12:20 GMT
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
Herfried K. Wagner [MVP] - 02 Dec 2004 13:26 GMT
"Cor Ligthert" <notmyfirstname@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/847
79a2037f1cae6?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.

Signature

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


Rate this thread:







Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.