帮助使用system.threading [英] Help using system.threading

查看:63
本文介绍了帮助使用system.threading的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

private void button2_Click(object sender, EventArgs e)
           {
           string Scan1Aob = ("?? 00 00 00 FF FF FF FF 00 E1 F5 05 00 00 00 00 01");
           string Scan2Aob = ("11 00 00 00 FF FF FF FF 00 E1 F5 05 00 00 00 00 01");

           button2.Text = "10%";
           Application.DoEvents();
           Aobscan.ReadAOB(Scan1Aob);
           button2.Text = "80%";
           Application.DoEvents();
           if (Aobscan.WriteAOB(Scan2Aob))
               {
               button2.Text = "ON";
               System.Media.SystemSounds.Beep.Play();
               }
           else
               {
               System.Media.SystemSounds.Hand.Play();
               MessageBox.Show("OPS-ERROR");
               button2.Text = "OFF";
               }
           }





我尝试了什么:



大家好。需要帮忙。我想扫描一个进程的内存,但它需要很长时间才能完成。我读到使用踏板这会更快。我想要一个如何使用它的实际例子。非常感谢。



What I have tried:

Hello guys. need help. I want to scan the memory of a process, but it takes too long to finish. I read that using treads this gets a lot faster. I would like a practical example of how I can use this. Thank you very much.

推荐答案

线程不一定能加速任何事情(在某些情况下,它们实际上可能会让你失望)。无论使用多少线程,都必须处理相同数量的数据,线程只允许您通过同时执行多项操作在整个系统中涂抹。

<每个线程都需要一个核心来运行 - 并且Windows会优先分配核心,因此如果您的处理器具有在您开始线程化应用程序时未使用的核心,那么它们可用于处理部分任务。



你需要做的是将扫描分成不同的线程,这样一个线程就可以执行上半部分,而另一个线程执行第二个线程。正是你如何用代码做到这一点...嗯...你所展示的糟糕是另一回事:任何调用DoEvents的代码都是可靠的,因为它表明你还没有真正理解框架你正在编写并且仍在以程序的方式思考,而不是以消息/事件驱动的方式思考。



你究竟如何将其分解将归结为你的Aobscan class是否有效 - 我们没有任何访问权限。首先看一下BackgroundWorker类 - 它可能是将线程与进度报告结合在一起的最简单方法: MSDN [ ^ ] - 该链接包含一个示例。
Threads don't necessarily speed anything up (in some cases they can actually slow you down). The same amount of data has to be processed regardless of how many threads are being used, threading just allows you to "smear it out" across the whole system by doing several things at the same time.

Each thread needs a core to run - and Windows allocates cores on a priority basis, so if your processor has cores which aren't in use when you start threading an app then they can be used to process sections of the task.

What you need to do is split the scan into different threads so one thread does the first half, and the other thread does the second. Exactly how you'd do that with code as ... um ... poor as that you show is another matter: any code which is calling DoEvents is suspect right from the off as it indicates that you haven't really understood the framework you are coding for and are still thinking in a procedural way instead of message / event driven way.

Exactly how you would split that up would be down to how your Aobscan class does it's work - and we don't have any access to that. Start by looking at the BackgroundWorker class - it's probably the easiest way to get threading together with progress reporting: MSDN[^] - the link includes an example.


使用线程可以更快地获得结果,但不能保证,因为它取决于太多的变量。除非您非常了解操作系统如何处理线程,以及您需要做些什么才能实现最高效率,否则最好不要使用它们。



Google会为您提供大量示例代码和相关主题的白皮书。
Using threads may get the results faster but there is no guarantee, as it depends on too many variables. Unless you have a good understanding of how an operating system handles threads, and what you need to do for maximum efficiency, it is best not to use them.

Google will find you plenty of sample code, and white papers on the subject.


这篇关于帮助使用system.threading的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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