如何使用多线程显示数据表数据? [英] How to display datatable data using multithreading?

查看:84
本文介绍了如何使用多线程显示数据表数据?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是C#中的多线程初学者。混淆当前的任务。



我的任务是使用多线程读取数据表并在C#控制台应用程序中显示数据表。



我有三个线程和一个有50000 Rors的数据表。

所有线程必须同时开始。

我想:

第1行打印第1行。

第2行打印第2行。

第3行打印第3行。

再次

第1个线程打印第4行。

第2个线程打印第5行。

第3个线程打印第6行。继续到最后一行。



如何实现这个???

有没有其他技术可以做到这一点????



我尝试了什么:



我也试过锁定但它没有对我来说很有用。

I am beginner in Multi-threading in C# & confuse in current task.

My task is to read datatable and display datatable in C# console application using multi threading.

I have three threads and one datatable which have 50000 Rors.
All thread must start same time.
I want :
1st thread print 1st row.
2nd thread print 2nd row.
3rd thread print 3rd row.
again
1st thread print 4st row.
2nd thread print 5nd row.
3rd thread print 6rd row. Continue till last row.

How to implement this???
Is there any other technique to do this????

What I have tried:

I have also tried locking but it didn't work for me.

推荐答案

如果线程只是从DataTable中读取,则不需要锁定或任何其他类型的线程同步。只需将DataTable传递给每个线程以及一个值,该值指示线程应该开始将每第三行写入控制台的哪一行。启动所有线程后加入它们并完成任务。



编辑:我的解决方案忽略了您希望按顺序显示行。正如Dave所说,根据该要求,任务不适合多线程,因为那时线程必须同步,并且使用单个线程没有任何好处。因此,如果通过多线程应该有某种好处,那么就必须放弃这个要求。 (但是由于控制台的显示速度不是那么高,即使线程不同步,通过多线程是否有任何好处也是值得怀疑的。)
If the threads are only reading from the DataTable you don't need locking or any other sort of thread synchronization. Just pass the DataTable to each of your threads along with a value that indicates at which row a thread should start writing every third row to the console. After starting all threads join them and your task is done.

My solution ignored that you want the rows to be displayed in order. As Dave said, with that requirement the task doesn't lend itself to multi-threading because then then threads would have to be synchronized and there would be no benefit over using a single thread. So if there should be some sort of benefit through multi-threading then this requirement would have to be dropped. (But as the display-speed of the console isn't that high it is questionable if there is any benefit through multi-threading even if the threads aren't synchronized.)


这个问题无法通过线程解决。不使用锁定,您无法保证线程运行的顺序。您可以获得1,2,3,1,3,2,3,1,2,1等线程订单...



引入锁定获取正确的顺序只是使这些线程同步执行,与单线程操作没有什么不同。



问题不适合通过线程解决。





现在,如果你想向用户显示50,000条记录,你就不应该这样做。要么将记录过滤到可管理的东西,要么实现某种分页机制,在那里你会显示较大的一组记录。



我不能告诉你做什么或怎么做因为你没有说过你要解决的真正问题。
This problem cannot be solved by threading. Without using locking, you cannot guarantee the order in which threads run. You CAN get a thread order like 1, 2, 3, 1, 3, 2, 3, 1, 2, 1, ...

Introducing locking to get the correct order just makes these threads execute synchronously, no different than a single-threaded operation.

The problem does not lend itself to being solved by threading.


Now, if you're trying to show a user 50,000 records, you shouldn't be doing that. Either filter the records down to something manageable or implement some kind of paging mechanism where you show a small block of the larger set of records.

I can't tell you what to do or how to do it because you haven't said anything about the real problem you're trying to solve.


如果要按顺序打印行,那么你将会可能需要使用等待状态。基本上它是导致线程暂停直到某些东西触发指示线程继续的状态的东西。因此,线程1只会读取第1,4,7行等。当线程1启动时会遇到等待状态,导致它等待,它的逻辑就像是



1开始

2等待

3读下一行(1,4,7等)

4如果有更多行留给我重置我的等待状态并循环回到2



线程2将几乎相同



1 start

2等待

3读下一行(2,5,8等)

4如果还剩下更多行重置我的等待状态并循环返回到2



线程3,你现在可以猜到。



这个的诀窍是触发等待状态启动过程将触发1,线程1将触发两个,两个将触发三个,三个将触发一个。这样他们都会按顺序进行。



如果你*不需要按顺序打印它们那么这很容易,这是上面的代码而没有全部等待状态的东西。谷歌c#manualresentevent来了解你如何编码这些东西。

resent event
If the rows reed to be printed in order then you'll probably need to use wait states. Basically it is something that causes a thread to pause until something triggers the state instructing the thread to continue. So thread 1 will only read rows 1, 4, 7 etc. When thread 1 starts it hits a wait state that causes it to wait, it's logic will be like

1 start
2 wait
3 read next row (1, 4, 7 etc)
4 if more rows are left for me reset my wait state and loop back to 2

Thread 2 will be almost identical

1 start
2 wait
3 read next row (2, 5, 8 etc)
4 if more rows are left for me reset my wait state and loop back to 2

Thread 3 you can probably guess by now.

The trick to this is triggering the wait states the starting process will trigger 1's, thread one will have to trigger two's, two will trigger three's and three's will trigger ones. That way they'll all go in sequence.

If you *don't* have to print them in sequence then that's easy, it's the code above without all the wait state stuff. Google "c# manualresentevent" to get an idea how you code these things.
resent event


这篇关于如何使用多线程显示数据表数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