| | |
openin more forms C#
Please support our C# advertiser: Intel Parallel Studio Home
Thread Solved |
•
•
Join Date: Aug 2008
Posts: 1,735
Reputation:
Solved Threads: 186
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.
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.
•
•
Join Date: Jul 2008
Posts: 27
Reputation:
Solved Threads: 0
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)?
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)?
•
•
Join Date: Aug 2008
Posts: 1,735
Reputation:
Solved Threads: 186
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.
•
•
Join Date: Jul 2008
Posts: 27
Reputation:
Solved Threads: 0
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
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
•
•
Join Date: Aug 2008
Posts: 1,735
Reputation:
Solved Threads: 186
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.
•
•
Join Date: Jul 2008
Posts: 27
Reputation:
Solved Threads: 0
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
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:
this is in class Form2:
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!!!
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
C# Syntax (Toggle Plain Text)
private void Button1_Click(object sender, EventArgs e) { form1.Visible = true; form1.Show(); }
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:
C# Syntax (Toggle Plain Text)
public void clicked() { if (MyEvent != null) { if (this.InvokeRequired) { this.BeginInvoke(this.MyEvent); } else { MyEvent(); } } }
Thanks!!!
Last edited by cscgal; Dec 5th, 2008 at 1:37 pm. Reason: Added code tags
•
•
Join Date: Aug 2008
Posts: 1,735
Reputation:
Solved Threads: 186
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()"
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.
•
•
Join Date: Jul 2008
Posts: 27
Reputation:
Solved Threads: 0
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;
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;
![]() |
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 angle array asp.net barchart bitmap box broadcast c# capturing check checkbox client combobox control conversion csharp custom database datagrid datagridview dataset datetime dbconnection degrees delegate design development disappear draganddrop drawing encryption enum eventhandlers excel file firefox form format forms function gdi+ image index input install java label leak libraries list listbox loop mandelbrot math monodevelop mouseclick msword mysql operator path pause photoshop picturebox pixelinversion post programming radians regex remoting resourcefile richtextbox round server sleep socket sql statistics stream string table tcpclientchannel text textbox thread time timer update usercontrol validation virtualization visualbasic visualstudio webbrowser windows winforms wpf xml






