热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

开发笔记:C#线程中操作窗体控件

本文由编程笔记#小编为大家整理,主要介绍了C#线程中操作窗体控件相关的知识,希望对你有一定的参考价值。1.在线程中给textB
本文由编程笔记#小编为大家整理,主要介绍了C# 线程中操作窗体控件相关的知识,希望对你有一定的参考价值。



1. 在线程中给textBox1







































































































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;
using System.Threading;
namespace WindowsFormsApp12{ public partial class Form1 : Form { public Form1() { InitializeComponent(); }
private void Form1_Load(object sender, EventArgs e) {
// CheckForIllegalCrossThreadCalls = false; // Class1 cs1 = new Class1(); Thread th = new Thread(test1); th.Start(); // cs1.ShowEvent = test;
}
public void test1() {
// ShowlbDevTem("123"); textBox1.Text = "123";
}

private void button1_Click(object sender, EventArgs e) {
// ShowlbDevTem("112");
}
/* public void test(string str) {
ShowlbDevTem(str); } public delegate void SWTDelegate(string AddStr); //public delegate void ComsumerTextDelegate(int Index, string AddStr); public void ShowlbDevTem(string AddStr) { if (textBox1.InvokeRequired) { SWTDelegate pd = new SWTDelegate(ShowlbDevTem); textBox1.Invoke(pd, new object[] { AddStr }); } else { textBox1.Text = AddStr; //richTextBox1.AppendText(AddStr); } }
public void dataShow(int row, int column, string str) { row = row % 30; ShowMessage(dataGridViewX1, str, row, column);
}

delegate void ShowMessageDelegate(DataGridView dg, string message, int row, int column); private void ShowMessage(DataGridView dg, string message, int row, int column) { if (dg.InvokeRequired) { ShowMessageDelegate showMessageDelegate = ShowMessage; dg.Invoke(showMessageDelegate, new object[] { dg, message, row, column }); } else {
dg.Rows[row].Cells[column].Value = message; } } */

}}

运行结果:

线程中是不允许直接操作窗体空间的

怎么解决呢?

方法1: CheckForIllegalCrossThreadCalls = false;

运行ok

方法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.Tasks;using System.Windows.Forms;
using System.Threading;
namespace WindowsFormsApp12{ public partial class Form1 : Form { public Form1() { InitializeComponent(); }
private void Form1_Load(object sender, EventArgs e) {
//CheckForIllegalCrossThreadCalls = false; // Class1 cs1 = new Class1(); Thread th = new Thread(test1); th.Start(); // cs1.ShowEvent = test;
}
public void test1() {
ShowlbDevTem("123"); //textBox1.Text = "123";
}

private void button1_Click(object sender, EventArgs e) {
// ShowlbDevTem("112");
}
/* public void test(string str) {
ShowlbDevTem(str); }*/ public delegate void SWTDelegate(string AddStr); //public delegate void ComsumerTextDelegate(int Index, string AddStr); public void ShowlbDevTem(string AddStr) { if (textBox1.InvokeRequired) { SWTDelegate pd = new SWTDelegate(ShowlbDevTem); textBox1.Invoke(pd, new object[] { AddStr }); } else { textBox1.Text = AddStr; //richTextBox1.AppendText(AddStr); } }
/* public void dataShow(int row, int column, string str) { row = row % 30; ShowMessage(dataGridViewX1, str, row, column);
}

delegate void ShowMessageDelegate(DataGridView dg, string message, int row, int column); private void ShowMessage(DataGridView dg, string message, int row, int column) { if (dg.InvokeRequired) { ShowMessageDelegate showMessageDelegate = ShowMessage; dg.Invoke(showMessageDelegate, new object[] { dg, message, row, column }); } else {
dg.Rows[row].Cells[column].Value = message; } } */

}}

C# 线程中操作窗体控件

然后我想在类中对空间进行操作,这样还可以么?

试试看

