openin more forms C#

Please support our C# advertiser: Intel Parallel Studio Home
Thread Solved

Join Date: Aug 2008
Posts: 1,735
Reputation: LizR has a spectacular aura about LizR has a spectacular aura about 
Solved Threads: 186
LizR LizR is offline Offline
Posting Virtuoso

Re: openin more forms C#

 
0
  #11
Dec 3rd, 2008
UI = User Interface

normally for example, if I were writing an app that was going to do anything of value, I have update routines on my form so that other things can send it data, and irrelevant of wether those things are threaded or not, my form sorts out the update. This is done through delegates.

You often run into issues as the UI cant be updated from anything other than the main thread - so if for example, you have a progress bar and you're working through a long CSV file verifying it, and loading data, and so on, you would call the updateprogressbar function which then checks if an invoke is required and then if it is, it invokes the main thread to do that job, otherwise it does it.
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.
Reply With Quote Quick reply to this message  
Join Date: Jul 2008
Posts: 27
Reputation: nikola.rosic is an unknown quantity at this point 
Solved Threads: 0
nikola.rosic nikola.rosic is offline Offline
Light Poster

Re: openin more forms C#

 
0
  #12
Dec 3rd, 2008
So if I understood this correctly. I shouldn't do update of an instance with get,set properties which have been defined in the Form2 class but after i gather all the informations in one cycle i should call a delegate which will update my instance from the main thread

But I use the main thread to run main screen form like an application from which i am opening my instances.

and one more question is should check the invoke required of what exactly (of an instance of Form2)?
Reply With Quote Quick reply to this message  
Join Date: Aug 2008
Posts: 1,735
Reputation: LizR has a spectacular aura about LizR has a spectacular aura about 
Solved Threads: 186
LizR LizR is offline Offline
Posting Virtuoso

Re: openin more forms C#

 
0
  #13
Dec 3rd, 2008
You should check for invoke on any UI component. its a good practice to get into so if you then do thread off activities you dont have to redo all your UI code. So, wether you're changing the position of a progress bar, the caption on a button, or even enabling/disabling, anything which belongs to the main thread in effect should be carefully done, as not doing so causes clashes.
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.
Reply With Quote Quick reply to this message  
Join Date: Jul 2008
Posts: 27
Reputation: nikola.rosic is an unknown quantity at this point 
Solved Threads: 0
nikola.rosic nikola.rosic is offline Offline
Light Poster

Re: openin more forms C#

 
0
  #14
Dec 4th, 2008
Thanks for your advice but I forgot to tell you that i already have the part of code which is checking the invoke required and I have the invoke.Before when i didn't had it the visual studio reported me an error and stopped the running of code.The problem now is that i don't get any report of an error or the visual studio stops the running of application.

Here is how i do it:

in background worker i do the call:

form.clicked();

and in the Form2 class i have:

public void clicked()
{
if (MyEvent != null)
{
if (this.InvokeRequired)
{
this.Invoke(this.MyEvent);
}
else
{
MyEvent();
}
}
}

and the function i have that does the update of form is

public void OnEvent()
{
}

as you have seen probebly in other 2 background workers i also have:

form2.clicked()

and in the 3BW:
form3.clicked();

form1 and form2 and form3 are of the same class FORM2.

BW1 updates form1
BW2 updates form2
BW3 updates form3

Thanks for the help
Reply With Quote Quick reply to this message  
Join Date: Aug 2008
Posts: 1,735
Reputation: LizR has a spectacular aura about LizR has a spectacular aura about 
Solved Threads: 186
LizR LizR is offline Offline
Posting Virtuoso

Re: openin more forms C#

 
0
  #15
Dec 4th, 2008
Well either somehow when you think youre closing the form the forms actually closed and gone, or, your still missing some of the thread handling..its hard to say with the code given.
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.
Reply With Quote Quick reply to this message  
Join Date: Jul 2008
Posts: 27
Reputation: nikola.rosic is an unknown quantity at this point 
Solved Threads: 0
nikola.rosic nikola.rosic is offline Offline
Light Poster

Re: openin more forms C#

 
0
  #16
