Click here to Skip to main content
15,888,254 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have program that uses BackgroundWorker (BW) to periodically query GPS hardware to obtain position information. It uses a single Form for the entire operation. I am expanding the project to include two BWs, one high rate to query an IMU and another slower BW to query the GPS.

I am trying to expand the project further to add an initialization winform that initializes the hardware and continues to query the GPS until a GPS fix is obtained.

I have tested a program to switch between forms by issuing a form.hide() on the base form and then doing a form.show() on the second form. This works except that when I do the form.show() it doesn't execute the code in the main method
public Form2()
{
Initialize Component();
*** this code doesn't get executed
}

That is the first problem and it may be that I have to reinstanciate the form.

The bigger issue is that Form1 (the base form on startup) and Form 2 are I believe in different threads. Form2 still has to use a BW to periodically query the GPS and INS after they have been initiated until a fix is obtained. I store the GPS and INS data in a public static class so they are available to all the other programs/classes, etc. I don't think I can use the BW functions from Form1 because I use the WorkCompleted event to write data to Form1. Can I start a separate set of BWs in Form2 to provide this function and will that interfere with the BWs in From1? I'd start the BWs in Form2 use them until every thing was properly initialized and the stop the Form2 BWs and go to Form1, stat the BW tasks there and do the data collection, display and storage in Form1. All this seems very complicated and it might be better to just pack all the initialization and display output on Form1, forget about Form2, and used methods in Form1 for the initialization, display and storage tasks.

Any other suggestions?

What I have tried:

I have been experimenting with multiple BWs and the seem to work properly. I've also had limited success in testing multiple forms in a small C# program.
Posted
Updated 17-Dec-16 5:35am
Comments
[no name] 17-Dec-16 11:37am    
"form.show() it doesn't execute the code ", and it won't. Show doesn't instantiate the form.
"Form 2 are I believe in different threads", you "believe"? Why don't you KNOW?
"seems very complicated ", yes it does. Looks to me that you are overcomplicating things.
"Any other suggestions", well my suggestion, since you don't know the difference between a Main method and a constructor, you should get yourself a basic book on programming and work through it before tackling more advanced topics like multithreading.

1 solution

It doesn't get executed when you do form.Show() because it's part of the constructor code: it gets executed when the form is created by
C#
Form2 form = new Form2();
And at no other time.
Move the code from the constructor to the Shown event and it will be executed the first (and only the first) time the form is actually displayed.
 
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