how to call a form from a thread

S

Suhail Kaleem

Hi !
I can call form2 from main thread but when i create a child thread i cannot
call form like

Dim frm as new form2
frm.visible = true

now this only works in mian thread so my question how do call a form from a
child thread ?

Thanks
 
H

Herfried K. Wagner [MVP]

Suhail Kaleem said:
I can call form2 from main thread but when i create a child
thread i cannot call form like

Dim frm as new form2
frm.visible = true

now this only works in mian thread so my question how do call
a form from a child thread ?

Always show your forms in the app's main UI thread and use 'Control.Invoke'
to communicate in the thread -> UI direction:

Multithreading:

<URL:http://msdn.microsoft.com/library/en-us/dnforms/html/winforms06112002.asp>
<URL:http://msdn.microsoft.com/library/en-us/dnforms/html/winforms08162002.asp>
<URL:http://msdn.microsoft.com/library/en-us/dnforms/html/winforms01232003.asp>

<URL:http://www.devx.com/dotnet/Article/11358/>

<URL:http://msdn.microsoft.com/library/e...SystemWindowsFormsControlClassInvokeTopic.asp>

Multithreading in Visual Basic .NET (Visual Basic Language Concepts)
<URL:http://msdn.microsoft.com/library/en-us/vbcn7/html/vaconthreadinginvisualbasic.asp>

Sample:

<URL:http://dotnet.mvps.org/dotnet/samples/filesystem/downloads/FileSystemEnumerator.zip>
 
S

Suhail Kaleem

Hi !
i read the articles but the code still does not work
if uncomment the msgbox the msgbox shows and then frm2 is displayed but
if

comment it frm2 is not displayed ?
and always no invoke is required sub shownotify is not excuted ?
i tired thread.sleep and thread.spinwait but still not working ?


Dim ca As New mailArgs
ca.val = not_text
ca.StartGMail = AddressOf notification
Dim t2 As New Thread(AddressOf ca.StartMail)
t2.IsBackground = True
t2.Start()
t2.Name = "NOTIFICATION"


Sub notification(ByVal val As String)
Dim del As MyDelSub
del = New MyDelSub(AddressOf shownotify)
Dim noti As New frmnotify
If noti.InvokeRequired Then
del.BeginInvoke(val, Nothing, Nothing)

Else
Debug.WriteLine("no invoke ")
noti.Timer3.Interval = 3000
noti.Label1.Text = val
noti.Show()

' MsgBox("i am here")
' if i uncomment the above line it works but if i comment it ,it
does not work ?
End If


End Sub


Private Sub shownotify(ByVal val As String)
Dim noti As New frmnotify
noti.Timer3.Interval = 3000
noti.Label1.Text = val
noti.Show()
Debug.WriteLine("I AM HERE - invoked")

End Sub
 
J

Jon Skeet [C# MVP]

Suhail Kaleem said:
i read the articles but the code still does not work
if uncomment the msgbox the msgbox shows and then frm2 is displayed but
if

comment it frm2 is not displayed ?

You're creating a new form in the notification method, rather than
using the existing one. Is that deliberate? Usually you want to update
an existing form from another thread, not create a new one. If you
create a new one, you need to run a message pump on that thread too.
 
S

Suhail Kaleem

Hi !
Well i have it working but noiw how could i pass parameter to the method
FireNotification





Dim t As Thread
Delegate Sub Notification()

Private Sub btnButton1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles btnButton1.Click
Debug.WriteLine("Thread Init")
t = New Thread(AddressOf ThreadWorker)
t.Name = "Test"
t.Start()
Debug.WriteLine("Thread Start")
End Sub

Public Sub ThreadWorker()
Debug.WriteLine("Thread Worker Start")
t.Sleep(3000)
Debug.WriteLine("Thread Worker : Generate Window")
Me.Invoke(New Notification(AddressOf FireNotification))
Debug.WriteLine("Thread Worker : Window Generated - Pause Again")
t.Sleep(5000)
Debug.WriteLine("Thread Worker : Finished")
End Sub

Private Sub FireNotification()
Dim frm As New DataForm1
Debug.WriteLine("Delegate : Generate Window")
frm.Show()
Debug.WriteLine("Delegate : Window Generated")
End Sub




Thnaks
 
S

Suhail Kaleem

Hi !
Thanks i have it working now
Thanks for all your help and my application is also comomplete once relased
i will post it here :)

Thanks
Suhail kaleem
 

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