1. 多线程加速计算
public
static
int
count = 0;
public
static
object
o =
new
object
();
private
static
void
Calculation()
{
lock
(o)
//加锁
{
for
(
int
j = 0; j < 10000000; j++)
{
count = count + 1;
}
}
}
调用:
List<IAsyncResult> listAction =
new
List<IAsyncResult>();
for
(
int
i = 0; i < 20; i++)
{
//创建线程 总共20个线程
listAction.Add(
new
Action(() =>
{
Calculation();
}).BeginInvoke(
null
,
null
));
}
//等所有线程都结束
while
(listAction.Count(r => !r.IsCompleted) > 0)
{
Thread.Sleep(100);
}
Console.WriteLine(
"加锁---计算结果为:{0}"
, count);
2. 关于四舍五入
Math.Round(4.5)
//结果为4
Math.Round(4.5, MidpointRounding.AwayFromZero)
//这样的结果才是 5
Math.Round(4.555, 2, MidpointRounding.AwayFromZero)
// 保留2位小数据 这样的结果才是 4.56
3.DataGridView只清空数据不清空表头
DataTable dt = (DataTable)dataGridView1.DataSource;
if
(dt !=
null
)
{
dt.Rows.Clear();
}
dataGridView1.DataSource = dt;
摘自:罗分明个人网络博客_asp.net,C#,js,数据库,web前端,学习笔记_以及工作问题解决方案 (luofenming.com)
标签:count,int,代码,笔记,static,搬运,dt,null,Math
From: https://www.cnblogs.com/Nikole/p/17816989.html