C# 关于 跨线程资源访问

lxtx1 2009-04-25 06:56:00
问题描述如下:
有一个静态类:
其中含有一个新建线程的静态方法
有一个窗体类:
其中含控件若干
现在窗体类中调用静态类的新建线程方法,问静态类中创建的线程入口函数中如何修改该窗体的控件的属性
环境VS2005

最好能给个小DEMO , 在线等待
...全文
276 13 打赏 收藏 转发到动态 举报
写回复
用AI写文章
13 条回复
切换为时间正序
请发表友善的回复…
发表回复
lxtx1 2009-04-25
  • 打赏
  • 举报
回复
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

using System.Threading;

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

private void Form1_Load(object sender, EventArgs e)
{
//某个事件中开启一个线程
Thread thread = new Thread(new ThreadStart(this.StartInit));
thread.Start();
}

//代理方法模板
public delegate void Procedure();

//线程代理方法, this是指窗体
public virtual void StartInit()
{
this.Invoke(
new Procedure(
delegate(){
this.Update();//这就是跨线程操作
this.DialogResult = DialogResult.OK;//比如...
this.Text = "信心很重要";//比如改变标题
}
)
);
//this.Invoke(new Procedure(this.Close));//比比调用关闭窗体的方法
}
}
}

using System;
using System.Collections.Generic;
using System.Text;

using System.Threading;

namespace exp
{
class Class1
{
public static Thread myThread;

public static void start()
{
myThread=new Thread(new ThreadStart(setText));
myThread.Start();
}



private void setText()
{
//////////////如何访问form1中的控件
}
}
}
Sysping1 2009-04-25
  • 打赏
  • 举报
回复
[Quote=引用 8 楼 lxtx1 的回复:]
谢谢,//线程代理方法, this是指窗体
public virtual void StartInit(){
this.Invoke(
new Procedure( 有点看不懂啊.................
//////////////////////Procedure代理不是没有参数的吗???
delegate(){
this.Update();//这就是跨线程操作
this.DialogResult …
[/Quote]

new Procedure(), 内在参数是什么一个实际的方法,你下VS写代码会提示的new Procedure(void() target),
就是说它的参数就是一个函数指针类似的东东。
代理和一般类有点特别,建议这方面先了解下!
请把那段代码写到VS测试,没问题,因为是我项目的源码的一段,我们一向是这样用的!
wuyq11 2009-04-25
  • 打赏
  • 举报
回复
利用线程和委托访问窗体控件
public delegate void minvole(string str);
private void button3_Click(object sender, EventArgs e)
{
Thread t = new Thread(new ThreadStart(GetTextFun));
t.Start();
}private void GetTextFun()
{
setText("");
}
private void setText(string str)
{
if (textBox1.InvokeRequired)
{
minvole _minvole = new minvole(setText);
this.Invoke(_myinvole, new object[] { str });
}
else
{
this.textBox1.Text = str;
}
}
参考
lxtx1 2009-04-25
  • 打赏
  • 举报
回复
[Quote=引用 9 楼 SuperMan_ 的回复:]
要用到委托:
希望对楼主有所启发
delegate void SetTextCallback(string text);
private void SetText(string text)
{
if (this.inforMg.InvokeRequired)
{
SetTextCallback d = new SetTextCallback(SetText);
this.Invoke(d, new object[] { text });
}
else
{
this.inforMg.T…
[/Quote]

不错,这段
但是如何在静态类中的线程方法中去访问另一个窗体类实例的控件呢?
快40的码农 2009-04-25
  • 打赏
  • 举报
回复
要用到委托:
希望对楼主有所启发
delegate void SetTextCallback(string text);
private void SetText(string text)
{
if (this.inforMg.InvokeRequired)
{
SetTextCallback d = new SetTextCallback(SetText);
this.Invoke(d, new object[] { text });
}
else
{
this.inforMg.Text = text;
}
}
lxtx1 2009-04-25
  • 打赏
  • 举报
回复
谢谢,//线程代理方法, this是指窗体
public virtual void StartInit(){
this.Invoke(
new Procedure( 有点看不懂啊.................
//////////////////////Procedure代理不是没有参数的吗???

delegate(){
this.Update();//这就是跨线程操作
this.DialogResult = DialogResult.OK;//比如...
this.Text = "信心很重要";//比如改变标题
}
)
);
this.Invoke(new Procedure(this.Close));//比比调用关闭窗体的方法
}
能不能给我写个不在同一个类里的例子
Sysping1 2009-04-25
  • 打赏
  • 举报
回复
1: 以下代码都在本窗体内

//代理方法模板
public delegate void Procedure();

//某个事件中开启一个线程
Thread thread = new Thread(new ThreadStart(this.StartInit));
thread.Start();

//线程代理方法, this是指窗体
public virtual void StartInit(){
this.Invoke(
new Procedure(
delegate(){
this.Update();//这就是跨线程操作
this.DialogResult = DialogResult.OK;//比如...
this.Text = "信心很重要";//比如改变标题
}
)
);
this.Invoke(new Procedure(this.Close));//比比调用关闭窗体的方法
}

2:希望对你有帮助
lxtx1 2009-04-25
  • 打赏
  • 举报
回复
问题描述如下:
有一个静态类:
其中含有一个新建线程的静态方法
有一个窗体类:
其中含控件若干
现在窗体类中调用静态类的新建线程方法,问静态类中创建的线程入口函数中如何修改该窗体的控件的属性 (我在线程中如何去访问那个窗体呢,不在同一个类中)
环境VS2005
lxtx1 2009-04-25
  • 打赏
  • 举报
回复
能不能详细点啊
还有我在线程中如何去访问那个窗体呢?
lxtx1 2009-04-25
  • 打赏
  • 举报
回复
这样倒是可以
但问题是我在线程中如何去访问那个窗体呢?
jmnmu2 2009-04-25
  • 打赏
  • 举报
回复
用委托
jmnmu2 2009-04-25
  • 打赏
  • 举报
回复
CheckForIllegalCrossThreadCalls = false;//禁用跨线程访问控件异常
lxtx1 2009-04-25
  • 打赏
  • 举报
回复
怎么没人啊,自己顶下。。。。。

111,126

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Creator Browser
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