主窗体代码:








































































































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;
using System.Threading;
namespace WindowsFormsApp12{ public partial class Form1 : Form { public Form1() { InitializeComponent(); }
private void Form1_Load(object sender, EventArgs e) {
//CheckForIllegalCrossThreadCalls = false; Class1 cs1 = new Class1(); Thread th = new Thread(cs1.test); th.Start(); // cs1.ShowEvent = test;
}
public void test1() {
ShowlbDevTem("123"); //textBox1.Text = "123";
}

private void button1_Click(object sender, EventArgs e) {
// ShowlbDevTem("112");
}
/* public void test(string str) {
ShowlbDevTem(str); }*/ public delegate void SWTDelegate(string AddStr); //public delegate void ComsumerTextDelegate(int Index, string AddStr); public void ShowlbDevTem(string AddStr) { if (textBox1.InvokeRequired) { SWTDelegate pd = new SWTDelegate(ShowlbDevTem); textBox1.Invoke(pd, new object[] { AddStr }); } else { textBox1.Text = AddStr; //richTextBox1.AppendText(AddStr); } }
/* public void dataShow(int row, int column, string str) { row = row % 30; ShowMessage(dataGridViewX1, str, row, column);
}

delegate void ShowMessageDelegate(DataGridView dg, string message, int row, int column); private void ShowMessage(DataGridView dg, string message, int row, int column) { if (dg.InvokeRequired) { ShowMessageDelegate showMessageDelegate = ShowMessage; dg.Invoke(showMessageDelegate, new object[] { dg, message, row, column }); } else {
dg.Rows[row].Cells[column].Value = message; } } */

}}

class1中代码:



































using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;
using System.Threading;using System.Windows.Forms;
namespace WindowsFormsApp12{ class Class1 { //public Action ShowEvent; public void test() {
//ShowEvent("123456789");
Form1 f1 = new Form1(); f1.ShowlbDevTem("112");

}
}}

运行结果:

哎呦,卧槽

运行不报错 但是文本框中是空的,怎么办?

主窗体:








































































































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;
using System.Threading;
namespace WindowsFormsApp12{ public partial class Form1 : Form { public Form1() { InitializeComponent(); }
private void Form1_Load(object sender, EventArgs e) {
//CheckForIllegalCrossThreadCalls = false; Class1 cs1 = new Class1(); Thread th = new Thread(cs1.test); th.Start(); cs1.ShowEvent = test;
}
public void test1() {
ShowlbDevTem("123"); //textBox1.Text = "123";
}

private void button1_Click(object sender, EventArgs e) {
// ShowlbDevTem("112");
}
public void test(string str) {
ShowlbDevTem(str); } public delegate void SWTDelegate(string AddStr); //public delegate void ComsumerTextDelegate(int Index, string AddStr); public void ShowlbDevTem(string AddStr) { if (textBox1.InvokeRequired) { SWTDelegate pd = new SWTDelegate(ShowlbDevTem); textBox1.Invoke(pd, new object[] { AddStr }); } else { textBox1.Text = AddStr; //richTextBox1.AppendText(AddStr); } }
/* public void dataShow(int row, int column, string str) { row = row % 30; ShowMessage(dataGridViewX1, str, row, column);
}

delegate void ShowMessageDelegate(DataGridView dg, string message, int row, int column); private void ShowMessage(DataGridView dg, string message, int row, int column) { if (dg.InvokeRequired) { ShowMessageDelegate showMessageDelegate = ShowMessage; dg.Invoke(showMessageDelegate, new object[] { dg, message, row, column }); } else {
dg.Rows[row].Cells[column].Value = message; } } */

}}

类中:


































using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;
using System.Threading;using System.Windows.Forms;
namespace WindowsFormsApp12{ class Class1 { public Action<string> ShowEvent; public void test() {
ShowEvent("123456789");
// Form1 f1 = new Form1(); //f1.ShowlbDevTem("112");

}
}}

总算可以了 

但是我有两个类,线程在class1,委托在class2回调呢?

主窗体代码不变

class1代码:




































using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;
using System.Threading;using System.Windows.Forms;
namespace WindowsFormsApp12{ class Class1 { public Action<string> ShowEvent; public void test() {

// Class2 cs2 = new Class2(); // cs2.test(ShowEvent); Class2 cs2 = new Class2(); cs2.test(ShowEvent);
}



}}

class2代码





























using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;
using System.Threading;
namespace WindowsFormsApp12{ class Class2 { // public Action ShowEvent1; public void test(Action<string> ShowEvent) {

ShowEvent("123456789");
} }}

完!!!



