当前位置:首页 » 密码管理 » 线程访问控件

线程访问控件

发布时间: 2022-07-13 00:03:38

㈠ C#多线程如何调用主线程创建的控件

1.新开的线程是无法直接访问UI控件的,如果需要访问,可以通过控件的Invoke方法,或者用

System.Threading.SynchronizationContext.Current.Post方法

2.一定不要在控件的内部事件处理方法里面使用Sleep等线程暂停方法!

像我这样写就不会阻塞了:

private void button1_Click(object sender, EventArgs e)
{
System.Threading.Thread thread = new System.Threading.Thread(() =>
{
while (true)
{
this.Invoke(new Action(() =>
{
label1.Text = (count++).ToString();
}));
System.Threading.Thread.Sleep(100);
}
});
thread.IsBackground = true;
thread.Start();
}

你把setmess这个线程方法Invoke到了UI的主线程上了,而这个线程里面有暂停方法,所以呢主线程也会暂停卡住。
而我的方法只是把“label1.Text = (count++).ToString();”Invoke到主线程,而那个新的线程并没有Invoke到主线程,所以在那个线程里面使用暂停就没事了!

假如有一个操作比较费时,或者需要等待之类的,就像我这样开始一个新的线程,所以的等待操作,费时的操作都在在这个线程里面做,当这些线程需要访问UI控件的时候,就用this.Invoke()这个方法!

㈡ c# 跨线程访问控件

用委托,具体代码如下~:
public delegate void MyInvoke(string str);

private void button9_Click(object sender, EventArgs e)
{
//_myInvoke = new MyInvoke(SetText);
// = false;
Thread t = new Thread(new ThreadStart(fun));
t.Start();
}

private void fun()
{
//_myInvoke("dddd");
SetText("ddd");
}
private void SetText(string s)
{
if (textBox6.InvokeRequired)
{
MyInvoke _myInvoke = new MyInvoke(SetText);
this.Invoke(_myInvoke, new object[] { s });
}
else
{
this.textBox6.Text = s;
}
}

㈢ WPF跨线程遍历访问容器内的控件

this.Dispatcher.Invoke(new Action(() =>{
//TODO:更新UI控件操作
}));

㈣ Delphi多线程访问控件时,主线程可以顺利访问,副线程访问时就卡死 求解释

对于涉及VCL的线程,是必须在主线程中的,在DELPHI中使用线程,你只需要记住两句:
1、线程不安全,2、VCL相关要放到主线程。

㈤ c#子线程如何访问子窗体中的控件

控件声明成public
然后得到窗体的引用,直接可以访问。如果窗体不是在子线程中创建的,需要使用Invoke来访问。

㈥ C#如何在线程中访问控件对象

利用ListView.Invoke解决跨线程安全调用,关键代码如下:

voidFindFileInDir(){
if(listView2.InvokeRequired)
{
//跨线程调用
listView2.Invoke(newMethodInvoker(delegate
{
listView2.Items.Add(path);
}));
}
else
{
//直接调用
listView2.Items.Add(path);
}
}//EndofFindFileInDir

㈦ C#中多线程里面如何调用控件

定义委托,然后用invoke
private void ucMidPartsid_Load(object sender, EventArgs e)
{

if (is_Load == true)
{
thread = new Thread(new ThreadStart(this.LoadData));
thread.Start();
}
//if (datafinish != null)
//{
// datafinish(this, e);
//}
}
private void LoadData()
{
QueryDataCallBack deleg;
deleg = new QueryDataCallBack(this.DataBinding);
this.Invoke(deleg,null);

}
private void DataBinding()
{
partstableAdapter = new GreatWall.SCM.Parts.DataAccess.DAParts.PartsTableAdapter();
partstableAdapter.Fill(dsParts.GB_PARTS);
cobMidPartsid.DataSource = dsParts.GB_PARTS;
cobMidPartsid.DisplayMember = "B_PARTSID";
cobMidPartsid.ValueMember = "B_ID";
EventArgs e = new EventArgs();
if (datafinish != null)
{
finish = true;
datafinish(this, e);
}

}

㈧ 如何跨线程调用.NET Windows窗体控件

(1)窗体及控件
(2)窗体代码Form1.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
using System;
using System.Collections.Generic;
using System.Windows.Forms;
using System.Threading;

namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}

private void button1_Click(object sender, EventArgs e)
{
// 启动线程
Thread t = new Thread(MyThread);
t.IsBackground = true;
t.Start();
}

void MyThread()
{
while (true)
{
// 在控件label1上显示日期和时间
UpdateLabel(DateTime.Now);
Thread.Sleep(1000);
}
}
// 1. 声明一个委托
delegate void UpdateLableDelegate(DateTime dt);

private void UpdateLabel(DateTime dt)
{

if (label1.InvokeRequired)
{
// 跨线程更新label1处理
UpdateLableDelegate del =
new UpdateLableDelegate(UpdateLabel);
label1.Invoke(del, dt);
}
else
{
// 显示日期和时间
label1.Text = dt.ToString("yyyy-MM-dd HH:mm:ss");
}
}
}
}

㈨ C#怎么跨线程访问控件

只能用委托,不推荐 = false; 这种做法。


推荐一个封装好的 InvokeHelper 类(http://www.cnblogs.com/conmajia/archive/2012/08/05/multithread-gui-invoker.html),使用时只需要:

InvokeHelper.Invoke(<控件名>,"<方法名称>",<参数>);
InvokeHelper.Get(<控件名>,"<属性名称>");
InvokeHelper.Set(<控件名>,"<属性名称>",<属性值>);

即可。

㈩ C# 子线程访问主窗体控件

是的,
在其他线程里,不允许调用主线程创建的控件~!!!!
这样做,是不安全的,因此,2.0屏蔽了这个~

楼上说的很对,用委托,具体代码如下~:
public delegate void MyInvoke(string str);

private void button9_Click(object sender, EventArgs e)
{
//_myInvoke = new MyInvoke(SetText);
// = false;
Thread t = new Thread(new ThreadStart(fun));
t.Start();
}

private void fun()
{
//_myInvoke("dddd");
SetText("ddd");
}
private void SetText(string s)
{
if (textBox6.InvokeRequired)
{
MyInvoke _myInvoke = new MyInvoke(SetText);
this.Invoke(_myInvoke, new object[] { s });
}
else
{
this.textBox6.Text = s;
}
}

热点内容
车辆参数配置包括什么 发布:2025-05-14 21:31:03 浏览:162
怎么引入安卓项目 发布:2025-05-14 21:26:39 浏览:823
游戏辅编程 发布:2025-05-14 21:18:49 浏览:686
三菱plc一段二段密码什么意思 发布:2025-05-14 21:17:16 浏览:527
电脑开机密码忘记了怎么破解 发布:2025-05-14 21:09:40 浏览:56
pythondict格式 发布:2025-05-14 21:09:38 浏览:885
落叶片拍摄脚本 发布:2025-05-14 20:40:49 浏览:798
安卓为什么不能用cmwap 发布:2025-05-14 20:40:43 浏览:657
jquery获取上传文件 发布:2025-05-14 20:27:57 浏览:44
云web服务器搭建 发布:2025-05-14 20:25:36 浏览:526