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!!!!
Loading
Bechir BejaouiPosted Dec 6, 2008, 4:30 AM
Nikola RosicPosted Dec 3, 2008, 2:56 AM
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.
Each background worker is beeing made in static void main but they functions are in seperate classes and there is everything beeing done.
As i have said before each background worker makes his instance of Form2 which is he updating.So every instance is beeing made in seperate class as public static!!!
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.
code part i think that matters:
well the first background worker
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!!!
Thanks for your help and i hope this will give you a clue of my problem
Bechir BejaouiPosted Dec 2, 2008, 4:14 PM
is the backgroundworked is created within the pages or in a separate classes