| | |
openin more forms C#
Please support our C# advertiser: Intel Parallel Studio Home
Thread Solved |
•
•
Join Date: Jul 2008
Posts: 27
Reputation:
Solved Threads: 0
Well i have one main form which i run like an application.On that main form i have 3 buttons which are opening another 3 forms(instances of Form2 class) which are of the same class Form2.
So i have :
Form2 form1=new Form2();
Form2 form2=new Form2();
Form2 form3=new Form2();
all of 3 instances are opened on the button click event on the mainForm.
I also have 3 backgroundworkers which are collecting data and passing them to the instances of forms with set,get properties.One background worker is for one instance.
My problem is when I open one of the instances everything works fine the instance is updated and is drawn just fine.I can see that the background worker is passing data to the instance,BUT the problem is when i close it and try to open an another instance everything freezes.The visual studio doesn't report any error everything stops.The instance that i have tried to open is beeing opened but everything is grey.
I open a instance with :form1.Visible=true
form1.show()
and i hide it with;
form1.Visible=false;
form1.Hide();
Each instance is opened and closed like this but something is not working well!!!
One more thing that might be important is that every form is updated with an event when i finish gathering the informations for drawing!!!!
Help please!!!!
So i have :
Form2 form1=new Form2();
Form2 form2=new Form2();
Form2 form3=new Form2();
all of 3 instances are opened on the button click event on the mainForm.
I also have 3 backgroundworkers which are collecting data and passing them to the instances of forms with set,get properties.One background worker is for one instance.
My problem is when I open one of the instances everything works fine the instance is updated and is drawn just fine.I can see that the background worker is passing data to the instance,BUT the problem is when i close it and try to open an another instance everything freezes.The visual studio doesn't report any error everything stops.The instance that i have tried to open is beeing opened but everything is grey.
I open a instance with :form1.Visible=true
form1.show()
and i hide it with;
form1.Visible=false;
form1.Hide();
Each instance is opened and closed like this but something is not working well!!!
One more thing that might be important is that every form is updated with an event when i finish gathering the informations for drawing!!!!
Help please!!!!
•
•
Join Date: May 2008
Posts: 538
Reputation:
Solved Threads: 86
So if I understand it correcly, your application has 4 windows. The main window and 3 other forms. The 3 forms are updated by backgroud worker threads.
Does the program always create all 3 forms, even if they are not visible?
Does the program always start the 3 background threads, or does it wait until the associated window is set to visible?
If the thread starts when the window is set to visible, are there any provisions to suspend or terminate the thread when the window is hidden?
Can you identify whether it is the close of the first window or the open of the second window that is causing your lockup?
How does the background thread interface with the window?
As I say the above, I seem to remember that you can (or could) generate problems when making calls into methods of the window (form) from more than one thread.
Does the program always create all 3 forms, even if they are not visible?
Does the program always start the 3 background threads, or does it wait until the associated window is set to visible?
If the thread starts when the window is set to visible, are there any provisions to suspend or terminate the thread when the window is hidden?
Can you identify whether it is the close of the first window or the open of the second window that is causing your lockup?
How does the background thread interface with the window?
As I say the above, I seem to remember that you can (or could) generate problems when making calls into methods of the window (form) from more than one thread.
•
•
Join Date: Aug 2008
Posts: 1,735
Reputation:
Solved Threads: 186
Depends a bit I guess on what exactly is being done in the background and how you've passed the forms reference to the worker thread so it knows which form to update.
Did I just hear "You gotta help us, Doc. We've tried nothin' and we're all out of ideas" ? Is this you? Dont let this be you! I will put in as much effort as you seem to.
•
•
Join Date: Jul 2008
Posts: 27
Reputation:
Solved Threads: 0
Thank you for your effort i will try to answer your questions the best i can.
1.the main program only creates the main form from which 3 other forms are opened.
Each of the background workers create one instance of Form2.
all 3 background workers are started even before the main application starts(main form).after the background worker does instance of Form2 and some other variables it goes in the loop where he collects some informations and after that i call an event which updates the instance of form.The background worker is all the time collecting data and updating the instance so he never shuts down or stops.He is in infinite loop.
The identification of problem.
When i open one of those 3 windows from main window and when i close it.If i open the same window(same instance) again everything is OK but when i open a window and close it and try to open an ANOTHER window(another instance of Form2) everything stops.
Each of 3 windows are opened with button click events from main form,and the background workers are started immideatly and are running all the time collecting data from 3 instances. when the user decides to show them they should be shown with the latest data from background worker.That is the main idea.
Thanks for your help and i hope this will give you a clue of my problem
1.the main program only creates the main form from which 3 other forms are opened.
Each of the background workers create one instance of Form2.
all 3 background workers are started even before the main application starts(main form).after the background worker does instance of Form2 and some other variables it goes in the loop where he collects some informations and after that i call an event which updates the instance of form.The background worker is all the time collecting data and updating the instance so he never shuts down or stops.He is in infinite loop.
The identification of problem.
When i open one of those 3 windows from main window and when i close it.If i open the same window(same instance) again everything is OK but when i open a window and close it and try to open an ANOTHER window(another instance of Form2) everything stops.
Each of 3 windows are opened with button click events from main form,and the background workers are started immideatly and are running all the time collecting data from 3 instances. when the user decides to show them they should be shown with the latest data from background worker.That is the main idea.
Thanks for your help and i hope this will give you a clue of my problem
•
•
Join Date: Aug 2008
Posts: 1,735
Reputation:
Solved Threads: 186
I believe the issue must be how you are assigning your forms to the worker threads (as hinted above).
However, you may need to take into account that your "form2" is not being reset each time, so, when it is reshown, it was at the end of whatever it was doing - this can have bearing as your logic may not have allowed for that.
However, you may need to take into account that your "form2" is not being reset each time, so, when it is reshown, it was at the end of whatever it was doing - this can have bearing as your logic may not have allowed for that.
Did I just hear "You gotta help us, Doc. We've tried nothin' and we're all out of ideas" ? Is this you? Dont let this be you! I will put in as much effort as you seem to.
•
•
Join Date: Jul 2008
Posts: 27
Reputation:
Solved Threads: 0
•
•
•
•
I believe the issue must be how you are assigning your forms to the worker threads (as hinted above).
However, you may need to take into account that your "form2" is not being reset each time, so, when it is reshown, it was at the end of whatever it was doing - this can have bearing as your logic may not have allowed for that.
Well instance of Form2 is being made in Background worker class as public static.Each background worker has his own class.ALL the data is beeing sent to the Form2 class via get,set properties also the instance of Form2 is beeing sent to the Form2 class via get,set.
Everything in the Form2 class all variables are private.
So each background worker has his own instance of form2 and is updating it with data.In Form2 class is beeing called an event when the data is collected and instance is then beeing updated.Well the problem is that i can open one instance and close it as many times as i won't,but when i open one of them and close it and when i try to open ANOTHER one (not the same instance all the time) then everything stops.It looks like when i open first instance that somehow i make it Active or primare and when i close it and try to open another one it won't let me.
•
•
Join Date: Aug 2008
Posts: 1,735
Reputation:
Solved Threads: 186
This is the bit that worries me
if your worker makes those forms then how is it sure which form its using?
C# Syntax (Toggle Plain Text)
Form2 form1=new Form2(); Form2 form2=new Form2(); Form2 form3=new Form2();
if your worker makes those forms then how is it sure which form its using?
Did I just hear "You gotta help us, Doc. We've tried nothin' and we're all out of ideas" ? Is this you? Dont let this be you! I will put in as much effort as you seem to.
•
•
Join Date: Jul 2008
Posts: 27
Reputation:
Solved Threads: 0
•
•
•
•
This is the bit that worries me
C# Syntax (Toggle Plain Text)
Form2 form1=new Form2(); Form2 form2=new Form2(); Form2 form3=new Form2();
if your worker makes those forms then how is it sure which form its using?
this code is in main:
BackgroundWorker bw1 = new BackgroundWorker();
bw1.DoWork += new DoWorkEventHandler(BWClass1.bw1_DoWork);
this code down is in BWClass1!!!!
public static Form2 form1;
public static void bw1_DoWork(object sender, DoWorkEventArgs e)
{
form1=new Form2(0);
}
this code down is in BWClass2!!!!
the second background worker those the same but for
public static void bw2_DoWork(object sender, DoWorkEventArgs e)
{
form2=new Form2(1);
}
each background worker is using his one instance in function.
in constructor i am passing an argument Form2(1) which says which form should he draw and which form to update
i am doing the update of instance of Form2 in each backgroundworker for each form with get,set properties.For an example:
backgroundworker1:
form1.TEMP_PROFIL1 =temperature1;
form1.TEMP_PROFIL2 =temperature2;
form1.SREDNJA_TEMP = average_temp;
form1.MAKS_TEMP = max_temp;
backgroundworker2:
form2.TEMP_PROFIL1 =temperature1;
form2.TEMP_PROFIL2 =temperature2;
form2.SREDNJA_TEMP = average_temp;
form2.MAKS_TEMP = max_temp;
after i do my update I call an event which will draw an panel on specified Form2 instance
form1.clicked();
or
form2.clicked();
the function of the event is declared in Form2 class!!!
Hopes this clarifies a bit more!!!
•
•
Join Date: Aug 2008
Posts: 1,735
Reputation:
Solved Threads: 186
OK, I dont see any thread handling in your code to check for the fact a UI cant be updated by anything other than the main thread.
I also am a little confused as to the Form2(0) and Form2(1) Im not sure what these parameters are for.
I also am a little confused as to the Form2(0) and Form2(1) Im not sure what these parameters are for.
Did I just hear "You gotta help us, Doc. We've tried nothin' and we're all out of ideas" ? Is this you? Dont let this be you! I will put in as much effort as you seem to.
•
•
Join Date: Jul 2008
Posts: 27
Reputation:
Solved Threads: 0
Form2(0) and Form2(1).
I use this argument because variables that the constructor needs to draw (sizes and stuff) are in a array so in this way the constructor knows that for first instance he must take variable of array[0] and for second instance variable from array[1].
I'm sorry i'm pretty new in all of this so i don't know what is UI?
And for thread handling i'm not sure what i need to use for background workers because i would like if they are all 3 working all the time and every one of them is updating a different instance of Form2.The only code they are using all 3 is the code in Form2 class for the event which is drawing on instances of Form2
I use this argument because variables that the constructor needs to draw (sizes and stuff) are in a array so in this way the constructor knows that for first instance he must take variable of array[0] and for second instance variable from array[1].
I'm sorry i'm pretty new in all of this so i don't know what is UI?

And for thread handling i'm not sure what i need to use for background workers because i would like if they are all 3 working all the time and every one of them is updating a different instance of Form2.The only code they are using all 3 is the code in Form2 class for the event which is drawing on instances of Form2
![]() |
Other Threads in the C# Forum
- Previous Thread: Simpel string sorting
- Next Thread: How to sort a List of FileInfo just like in a Windows folder? (Same sort Function)
| Thread Tools | Search this Thread |
.net access algorithm array asp barchart bitmap box broadcast c# check checkbox client column combobox control conversion csharp custom database databasesearch datagrid datagridview datagridviewcheckbox dataset datetime degrees development display draganddrop drawing encryption enum equation excel file form format formatting formbox forms formupdate function gdi+ image index input install java label linux list listbox mandelbrot math mouseclick mysql namevaluepairs networking operator packaging path photoshop picturebox pixelinversion post powerpacks programming radians regex remoting reporting richtextbox robot server sleep socket sql statistics stream string table text textbox thread time timer transform treeview update usercontrol validation visualstudio wait webbrowser wfa windows winforms wpf xml






