473,396 Members | 1,907 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Accessing a form from a thread running on a second form

11
Hi,

I have a main form which starts up a thread as well as a 2nd form.

How do I make it so that I can access things such as labels on the 2nd form from the thread that was started on form1?

I have some sample code below...
Expand|Select|Wrap|Line Numbers
  1. Public Class Form1
  2.  
  3.     Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
  4.         Form2.Show()
  5.         Form2.BringToFront()
  6.         signal = New Class1
  7.     End Sub
  8.     Private signal As Class1
  9.  
  10. End Class
  11.  
  12.  
  13. Public Class Class1
  14.     Public Sub New()
  15.         trd = New Threading.Thread(AddressOf task)
  16.         trd.IsBackground = True
  17.         trd.Start()
  18.     End Sub
  19.     Private trd As Threading.Thread
  20.     Private Sub task()
  21.         For i As Integer = 0 To 5
  22.             Threading.Thread.Sleep(5000)
  23.             Form2.Label1.Text = i.ToString
  24.         Next
  25.     End Sub
  26. End Class
  27.  
Any help would be much appreciated.

Thanks,

Wagz
Mar 4 '08 #1
7 1291
Shashi Sadasivan
1,435 Expert 1GB
have you tried passing the instance of Form1 to both Form2 and signal ?
Mar 4 '08 #2
Wagz
11
It appears that every time I'm in the created thread and call something from form2 that it creates a new instance of the form. How do I reference the form that form1 created from the created thread?
Mar 4 '08 #3
The best approach to this would be to create an event in Class1 that gets raised whenever you want to indicate progress from your thread.

Expand|Select|Wrap|Line Numbers
  1.         For i As Integer = 0 To 5
  2.             Threading.Thread.Sleep(5000)
  3.             ' Form2.Label1.Text = i.ToString
  4.             ' singal event here
  5.         Next
  6.  
Then from your Form1 class you can handle the event and then set your label for form2

Expand|Select|Wrap|Line Numbers
  1. Private Sub SomeEventHandler
  2.    ' If Me.InvokeRequired
  3.    ' Me.Invoke(new EventHandler(SomeEventHandler))
  4.    ' End If
  5.    '
  6.    ' Set label for Form2
  7. End Sub
  8.  
Note that you need to invoke back to the main thread in order to set the label for Form2.

I hope this helps, it is pseudo code but should hopefully explain what you need to do.

Hi,

I have a main form which starts up a thread as well as a 2nd form.

How do I make it so that I can access things such as labels on the 2nd form from the thread that was started on form1?

I have some sample code below...
Expand|Select|Wrap|Line Numbers
  1. Public Class Form1
  2.  
  3.     Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
  4.         Form2.Show()
  5.         Form2.BringToFront()
  6.         signal = New Class1
  7.     End Sub
  8.     Private signal As Class1
  9.  
  10. End Class
  11.  
  12.  
  13. Public Class Class1
  14.     Public Sub New()
  15.         trd = New Threading.Thread(AddressOf task)
  16.         trd.IsBackground = True
  17.         trd.Start()
  18.     End Sub
  19.     Private trd As Threading.Thread
  20.     Private Sub task()
  21.         For i As Integer = 0 To 5
  22.             Threading.Thread.Sleep(5000)
  23.             Form2.Label1.Text = i.ToString
  24.         Next
  25.     End Sub
  26. End Class
  27.  
Any help would be much appreciated.

Thanks,

Wagz
Mar 4 '08 #4
Wagz
11
If I pass Form2 to Class1 when I create it and create a delegate to handle the changing the text then it works.

I passed it in to Class1 in the constructor, is there a better way to do this? In the future I'd like to be able to possibly add additional forms and therefore don't necessarily want to have to have to modify the class every time I create a new form.

Below is the code I used:

Expand|Select|Wrap|Line Numbers
  1. Public Class Class1
  2.     Public Sub New(ByVal Form2)
  3.         trd = New Threading.Thread(AddressOf task)
  4.         trd.IsBackground = True
  5.         trd.Start()
  6.         Me.Form2 = Form2
  7.     End Sub
  8.     Private Form2 As Form2
  9.     Private trd As Threading.Thread
  10.     Delegate Sub SetText(ByVal label As Windows.Forms.Label, ByVal text As String)
  11.     Private Sub task()
  12.         For i As Integer = 0 To 5
  13.             Threading.Thread.Sleep(5000)
  14.             Dim DelSetText As SetText = New SetText(AddressOf Form2.SetText)
  15.             Form2.Invoke(DelSetText, New Object(1) {Form2.Label1, i.ToString})
  16.         Next
  17.     End Sub
  18. End Class
  19.  
  20. Public Class Form1
  21.  
  22.     Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
  23.         Form2.Show()
  24.         Form2.BringToFront()
  25.         signal = New Class1(Form2)
  26.     End Sub
  27.     Private signal As Class1
  28.  
  29. End Class
  30.  
  31. Public Class Form2
  32.     Public Sub SetText(ByVal label As Windows.Forms.Label, ByVal item As String)
  33.         label.Text = item
  34.     End Sub
  35. End Class
Mar 4 '08 #5
Wagz
11
jjvainav, I couldn't get your method to work.

I raised the event in Class1 and handled it in Form1, but when I go to change the text in Form2 it creates a new instance of Form2.

