简体   繁体   English

GUI中的C#多线程方法

[英]C# multi-thread approach in GUI

I'm developing a small app. 我正在开发一个小型应用程序。 Need your help. 需要你的帮助。

I have a table of 10 columns. 我有一个10列的表格。 Say, i select 5 rows in a list view. 说,我在列表视图中选择5行。 I take all the values of col_1, in a list, pass it to a method. 我将col_1的所有值放在一个列表中,并将其传递给方法。

If all values are equal, set combo_box1 value = "equal" else value = "not equal". 如果所有值均相等,则将combo_box1的值设置为“等于”,否则将值设置为“不等于”。

Current Approach: 当前方法:

I have 10 lists ( seems pretty lame... isn't it? i had asked a ques regarding this...), one for each col. 我有10个清单(似乎很la脚...是吗?我问了一个有关此的问题...),每个清单一个。

10 calls to the method that checks for equality of values, each for one list. 该方法的10个调用检查值的相等性,每个调用一个列表。 Subsequently, set the combo box's(10 combo boxes) values. 随后,设置组合框的(10个组合框)值。

If i have say 100 records, i guess the time taken will increase. 如果我说100条记录,我估计花费的时间将会增加。 So, i thought of implementing threads. 因此,我想到了实现线程。

Effort Put: 投入的精力:

I have used the this.Invoke(new Delegate...) approach for a thread which tries to access a control of Main thread. 我已经使用this.Invoke(new Delegate ...)方法来尝试访问主线程的控件。 It works fine. 工作正常。 I tried to manipulate this according to my needs. 我试图根据自己的需要进行操作。 Couldn't do so. 无法这样做。 Please help me out guys. 请帮我。

[EDIT] [编辑]

the main culprit was the image comparison... its taking awfully long time to finish... below is the code... i'm storing all the image(say col no 3) of say 10 rows in a list... 罪魁祸首是图像比较...完成它花了很长时间...下面是代码...我正在列表中存储所有说10行的图像(比如说第3行)...

// other stuffs [DllImport("msvcrt.dll", CallingConvention=CallingConvention.Cdecl)] private static extern int memcmp(IntPtr b1, IntPtr b2, long count);

// create a list of images MemoryStream imageStream = new MemoryStream(tempImage.Data.Data); Bitmap artCoverImage = new Bitmap(imageStream); // culprit ? artCoverList.Add(artCoverImage);

// call the method CheckIfEqual(artCoverList) // culprit ?

// THE method private void CheckIfEqual(artCoverList) { Bitmap tempBitMap = artCoverList[0];

foreach (Bitmap bmp in artCoverList) { if (bmp == null) return false; if (bmp.Size != tempBitMap.Size) return false; var bd1 = tempBitMap.LockBits(new Rectangle(new Point(0, 0), tempBitMap.Size), ImageLockMode.ReadOnly, PixelFormat.Format32bppArgb); IntPtr bd1scan0 = bd1.Scan0; int stride = bd1.Stride; long len = stride * tempBitMap.Height; var bd2 = bmp.LockBits(new Rectangle(new Point(0, 0), bmp.Size), ImageLockMode.ReadOnly, PixelFormat.Format32bppArgb); IntPtr bd2scan0 = bd2.Scan0; returnValue = memcmp(bd1scan0, bd2scan0, len) == 0; bmp.UnlockBits(bd2); tempBitMap.UnlockBits(bd1); } }

I had spent a whole day to get this part image comparison working... i think the part where it converts mem stream to bmp is the culprit... 我花了整整一天的时间来进行部分图像比较工作...我认为将mem流转换为bmp的部分是罪魁祸首...

[EDIT 2] [编辑2]

guys...need your help... any idea... how to compare a list of images... other than above... 伙计们...需要您的帮助...任何想法...如何比较图片列表...除上述之外...

Thanks, 谢谢,

Dev 开发

Implementing a threaded design has its own overhead and adds complexity. 实施线程设计有其自身的开销,并增加了复杂性。 You really only want to consider moving to a threaded model if performance is poor enough to warrant the added complexity. 您确实只想在性能差到足以保证增加的复杂性的情况下考虑使用线程模型。

If you have 100 rows and you are checking equality of 10 columns, then you're doing 1000 equality checks. 如果您有100行,并且要检查10列的相等性,那么您要进行1000次相等性检查。 Depending on the implementation, this should be a trivial operation with negligible performance ramifications. 根据实现的不同,这应该是微不足道的操作,并且对性能的影响可以忽略不计。

Consider profiling your application to see if there is a performance issue. 考虑对应用程序进行性能分析,以查看是否存在性能问题。

EDIT 编辑
Based on your findings (that image comparison is the culprit), you may want to consider using a simple checksum comparison against the respective image byte arrays. 根据发现(图像比较是罪魁祸首),您可能要考虑对各个图像字节数组使用简单的校验和比较。

Here is an example of how to do so: http://www.dreamincode.net/code/snippet2859.htm 这是有关此操作的示例: http : //www.dreamincode.net/code/snippet2859.htm

Note: MD5 is sufficient in this case and should prove to be slightly faster. 注意:在这种情况下,MD5就足够了,应该证明速度稍快一些。

I would say that instead of using Invoke, check out BackgroundWorker . 我想说的是,而不是使用Invoke,请查看BackgroundWorker It has callback events that automatically run on the main thread, so it makes updating your UI when you're done easier, so as to avoid cross thread UI exceptions. 它具有自动在主线程上运行的回调事件,因此它使您在完成操作后更新UI更加容易,从而避免了跨线程UI异常。

BackgroundWorker worker = new BackgroundWorker();
worker.RunWorkerCompleted += (s, e) => updateUI();
worker.DoWork += (s, e) => longProcess();

worker.RunWorkerAsync();

检查的一种方式,如果列表中的所有元素都是相同的是把它们塞进一个HashSet并检查项目金额HashSet算账:如果你只有一个项目,那么他们都是平等的...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM