委托和类,值一样是C#的数据类型...
使用委托要和使用其它类型一样定义现定义类型本身
就向定义类要用class关键字一样,委托..使用delegate关键字.
委托的用处就是可以把方法当成变量使用..
这段代码多处使用了委托...LZ请看
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Microsoft.Build.Tasks.Deployment.ManifestUtilities;
using System.Threading;namespace WindowsApplication1
{
public partial class Form1 : Form
{ Thread thread1;
Thread thread2;
Thread thread3;
Thread thread4; delegate void VOID();
Form2 form2 = new Form2(); bool end1 = false;
bool end2 = false;
bool end3 = false;
bool end4 = false; public Form1()
{
InitializeComponent();
thread1 = new Thread(new ThreadStart(ThreadRun4));
thread2 = new Thread(new ThreadStart(ThreadRun1));
thread3 = new Thread(new ThreadStart(ThreadRun2));
thread4 = new Thread(new ThreadStart(ThreadRun3)); } void ThreadEnd()
{
if (end1 && end2 && end3 && end4)
{
form2.Close();
}
} void ShowForm2()
{
if (form2.Visible != true)
{
form2.Show(this);
}
}
float plan1 = 0;
float plan2= 0;
float plan3 = 0;
float plan4 = 0;
void SetPlan()
{
float temp = (plan1 + plan2 + plan3 + plan4) * 10000 * 0.25f; form2.progressBar1.Value = (int)Math.Round(temp);
form2.progressBar2.Value = (int)Math.Round(plan1 * 10000);
form2.progressBar3.Value = (int)Math.Round(plan2 * 10000);
form2.progressBar4.Value = (int)Math.Round(plan3 * 10000);
form2.progressBar5.Value = (int)Math.Round(plan4 * 10000);
} private void button1_Click(object sender, EventArgs e)
{
thread1.Start();
thread2.Start();
thread3.Start();
thread4.Start();
} Random random = new Random(); void ThreadRun4()
{
int maxCount;
int sleep;
lock (random)
{
maxCount = random.Next(10, 100);
sleep = random.Next(100, 500);
}
this.Invoke(new VOID(ShowForm2));
for (int a = 0; a <= maxCount;a++ )
{
Thread.Sleep(sleep);
plan4 = (float)a / maxCount;
this.Invoke(new VOID(SetPlan));
}
end4 = true;
this.Invoke(new VOID(ThreadEnd));
} void ThreadRun1()
{ int maxCount;
int sleep;
lock (random)
{
maxCount = random.Next(10, 100);
sleep = random.Next(100, 500);
}
this.Invoke(new VOID(ShowForm2));
for (int a = 0; a <= maxCount; a++)
{
Thread.Sleep(sleep);
plan1 = (float)a / maxCount;
this.Invoke(new VOID(SetPlan));
}
end1 = true;
this.Invoke(new VOID(ThreadEnd));
}
void ThreadRun2()
{ int maxCount;
int sleep;
lock (random)
{
maxCount = random.Next(10, 100);
sleep = random.Next(100, 500);
}
this.Invoke(new VOID(ShowForm2));
for (int a = 0; a <= maxCount; a++)
{
Thread.Sleep(sleep);
plan2 = (float)a / maxCount;
this.Invoke(new VOID(SetPlan));
}
end2 = true;
this.Invoke(new VOID(ThreadEnd));
}
void ThreadRun3()
{
int maxCount;
int sleep;
lock (random)
{
maxCount = random.Next(10, 100);
sleep = random.Next(100, 500);
}
this.Invoke(new VOID(ShowForm2));
for (int a = 0; a <= maxCount; a++)
{
Thread.Sleep(sleep);
plan3 = (float)a / maxCount;
this.Invoke(new VOID(SetPlan));
}
end3 = true;
this.Invoke(new VOID(ThreadEnd));
}
}
}
使用委托要和使用其它类型一样定义现定义类型本身
就向定义类要用class关键字一样,委托..使用delegate关键字.
委托的用处就是可以把方法当成变量使用..
这段代码多处使用了委托...LZ请看
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Microsoft.Build.Tasks.Deployment.ManifestUtilities;
using System.Threading;namespace WindowsApplication1
{
public partial class Form1 : Form
{ Thread thread1;
Thread thread2;
Thread thread3;
Thread thread4; delegate void VOID();
Form2 form2 = new Form2(); bool end1 = false;
bool end2 = false;
bool end3 = false;
bool end4 = false; public Form1()
{
InitializeComponent();
thread1 = new Thread(new ThreadStart(ThreadRun4));
thread2 = new Thread(new ThreadStart(ThreadRun1));
thread3 = new Thread(new ThreadStart(ThreadRun2));
thread4 = new Thread(new ThreadStart(ThreadRun3)); } void ThreadEnd()
{
if (end1 && end2 && end3 && end4)
{
form2.Close();
}
} void ShowForm2()
{
if (form2.Visible != true)
{
form2.Show(this);
}
}
float plan1 = 0;
float plan2= 0;
float plan3 = 0;
float plan4 = 0;
void SetPlan()
{
float temp = (plan1 + plan2 + plan3 + plan4) * 10000 * 0.25f; form2.progressBar1.Value = (int)Math.Round(temp);
form2.progressBar2.Value = (int)Math.Round(plan1 * 10000);
form2.progressBar3.Value = (int)Math.Round(plan2 * 10000);
form2.progressBar4.Value = (int)Math.Round(plan3 * 10000);
form2.progressBar5.Value = (int)Math.Round(plan4 * 10000);
} private void button1_Click(object sender, EventArgs e)
{
thread1.Start();
thread2.Start();
thread3.Start();
thread4.Start();
} Random random = new Random(); void ThreadRun4()
{
int maxCount;
int sleep;
lock (random)
{
maxCount = random.Next(10, 100);
sleep = random.Next(100, 500);
}
this.Invoke(new VOID(ShowForm2));
for (int a = 0; a <= maxCount;a++ )
{
Thread.Sleep(sleep);
plan4 = (float)a / maxCount;
this.Invoke(new VOID(SetPlan));
}
end4 = true;
this.Invoke(new VOID(ThreadEnd));
} void ThreadRun1()
{ int maxCount;
int sleep;
lock (random)
{
maxCount = random.Next(10, 100);
sleep = random.Next(100, 500);
}
this.Invoke(new VOID(ShowForm2));
for (int a = 0; a <= maxCount; a++)
{
Thread.Sleep(sleep);
plan1 = (float)a / maxCount;
this.Invoke(new VOID(SetPlan));
}
end1 = true;
this.Invoke(new VOID(ThreadEnd));
}
void ThreadRun2()
{ int maxCount;
int sleep;
lock (random)
{
maxCount = random.Next(10, 100);
sleep = random.Next(100, 500);
}
this.Invoke(new VOID(ShowForm2));
for (int a = 0; a <= maxCount; a++)
{
Thread.Sleep(sleep);
plan2 = (float)a / maxCount;
this.Invoke(new VOID(SetPlan));
}
end2 = true;
this.Invoke(new VOID(ThreadEnd));
}
void ThreadRun3()
{
int maxCount;
int sleep;
lock (random)
{
maxCount = random.Next(10, 100);
sleep = random.Next(100, 500);
}
this.Invoke(new VOID(ShowForm2));
for (int a = 0; a <= maxCount; a++)
{
Thread.Sleep(sleep);
plan3 = (float)a / maxCount;
this.Invoke(new VOID(SetPlan));
}
end3 = true;
this.Invoke(new VOID(ThreadEnd));
}
}
}
解决方案 »
- net IDE环境里,除了Visual Assist,还有什么好用的工具吗?
- C的指针指向数组的问题
- 求一句SQL语句,合并多条记录,共用第一个字段,合并第2个字段,在线等~~~~
- 急急急!为什么我数据库里的信息不能再DATAGRIDVIEW显示
- 怎样用 ListView 显示数据库里的数据
- 我用DataGrid的修改按钮功能,可是无法取到修改后的文本值,有人知道怎么回事吗?
- comboBox数据绑定显示问题~在线等~!
- 怎样混淆string值????混淆器只能改变量方法名等
- 新手问题--关于自定义进度条控件
- 初学c#,有个简单的问题搞不定,各位帮帮忙!
- ASP.NET 平台
- C#代码怎样连接到Crystal Reports 9
委托可以说是“.NET版的函数指针”,实际上,委托较之函数指针有了很多改进,其中最实用也是最酷的一点就是:委托是“多播”的,而函数指针是“单播”的。换句话说:委托是“一对多的”,即一个委托上可以挂接好几个与之签名相同的函数;而函数指针只能“一对一”,即一个函数指针只能指向一个函数
int i = 10;
string message = "hello,world!";
class A {
public void test(string text) {
Console.WriteLine(text);
}
}void ff(A a) {
a.test("中国人");
}
用委托并不很难,在什么情况下用,怎样用才是最难的,
委托用的地方很多,我所知的
1事件上,这个不多说
2多线程上,在2003上thread是不安全的线程,在2005引入了委托机制弥补线程不安全的缺点
使windows的消息得以同步
3自动业务流程上,运用多播委托可以实现,