推荐阅读
  • 本文分享了一个关于在C#中使用异步代码的问题,作者在控制台中运行时代码正常工作,但在Windows窗体中却无法正常工作。作者尝试搜索局域网上的主机,但在窗体中计数器没有减少。文章提供了相关的代码和解决思路。 ... [详细]
  • 后台获取视图对应的字符串
    1.帮助类后台获取视图对应的字符串publicclassViewHelper{将View输出为字符串(注:不会执行对应的ac ... [详细]
  • 本文介绍了lua语言中闭包的特性及其在模式匹配、日期处理、编译和模块化等方面的应用。lua中的闭包是严格遵循词法定界的第一类值,函数可以作为变量自由传递,也可以作为参数传递给其他函数。这些特性使得lua语言具有极大的灵活性,为程序开发带来了便利。 ... [详细]
  • 在Android开发中,使用Picasso库可以实现对网络图片的等比例缩放。本文介绍了使用Picasso库进行图片缩放的方法,并提供了具体的代码实现。通过获取图片的宽高,计算目标宽度和高度,并创建新图实现等比例缩放。 ... [详细]
  • Spring源码解密之默认标签的解析方式分析
    本文分析了Spring源码解密中默认标签的解析方式。通过对命名空间的判断,区分默认命名空间和自定义命名空间,并采用不同的解析方式。其中,bean标签的解析最为复杂和重要。 ... [详细]
  • HDU 2372 El Dorado(DP)的最长上升子序列长度求解方法
    本文介绍了解决HDU 2372 El Dorado问题的一种动态规划方法,通过循环k的方式求解最长上升子序列的长度。具体实现过程包括初始化dp数组、读取数列、计算最长上升子序列长度等步骤。 ... [详细]
  • 本文介绍了C#中数据集DataSet对象的使用及相关方法详解,包括DataSet对象的概述、与数据关系对象的互联、Rows集合和Columns集合的组成,以及DataSet对象常用的方法之一——Merge方法的使用。通过本文的阅读,读者可以了解到DataSet对象在C#中的重要性和使用方法。 ... [详细]
  • C# 7.0 新特性:基于Tuple的“多”返回值方法
    本文介绍了C# 7.0中基于Tuple的“多”返回值方法的使用。通过对C# 6.0及更早版本的做法进行回顾,提出了问题:如何使一个方法可返回多个返回值。然后详细介绍了C# 7.0中使用Tuple的写法,并给出了示例代码。最后,总结了该新特性的优点。 ... [详细]
  • 《数据结构》学习笔记3——串匹配算法性能评估
    本文主要讨论串匹配算法的性能评估,包括模式匹配、字符种类数量、算法复杂度等内容。通过借助C++中的头文件和库,可以实现对串的匹配操作。其中蛮力算法的复杂度为O(m*n),通过随机取出长度为m的子串作为模式P,在文本T中进行匹配,统计平均复杂度。对于成功和失败的匹配分别进行测试,分析其平均复杂度。详情请参考相关学习资源。 ... [详细]
  • 本文详细介绍了Java中vector的使用方法和相关知识,包括vector类的功能、构造方法和使用注意事项。通过使用vector类,可以方便地实现动态数组的功能,并且可以随意插入不同类型的对象,进行查找、插入和删除操作。这篇文章对于需要频繁进行查找、插入和删除操作的情况下,使用vector类是一个很好的选择。 ... [详细]
  • ASP.NET2.0数据教程之十四:使用FormView的模板
    本文介绍了在ASP.NET 2.0中使用FormView控件来实现自定义的显示外观,与GridView和DetailsView不同,FormView使用模板来呈现,可以实现不规则的外观呈现。同时还介绍了TemplateField的用法和FormView与DetailsView的区别。 ... [详细]
  • 开发笔记:加密&json&StringIO模块&BytesIO模块
    篇首语:本文由编程笔记#小编为大家整理,主要介绍了加密&json&StringIO模块&BytesIO模块相关的知识,希望对你有一定的参考价值。一、加密加密 ... [详细]
  • 阿,里,云,物,联网,net,core,客户端,czgl,aliiotclient, ... [详细]
  • 本文介绍了OC学习笔记中的@property和@synthesize,包括属性的定义和合成的使用方法。通过示例代码详细讲解了@property和@synthesize的作用和用法。 ... [详细]
  • 1,关于死锁的理解死锁,我们可以简单的理解为是两个线程同时使用同一资源,两个线程又得不到相应的资源而造成永无相互等待的情况。 2,模拟死锁背景介绍:我们创建一个朋友 ... [详细]
author-avatar
黑马@梦想
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有