Dec 4th, 2008
Thanks, i will put locks in the code that all 3 background workers use so maybe this will help

Also i will instance all 3 instances of Form2 in main() and not in the BW-s maybe this will give some effect
Reply With Quote Quick reply to this message  
Join Date: Aug 2008
Posts: 1,735
Reputation: LizR has a spectacular aura about LizR has a spectacular aura about 
Solved Threads: 186
LizR LizR is offline Offline
Posting Virtuoso

Re: openin more forms C#

 
0
  #17
Dec 4th, 2008
You already are, because it creates it as it sends it.
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.
Reply With Quote Quick reply to this message  
Join Date: Jul 2008
Posts: 27
Reputation: nikola.rosic is an unknown quantity at this point 
Solved Threads: 0
nikola.rosic nikola.rosic is offline Offline
Light Poster

Re: openin more forms C#

 
0
  #18
Dec 5th, 2008
Well i have tried what have you told me but unfortunately the problem is not here.I still got the freeze when i change the instances on the button click event of the main form application.

I'm going to try to give you more explanations of my code.

The first i have a main form which is the Application.Run(mainScreen)

On it there are only 2 buttons which when are clicked each of them is openig one instance of Class Form2.

i also have two background workers an they are both in infinitive loop collecting data and pasing it to one of the instances

BW1 is for form1 and BW2 is for form2.

i am passing the both forms via the e.Argument in the background worker call.

i also have an event which is called for each form when BW collects a package of data.The event updates the form and the BW collects than again and the process is in while(true) loop.


form the event i have beginInvoke and the visual studio doesn't report any error.

the instance is opened like this
  1. private void Button1_Click(object sender, EventArgs e)
  2. {
  3. form1.Visible = true;
  4. form1.Show();
  5. }
the second instance is opened the same.

They are created in void main().

I tried to create them in class MainScreen but nothing changed.



here is the code for event in background worker which updates the instance.

this is in BW1:

form1.MyEvent += new MyDelegate(form1.OnEvent);

form1.clicked();

this is in class Form2:
  1. public void clicked()
  2. {
  3. if (MyEvent != null)
  4. {
  5. if (this.InvokeRequired)
  6. {
  7. this.BeginInvoke(this.MyEvent);
  8. }
  9. else
  10. {
  11. MyEvent();
  12. }
  13. }
  14. }
If you need more of code say so but there is a big bunch of it so i don't no what to show


Thanks!!!
Last edited by cscgal; Dec 5th, 2008 at 1:37 pm. Reason: Added code tags
Reply With Quote Quick reply to this message  
Join Date: Aug 2008
Posts: 1,735
Reputation: LizR has a spectacular aura about LizR has a spectacular aura about 
Solved Threads: 186
LizR LizR is offline Offline
Posting Virtuoso

Re: openin more forms C#

 
0
  #19
Dec 5th, 2008
Still so many questions..

How do your background processes know not to work?
How is that clicked code running
Where did you do the adding of the delegate?
Whats in "MyEvent()"
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.
Reply With Quote Quick reply to this message  
Join Date: Jul 2008
Posts: 27
Reputation: nikola.rosic is an unknown quantity at this point 
Solved Threads: 0
nikola.rosic nikola.rosic is offline Offline
Light Poster

Re: openin more forms C#

 
0
  #20
Dec 8th, 2008
1.The background workers are working all the time.I thought that that's the idea that BGW are collecting data all the time.I'm not stopping them even when i click to open one of those 2 instances.

2. The clicked code is being called from the background worker after the collection of one package of data.The function clicked() is only called for checking the required of invoke.And from it i'm calling the MyEvent();

The adding of the delegate is in the Form2 class here is the code form the delegate and the event:

public delegate void MyDelegate(); is declared between the namspace and class
public event MyDelegate MyEvent;

and the adding of delagete is in the BGW:

form1.MyEvent += new MyDelegate(form1.OnEvent);

4.In function My event I'am depending on the collected data drawing a x-x graph on the form and changing the color of pictures of form2:

For example:

for the picture:
private PictureBox Zona29 = new PictureBox();

i'm doing the change:
Zona29.Image = Properties.Resources.Cesta_crvena;
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC