winform程序两个窗体间同步数据(二): 子窗体和线程实现
来源:互联网 发布:淘宝网涵元服饰 编辑:程序博客网 时间:2024/06/06 03:23
一 : 显示效果
二 代码
1 入口程序
using System;using System.Collections.Generic;using System.Linq;using System.Windows.Forms;namespace WindowsFormsApplication1{ static class Program { /// <summary> /// 应用程序的主入口点。 /// </summary> [STAThread] static void Main() { Application.EnableVisualStyles(); Application.SetCompatibleTextRenderingDefault(false); Application.Run(new ParentFrm());//启动父窗体 } }}
2 父窗体
using System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using System.Drawing;using System.Linq;using System.Text;using System.Threading;using System.Threading.Tasks;using System.Windows.Forms;namespace WindowsFormsApplication1{ public partial class ParentFrm : Form { public ParentFrm() { InitializeComponent(); } private void button1_Click(object sender, EventArgs e)//点击事件 { ChildFrm childFrm = new ChildFrm(); childFrm.Show();//显示子窗体 Thread thread = new Thread(() => { while (true) { this.TbParent.Invoke(new Action(() => { this.TbParent.Text = childFrm.str; }));//夸线程访问,取出子窗体(childFrm)的属性值(str)放入父窗体的TEXTBOX控件中显示 } }); thread.Start();//启动线程 } }}
3 子窗体
using System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using System.Drawing;using System.Linq;using System.Text;using System.Threading.Tasks;using System.Windows.Forms;namespace WindowsFormsApplication1{ public partial class ChildFrm : Form { public string str; public ChildFrm() { InitializeComponent(); } private void TbChild_TextChanged(object sender, EventArgs e) { this.str = this.TbChild.Text;//将用户输入到子窗体TEXTBOX控件中的内容放入到子窗体的属性值(str)里 } }}
三 问题
是否可以去掉多线程?
多线程很像之前的web 聊天程序里的长轮询,父窗口不停地去请请求子窗口里的数据。如果改成子窗体主动发送数据就可以去掉多线程?
0 0
- winform程序两个窗体间同步数据(二): 子窗体和线程实现
- winform程序两个窗体间同步数据(一): 静态变量和线程实现
- winform程序两个窗体间同步数据(三):建立父窗口与子窗口的父子关系(不使用线程)
- winform程序三个窗体间同步数据(四):建立兄弟关系(不适用线程)
- winform程序三个窗体间同步数据(五):接口实现多态减少代码(观察者模式)
- Winform中如何实现父窗体传递数据到子窗体并刷新子窗体
- winform程序三个窗体间同步数据(六):利用多播委托同步数据
- WinForm窗体间传递数据(二)
- 【转载】C# winform在两个子窗体间传递参数
- C# Winform实现MDI程序子窗体与主窗体菜单合并
- 在winForm 中实现两个窗体间的数据传递(转自Codeproject)
- 两个窗体间实现数据传递
- winform 子窗体数据改变刷新父窗体
- Winform中如何实现子窗体刷新父窗体
- WinForm 窗体间传递数据
- Winform窗体间传递数据
- winform窗体间数据传递
- WinForm子窗体和母窗体的交互
- UVA10763:Foreign Exchange(交换学生)
- javascript字符串
- 解密ThreadLocal
- CSS透明度兼容写法
- Android调用系统摄像头和相册遇到大图片处理的解决方案
- winform程序两个窗体间同步数据(二): 子窗体和线程实现
- 2.2.2打印X图优化
- 屏幕适配
- 数学分布
- linux shell的here document用法(cat << EOF)
- 前端,不要太过依赖JQuery库(二)
- Leetcode 5. Longest Palindromic Substring
- Path Sum II
- HCTF GAME RE-Windows礼包