热门标签 | 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");
} }}

完!!!



推荐阅读
  • 本文由编程笔记#小编为大家整理,主要介绍了logistic回归(线性和非线性)相关的知识,包括线性logistic回归的代码和数据集的分布情况。希望对你有一定的参考价值。 ... [详细]
  • 本文分享了一个关于在C#中使用异步代码的问题,作者在控制台中运行时代码正常工作,但在Windows窗体中却无法正常工作。作者尝试搜索局域网上的主机,但在窗体中计数器没有减少。文章提供了相关的代码和解决思路。 ... [详细]
  • android listview OnItemClickListener失效原因
    最近在做listview时发现OnItemClickListener失效的问题,经过查找发现是因为button的原因。不仅listitem中存在button会影响OnItemClickListener事件的失效,还会导致单击后listview每个item的背景改变,使得item中的所有有关焦点的事件都失效。本文给出了一个范例来说明这种情况,并提供了解决方法。 ... [详细]
  • C# 7.0 新特性:基于Tuple的“多”返回值方法
    本文介绍了C# 7.0中基于Tuple的“多”返回值方法的使用。通过对C# 6.0及更早版本的做法进行回顾,提出了问题:如何使一个方法可返回多个返回值。然后详细介绍了C# 7.0中使用Tuple的写法,并给出了示例代码。最后,总结了该新特性的优点。 ... [详细]
  • 拥抱Android Design Support Library新变化(导航视图、悬浮ActionBar)
    转载请注明明桑AndroidAndroid5.0Loollipop作为Android最重要的版本之一,为我们带来了全新的界面风格和设计语言。看起来很受欢迎࿰ ... [详细]
  • ASP.NET2.0数据教程之十四:使用FormView的模板
    本文介绍了在ASP.NET 2.0中使用FormView控件来实现自定义的显示外观,与GridView和DetailsView不同,FormView使用模板来呈现,可以实现不规则的外观呈现。同时还介绍了TemplateField的用法和FormView与DetailsView的区别。 ... [详细]
  • 在Xamarin XAML语言中如何在页面级别构建ControlTemplate控件模板
    本文介绍了在Xamarin XAML语言中如何在页面级别构建ControlTemplate控件模板的方法和步骤,包括将ResourceDictionary添加到页面中以及在ResourceDictionary中实现模板的构建。通过本文的阅读,读者可以了解到在Xamarin XAML语言中构建控件模板的具体操作步骤和语法形式。 ... [详细]
  • 在springmvc框架中,前台ajax调用方法,对图片批量下载,如何弹出提示保存位置选框?Controller方法 ... [详细]
  • iOS Swift中如何实现自动登录?
    本文介绍了在iOS Swift中如何实现自动登录的方法,包括使用故事板、SWRevealViewController等技术,以及解决用户注销后重新登录自动跳转到主页的问题。 ... [详细]
  • Linux重启网络命令实例及关机和重启示例教程
    本文介绍了Linux系统中重启网络命令的实例,以及使用不同方式关机和重启系统的示例教程。包括使用图形界面和控制台访问系统的方法,以及使用shutdown命令进行系统关机和重启的句法和用法。 ... [详细]
  • 本文介绍了C#中数据集DataSet对象的使用及相关方法详解,包括DataSet对象的概述、与数据关系对象的互联、Rows集合和Columns集合的组成,以及DataSet对象常用的方法之一——Merge方法的使用。通过本文的阅读,读者可以了解到DataSet对象在C#中的重要性和使用方法。 ... [详细]
  • 也就是|小窗_卷积的特征提取与参数计算
    篇首语:本文由编程笔记#小编为大家整理,主要介绍了卷积的特征提取与参数计算相关的知识,希望对你有一定的参考价值。Dense和Conv2D根本区别在于,Den ... [详细]
  • C# WPF自定义按钮的方法
    本文介绍了在C# WPF中实现自定义按钮的方法,包括使用图片作为按钮背景、自定义鼠标进入效果、自定义按压效果和自定义禁用效果。通过创建CustomButton.cs类和ButtonStyles.xaml资源文件,设计按钮的Style并添加所需的依赖属性,可以实现自定义按钮的效果。示例代码在ButtonStyles.xaml中给出。 ... [详细]
  • 第四章高阶函数(参数传递、高阶函数、lambda表达式)(python进阶)的讲解和应用
    本文主要讲解了第四章高阶函数(参数传递、高阶函数、lambda表达式)的相关知识,包括函数参数传递机制和赋值机制、引用传递的概念和应用、默认参数的定义和使用等内容。同时介绍了高阶函数和lambda表达式的概念,并给出了一些实例代码进行演示。对于想要进一步提升python编程能力的读者来说,本文将是一个不错的学习资料。 ... [详细]
  • This article discusses the efficiency of using char str[] and char *str and whether there is any reason to prefer one over the other. It explains the difference between the two and provides an example to illustrate their usage. ... [详细]
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社区 版权所有