C#编程应用--线程与委托
2011-09-28 23:52
351 查看
1. C#开发C/S程序,有时需要几个端,如服务器端,管理端,客户端等等, 端与端之间是不同线程或者进程,这就涉及跨线程调用的问题,
使用委托或者异步线程是必不可少的,这里是一个简单的委托线程,即通过委托调用另外一个线程;
2. 有图有真相:
View Code 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 ThreadControlExample
{
public partial class Form1 : Form
{
Thread thread1;
Thread thread2;
delegate void AppendStringDelegate(string str);
AppendStringDelegate appendStringDelegate;
public Form1()
{
InitializeComponent();
appendStringDelegate = new AppendStringDelegate(AppendString);
}
private void AppendString(string str)
{
richTextBox1.Text += str;
}
private void Method1()
{
while (true)
{
Thread.Sleep(100); //线程1休眠100毫秒
richTextBox1.Invoke(appendStringDelegate, "a");
}
}
private void Method2()
{
while (true)
{
Thread.Sleep(100); //线程2休眠100毫秒
richTextBox1.Invoke(appendStringDelegate, "b");
}
}
private void buttonStart_Click(object sender, EventArgs e)
{
richTextBox1.Text = "";
thread1 = new Thread(new ThreadStart(Method1));
thread2 = new Thread(new ThreadStart(Method2));
thread1.Start();
thread2.Start();
}
private void buttonStop_Click(object sender, EventArgs e)
{
thread1.Abort();
thread1.Join();
thread2.Abort();
thread2.Join();
MessageBox.Show("线程1、2终止成功");
}
}
}
使用委托或者异步线程是必不可少的,这里是一个简单的委托线程,即通过委托调用另外一个线程;
2. 有图有真相:
View Code 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 ThreadControlExample
{
public partial class Form1 : Form
{
Thread thread1;
Thread thread2;
delegate void AppendStringDelegate(string str);
AppendStringDelegate appendStringDelegate;
public Form1()
{
InitializeComponent();
appendStringDelegate = new AppendStringDelegate(AppendString);
}
private void AppendString(string str)
{
richTextBox1.Text += str;
}
private void Method1()
{
while (true)
{
Thread.Sleep(100); //线程1休眠100毫秒
richTextBox1.Invoke(appendStringDelegate, "a");
}
}
private void Method2()
{
while (true)
{
Thread.Sleep(100); //线程2休眠100毫秒
richTextBox1.Invoke(appendStringDelegate, "b");
}
}
private void buttonStart_Click(object sender, EventArgs e)
{
richTextBox1.Text = "";
thread1 = new Thread(new ThreadStart(Method1));
thread2 = new Thread(new ThreadStart(Method2));
thread1.Start();
thread2.Start();
}
private void buttonStop_Click(object sender, EventArgs e)
{
thread1.Abort();
thread1.Join();
thread2.Abort();
thread2.Join();
MessageBox.Show("线程1、2终止成功");
}
}
}
相关文章推荐
- C# 委托高级应用----线程——创建无阻塞的异步调用(二)
- C# 委托在线程与UI界面之间的应用
- C# 多线程编程 - 使用委托创建线程
- C# 委托高级应用----线程——创建无阻塞的异步调用(一)
- [C#网络应用编程]2、对线程的管理
- C#编程应用--简单线程
- C#实践——委托实例应用
- C#之入门总结_线程,委托,事件的关系_20
- 深入解析C#编程中泛型委托的使用
- Linux 系统应用编程——线程基础
- 关于C#/.NET中委托和事件的机制和应用的故事(转)
- Action<T> 泛型委托 在跨线程访问控件委托中的应用
- 三种实例化委托的方式(C# 编程指南)
- 深入理解C#编程中的组件-事件-委托
- C# :线程中操作多控件 (委托实现异步及同步的方法)
- Mark:使用线程处理(C# 编程指南)
- C# 委托应用总结
- (转)C#线程系列讲座(2):Thread类的应用
- iOS之多线程编程:三个层次线程应用
- C# 用委托BeginInvoke做异步线程