Expand|Select|Wrap|Line Numbers
  1. Public Class Form1
  2.     Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
  3.         Form2.Show()
  4.         Form2.BringToFront()
  5.         signal = New Class1()
  6.     End Sub
  7.     Private WithEvents signal As Class1
  8.     Public Sub SetForm2Text(ByVal text As String) Handles signal.SetText
  9.         Form2.Label1.Text = text
  10.     End Sub
  11. End Class
  12.  
  13. Public Class Class1
  14.     Public Sub New()
  15.         trd = New Threading.Thread(AddressOf task)
  16.         trd.IsBackground = True
  17.         trd.Start()
  18.     End Sub
  19.     Public Event SetText(ByVal text As String)
  20.     Private trd As Threading.Thread
  21.     Private Sub task()
  22.         For i As Integer = 0 To 5
  23.             Threading.Thread.Sleep(5000)
  24.             RaiseEvent SetText(i.ToString)
  25.         Next
  26.     End Sub
  27. End Class
Mar 4 '08 #6
Try this:

Expand|Select|Wrap|Line Numbers
  1. Public Class Form1
  2.     Private Delegate Sub SetTextCallback(ByVal text As String)
  3.  
  4.     Private form2 As Form2
  5.  
  6.     Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
  7.         form2 = New Form2
  8.         form2.Show()
  9.         form2.BringToFront()
  10.         signal = New Class1()
  11.     End Sub
  12.     Private WithEvents signal As Class1
  13.     Public Sub SetForm2Text(ByVal text As String) Handles signal.SetText
  14.         If Me.InvokeRequired Then
  15.             Me.Invoke(New SetTextCallback(AddressOf SetForm2Text), text)
  16.         End If
  17.  
  18.         form2.Label1.Text = text
  19.     End Sub
  20. End Class
  21.  
  22. Public Class Class1
  23.     Public Sub New()
  24.         trd = New Threading.Thread(AddressOf task)
  25.         trd.IsBackground = True
  26.         trd.Start()
  27.     End Sub
  28.     Public Event SetText(ByVal text As String)
  29.     Private trd As Threading.Thread
  30.     Private Sub task()
  31.         For i As Integer = 0 To 5
  32.             Threading.Thread.Sleep(1000)
  33.             RaiseEvent SetText(i.ToString)
  34.         Next
  35.     End Sub
  36. End Class
  37.  
jjvainav, I couldn't get your method to work.

I raised the event in Class1 and handled it in Form1, but when I go to change the text in Form2 it creates a new instance of Form2.

Expand|Select|Wrap|Line Numbers
  1. Public Class Form1
  2.     Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
  3.         Form2.Show()
  4.         Form2.BringToFront()
  5.         signal = New Class1()
  6.     End Sub
  7.     Private WithEvents signal As Class1
  8.     Public Sub SetForm2Text(ByVal text As String) Handles signal.SetText
  9.         Form2.Label1.Text = text
  10.     End Sub
  11. End Class
  12.  
  13. Public Class Class1
  14.     Public Sub New()
  15.         trd = New Threading.Thread(AddressOf task)
  16.         trd.IsBackground = True
  17.         trd.Start()
  18.     End Sub
  19.     Public Event SetText(ByVal text As String)
  20.     Private trd As Threading.Thread
  21.     Private Sub task()
  22.         For i As Integer = 0 To 5
  23.             Threading.Thread.Sleep(5000)
  24.             RaiseEvent SetText(i.ToString)
  25.         Next
  26.     End Sub
  27. End Class
Mar 5 '08 #7
Shashi Sadasivan
1,435 Expert 1GB
All forms that you create inheri the base class Form.

If you can identify which forms will be sent (like you send Form1) to Class1
Also identify the methods that these forms will have in common,

Put them all into an interface, and pass the instance of the Form as the interface type you just created.

This way, you will only need to pull the interface type, and call the method, which will do the trick for you
Mar 5 '08 #8

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: bryhhh | last post by:
I have am still learning C#, so please bear with me. I have an application that needs to open a second form, but I need the second form to process message loops, whilst the original form also...
3
by: Brian Keating EI9FXB | last post by:
Hello again, I've already placed a few posts on this topic. This time i've a simple application that exhibits my problem, I've placed sample solution 8k on my website should anyone be interested...
13
by: Ioannis Vranos | last post by:
Why in this code the form *does not refresh* when it gets the focus/after some time? #using <mscorlib.dll> #using <system.windows.forms.dll> #using <system.dll> #using <system.drawing.dll>
9
by: Jervin Justin | last post by:
Hi, I've been having this problem for some time with Web Forms: I have a web app that sends data to a service when the user presses a button. Based on the data, the server will send several...
2
by: Nick | last post by:
Hi there, I'm creating a form using the NativeWindow class to process some custom messages. Unfortunately as it's created on the main thread of the application it doesnt recieve messages while...
10
by: morangolds | last post by:
Hi, I've been having a problem with C++ Windows Forms apps not "ending" when you close the form window. I've searched about this problem all over the place and most searches have lead me to...
12
by: titan nyquist | last post by:
I have a class with data and methods that use it. Everything is contained perfectly THE PROBLEM: A separate thread has to call a method in the current instantiation of this class. There is...
4
by: Lilith | last post by:
This was easy in C++ (well, relatively.) Now I'm in the C# environment and everything I try doesn't work. I'm running a thread in which I have two accesses I need to components on a form. In...
1
by: Franck | last post by:
I have a form with a loading bar as marquee so i don't need to play with the progress thing. It's just a form to show activity in other way. So what i need to do is start it at the beginning of...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.