DataRow 线程安全吗?如何使用多个线程更新数据表中的单个数据行? - .net 2.0
我想使用多个线程更新数据表中的单个数据行。这实际上可能吗?
我编写了以下代码,实现了一个简单的多线程来更新单个数据行。我每次都会得到不同的结果。为什么会这样呢?
public partial class Form1 : Form
{
private static DataTable dtMain;
private static string threadMsg = string.Empty;
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
Thread[] thArr = new Thread[5];
dtMain = new DataTable();
dtMain.Columns.Add("SNo");
DataRow dRow;
dRow = dtMain.NewRow();
dRow["SNo"] = 5;
dtMain.Rows.Add(dRow);
dtMain.AcceptChanges();
ThreadStart ts = new ThreadStart(delegate { dtUpdate(); });
thArr[0] = new Thread(ts);
thArr[1] = new Thread(ts);
thArr[2] = new Thread(ts);
thArr[3] = new Thread(ts);
thArr[4] = new Thread(ts);
thArr[0].Start();
thArr[1].Start();
thArr[2].Start();
thArr[3].Start();
thArr[4].Start();
while (!WaitTillAllThreadsStopped(thArr))
{
Thread.Sleep(500);
}
foreach (Thread thread in thArr)
{
if (thread != null && thread.IsAlive)
{
thread.Abort();
}
}
dgvMain.DataSource = dtMain;
}
private void dtUpdate()
{
for (int i = 0; i < 1000; i++)
{
try
{
dtMain.Rows[0][0] = Convert.ToInt32(dtMain.Rows[0][0]) + 1;
dtMain.AcceptChanges();
}
catch
{
continue;
}
}
}
private bool WaitTillAllThreadsStopped(Thread[] threads)
{
foreach (Thread thread in threads)
{
if (thread != null && thread.ThreadState == ThreadState.Running)
{
return false;
}
}
return true;
}
}
对此有什么想法吗?
谢谢
NLV
I want to update a single datarow in a datatable using multiple threads. Is this actually possible?
I've written the following code implementing a simple multi-threading to update a single datarow. I get different results each time. Why is it so?
public partial class Form1 : Form
{
private static DataTable dtMain;
private static string threadMsg = string.Empty;
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
Thread[] thArr = new Thread[5];
dtMain = new DataTable();
dtMain.Columns.Add("SNo");
DataRow dRow;
dRow = dtMain.NewRow();
dRow["SNo"] = 5;
dtMain.Rows.Add(dRow);
dtMain.AcceptChanges();
ThreadStart ts = new ThreadStart(delegate { dtUpdate(); });
thArr[0] = new Thread(ts);
thArr[1] = new Thread(ts);
thArr[2] = new Thread(ts);
thArr[3] = new Thread(ts);
thArr[4] = new Thread(ts);
thArr[0].Start();
thArr[1].Start();
thArr[2].Start();
thArr[3].Start();
thArr[4].Start();
while (!WaitTillAllThreadsStopped(thArr))
{
Thread.Sleep(500);
}
foreach (Thread thread in thArr)
{
if (thread != null && thread.IsAlive)
{
thread.Abort();
}
}
dgvMain.DataSource = dtMain;
}
private void dtUpdate()
{
for (int i = 0; i < 1000; i++)
{
try
{
dtMain.Rows[0][0] = Convert.ToInt32(dtMain.Rows[0][0]) + 1;
dtMain.AcceptChanges();
}
catch
{
continue;
}
}
}
private bool WaitTillAllThreadsStopped(Thread[] threads)
{
foreach (Thread thread in threads)
{
if (thread != null && thread.ThreadState == ThreadState.Running)
{
return false;
}
}
return true;
}
}
Any thoughts on this?
Thank you
NLV
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
根据MSDN:
因此,由于您正在更新
DataRow
和DataTable
对象,因此您将需要使用某种形式的同步来保证您的代码是线程安全的。顺便说一句,您也不应该调用 Thread.Sleep 或在 UI 线程中执行繁忙循环。否则,UI 将完全无响应,直到所有线程都完成执行。相反,您应该在 UI 上显示某种进度条(或只是一个微调器),该进度条可以选择通过线程中的事件进行更新。 BackgroundWorker 类专门设计用于完成此工作对您来说更容易,因此您可能需要考虑使用它。
According to MSDN:
So since you are updating
DataRow
andDataTable
objects you will need to use some form of synchronization to guarantee your code is thread safe.By the way, you should also never call
Thread.Sleep
or do a busy loop in your UI thread. Otherwise the UI will be completely unresponsive until all threads have finished executing. Instead you should have some kind of progress bar (or just a spinner) displayed on your UI, which is optionally updated by events from your threads. The BackgroundWorker class is specifically designed to make this job easier for you, so you may want to consider using it.除非类型或成员的文档另有规定:
Unless the documentation for the type or member specifies otherwise: