Click here to Skip to main content
15,901,505 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
hi,

I am trying to pass a string from one form to another form and want to display it in first form while sencond form is in running state.

i am very munch new to threads concept . please suggest me with some sample code.
Posted
Comments
pasupulety 12-Jan-12 23:07pm    
Its not the problem of data transfer . i want both the forms to run in parallel and the process going on in form should display parallely in another form.

in sencond form i am trying to compile set of cpp(may be thousands of files) files and i need to display error or success status of each file compilation to first form WHILE COMPILATION IS GOING ON IN SECOND FORM .

This has nothing to do with threads - it is purely a data transfer between objects problem.

In order to transfer data between two forms (assume Form1 has the new data, and Form2 should end up receiving it), you either need
1) To know the instance of Form2 you want to transfer to in Form1
or
2) To get Form2 to subscribe to an event in the instance of Form1 that will have the data.

If you do this in Form1:
C#
Form2 f = new Form2();
f.Show();
Then you can use the first method.
1) Save the Form2 instance in a class level variable in Form1 (this just means moving it outside any method code and putting "private" in front of it.
2) Set up a Property in Form2 to receive the data, and handle it.
3) When you want to give the data to Form2, you just set the property value, using the class level variable.

If you don't, then you will need to set up and signal an event in Form2, and set the Form1 instance to handle it, wherever you do create Form2 and Form2.
This is a little more complex (but a much better solution) - I can give you the details if you need them.
 
Share this answer
 
Comments
Manfred Rudolf Bihy 12-Jan-12 8:50am    
I was just about to write something along these lines too. 5!
*Disgustedly rummages in drawer and puts tin foil hat on gain*
Wonde Tadesse 12-Jan-12 18:19pm    
5+
It has nothing to do with threads.

This is the popular question about form collaboration. The most robust solution is implementation of an appropriate interface in form class and passing the interface reference instead of reference to a "whole instance" of a Form. Please see my past solution for more detail: How to copy all the items between listboxes in two forms[^].

As to using threading with Forms, this is a separate story. Please see my past solutions:
Control.Invoke() vs. Control.BeginInvoke()[^],
Problem with Treeview Scanner And MD5[^].

—SA
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900