[英]What is the best way to implement a method that does some pretty heavy operations like updating a database, retrieving from a database?
I have a piece of code about few hundread lines, this code is inside a while loop which gets executed each time for about 1000 records. 我有一段关于几行hundread行的代码,该代码位于while循环内,每次循环执行约1000条记录。
I want to move this code out of the while loop and place it in a seperate method , what is the best way improve the performance of the application 我想将这段代码移出while循环,然后将其放在单独的方法中,提高应用程序性能的最佳方法是什么?
Implementing the code as a static method, or a non static method 将代码实现为静态方法还是非静态方法
Note: The code contains objects like Datasets, Datareaders, and data adapters whic 注意:该代码包含诸如数据集,数据读取器和数据适配器之类的对象
There is almost no difference between calling a static method and calling an instance method. 调用静态方法和调用实例方法几乎没有区别。 There is a slight difference since one is doing CLR
call
and the other doing virtual call
and CLR has to navigate the types to find the implementation but that is really negligible . 两者之间存在细微的差异,因为一个正在执行CLR
call
,而另一个正在进行virtual call
而CLR必须导航类型以查找实现,但这确实可以忽略不计 。
Your main concern here must be readability when you have a 1000-line code rather than performance. 当您拥有1000行代码而不是性能时,此处的主要问题必须是可读性。 Whatever you do, calling your database will be the slowest part of your code so instead focus on refactoring .
无论您做什么,调用数据库都是代码中最慢的部分,因此请专注于重构 。
静态或非静态对您的情况没有影响。
I would move the code into the DoWork
method of a BackgroundWorker
. 我将代码移到
BackgroundWorker
的DoWork
方法中。 This way, your code can run in a background thread, give periodic progress updates to the main UI thread, and let the UI thread know when the operations are completed by invoking the WorkerCompleted
event. 这样,您的代码可以在后台线程中运行,对主UI线程进行定期进度更新,并通过调用
WorkerCompleted
事件让UI线程知道何时完成操作。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.