Invoke实现跨线程访问控件

不带参数的跨线程


private
void button1_Click(object sender, EventArgs e)

{


Thread t = new
Thread(() =>

{


Random R = new
Random();


this.Invoke(new
Action(()=> { this.Text = R.NextDouble().ToString(); }));

});

t.Start();

}

注意:Invoke应用在其他线程内部;

带参数的跨线程


private
void button1_Click(object sender, EventArgs e)

{


Action<object> a = new
Action<object>(printf);


this.Invoke(a,“Caption”);

}


public
void printf(object s)

{


this.Text = s.ToString();

}

画面之间采用委托传递参数

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;

 

namespace WindowsFormsApplication2

{


public
delegate
void
delegateShow(int counter);


public
partial
class
Form1 : Form

{


public Form1()

{

InitializeComponent();

 


Form2 f2 = new
Form2();

f2.ShowCounter = new
delegateShow(showcounter);

f2.Show();

}

 


public
void showcounter(int counter)

{


this.Text = counter.ToString();

}

}

}

 

 

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;

 

namespace WindowsFormsApplication2

{


public
partial
class
Form2 : Form

{


public Form2()

{

InitializeComponent();

}

 


public
delegateShow ShowCounter;


int counter=0;

 


private
void button1_Click(object sender, EventArgs e)

{

counter++;

ShowCounter?.Invoke(counter);

}

}

}

 

 

XML文件读写

xml文件内容

<?xml version=”1.0″ encoding=”utf-8″ standalone=”yes”?>

<library>

<book>

<name>笑傲江湖</name>

<name1>罪与罚</name1>

</book>

</library>

文件的读取


private
void button1_Click(object sender, EventArgs e)

{


OpenFileDialog op = new
OpenFileDialog();

op.Filter = “xml文件|*.xml”;

op.ShowDialog();

 


XDocument document = XDocument.Load( op.FileName );


XElement root = document.Root;


XElement book = root.Element(“book”);


XElement name1 = book.Element(“name”);


XElement name2 = book.Element(“name1”);


MessageBox.Show(name1.Value);


MessageBox.Show(name2.Value);

}

文件的写入


private
void button2_Click(object sender, EventArgs e)

{


OpenFileDialog op = new
OpenFileDialog();

op.Filter = “xml文件|*.xml”;

op.ShowDialog();

 


XDocument document = XDocument.Load(op.FileName);


XElement root = document.Root;


XElement book = root.Element(“book”);


XElement name1 = book.Element(“name”);


XElement name2 = book.Element(“name1”);

name1.Value = 笑傲江湖;

name2.Value = 罪与罚;

document.Save(op.FileName);

}

 

 

委托

你有没有想过像调用变量一样调用函数,把函数当做变量一样传递。

定义委托

delegate
int
delagateF(int a, int b);

仔细看一下上面的定义,会发现这很像一个函数。

下面演示一下委托的简单的应用。

using System;

 

namespace ConsoleApplication3

{


class
Program

{


delegate
int
delegateF(int a, int b);


static
void Main(string[] args)

{


delegateF f = new
delegateF(Add);


int sum = f(1,2);


Console.WriteLine(sum);

}

 


static
int Add(int a,int b)

{


return a + b;

}

}

}

下面的例子演示函数作为变量传递

using System;

 

namespace ConsoleApplication3

{


class
Program

{


delegate
int
delegateF(int a, int b);


static
void Main(string[] args)

{


delegateF f = new
delegateF(Add);

Wsum(f,1,2);

Wsum(Add,1,2);

}

 


static
int Add(int a,int b)

{


return a + b;

}

 


static
void Wsum(delegateF f,int a,int b)

{


int sum = f(a,b);


Console.WriteLine(sum);

}

}

}

2021年11月5日