使用多种形式和后台工作者 [英] Using multiple forms and backgroundworker

查看:74
本文介绍了使用多种形式和后台工作者的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有程序使用BackgroundWorker(BW)定期查询GPS硬件以获取位置信息。它使用单个Form进行整个操作。我正在扩展项目,包括两个BW,一个高速查询IMU,另一个慢速BW查询GPS。



我试图进一步扩展项目添加初始化winform,初始化硬件并继续查询GPS,直到获得GPS修复。



我测试了一个程序,通过发出一个表单来切换表单在基础表单上的form.hide(),然后在第二个表单上执行form.show()。这样做除了当我执行form.show()时它不执行main方法中的代码

public Form2()

{

初始化组件();

***此代码未执行

}



这是第一个问题,可能是我必须重新注册表格。



更大的问题是Form1(启动时的基本表格)和表格2我相信不同的主题。 Form2仍然必须使用BW在它们被启动之后定期查询GPS和INS,直到获得修复。我将GPS和INS数据存储在公共静态类中,因此它们可用于所有其他程序/类等。我认为我不能使用Form1中的BW函数,因为我使用WorkCompleted事件将数据写入Form1 。我可以在Form2中启动一组单独的BW来提供此功能吗?这会干扰From1中的BW吗?我将在Form2中启动BW,直到每个东西都被正确初始化并停止Form2 BW并转到Form1,在那里统计BW任务并在Form1中进行数据收集,显示和存储。所有这些看起来都非常复杂,最好只在Form1上打包所有初始化和显示输出,忘记Form2,并在Form1中使用方法进行初始化,显示和存储任务。



还有其他建议吗?



我尝试过:



我一直在试验多个BW,似乎工作正常。我在小型C#程序中测试多个表单的成功也很有限。

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.

推荐答案

当你执行表单时它不会被执行.Show()因为它是构造函数代码的一部分:它在表单创建时执行

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
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.


这篇关于使用多种形式和后台工作者的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