vb.net计时器和后台工作者 [英] vb.net timer and background worker

查看:77
本文介绍了vb.net计时器和后台工作者的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不久前编写了一个观察程序,需要进行一些更新.我认为自己可以在纠正问题的同时进行纠正.现在,这非常简单.它具有一个计时器(仅是Windows窗体计时器),该计时器每隔几秒钟或几分钟就会滴答一次,具体取决于XML文件中的用户设置.每次打勾时,它都会查找要处理的文本文件,将其抓取,进行解析,然后将数据放入数据库中.它已经运行了好几年了.

问题是,如果有大量文件要立即处理,而用户想停止系统,那将非常棘手.您必须一直单击停止"按钮,直到在两次刻度事件之间的代码中找到正确的位置.移动或最小化表单也是一种痛苦.如果它是在计时器滴答事件的中间,那么您必须等待.另外,用每个文件的状态更新用户状态的表单上的文本框仅在每个刻度上更新,因此,如果它在一个刻度内处理20个文件,则必须等待所有这些文件查看发生了什么.即使完成了工作,显然也不是理想的设置.

即使有几十个要处理,每个滴答一次也只处理几个.无论如何,我将后台工作人员代码添加到了我刚刚构建的新应用程序中,并希望通过此应用程序来完成.但是我给人的印象是,将其应用于计时器驱动的应用程序是不同的.我读到,对于Windows窗体,使用system.threading.timer是错误的(此应用程序以窗体形式运行,而不是作为服务运行).所以我不确定该如何处理.

它现在设置的方式是在计时器滴答事件中调用例程来检查要处理的文件.如果有文件,则该例程将文件名保存在数组中,然后循环遍历该数组.在循环中,调用另一个例程来一次处理每个文件.如果这样做是正确的方法,我不知道将后台工作程序代码放在哪里.

任何帮助将不胜感激.

谢谢.

I wrote a watcher program a while back that needs some updating. I figured I''d correct an issue while I do that. Right now it''s pretty simple. It has a timer (just a windows forms timer) that ticks every few seconds or minutes depending on a user setting in an XML file. At each tick, it looks for text files to process, grabs them, parses them and then puts the data into a database. It''s been running fine for a few years now.

The problem is that if there are a lot of files to process at once and the user wants to stop the system, it''s very tricky. You have to keep clicking the stop button until you get just the right spot in code where it''s between tick events. it''s also a pain just moving or minimizing the form. If it''s in the middle of a timer tick event then you have to wait. Also, the text box on the form that updates the user with the status of each file only gets updated on each tick so if it processes 20 files in a tick then you have to wait for all of them to see what happened. Not the ideal setup obviously even though it gets the job done.

It only processes a few at a time for each tick even if there are dozens to process. Anyway, I added background worker code to a new app I just built and would like to do that with this one. But I''m getting the impression that working that into a timer driven app is different. I read that using system.threading.timer is wrong for windows forms (this app runs as a form, not as a service). So I''m not sure how to approach this.

The way its'' set up right now, is that on the timer tick event, it calls a routine that checks for files to be processed. If there are files, then that routine saves the file names in an array and then loops through the array. In the loop, another routine is called to process each file one at a time. I don''t know where to put the background worker code if that''s the right way to do it.

Any help would be appreciated.

Thanks.

推荐答案

帮自己一个忙,不要为此目的使用任何计时器-避免麻烦!

相反,请始终创建和使用处理线程.即使在您需要像执行轮询那样的情况下(它本身很糟糕,但是您可能无法更改体系结构,但是-参见下文),您也可以在该线程中循环使用Thread.Sleep来实现定期数据处理.这种架构是稳定的,基于计时器的架构则不稳定. (您是否尝试设计对情况的正确处理,当由前一个事件触发的处理程序尚未完成时,如果引发了新的计时器事件,则该线程不会有此类问题.)

要查看涉及的内容,请查看我对类似主题过去的回答的集合:
如何获取keydown事件在vb.net中的不同线程上操作 [
Do yourself a big favor and do not use any kind of timers for this purpose — stay away of trouble!

Instead, always create and use a processing thread. Even in the case you need to do the polling as you do (which is bad by itself but you probably cannot change the architecture, but — see below), you can use Thread.Sleep in this thread in a loop to implement periodic data processing. This schema is stable, the one based on a timer is not. (Did you try to design correct handling of the situation, when a new timer event is fired when the handler triggered by the previous event is not yet finished? With a thread you won''t have such problems).

To see, what''s involved, look at my collection of my past answers on similar topics:
How to get a keydown event to operate on a different thread in vb.net[^].

Maybe you still can avoid polling of the file structure. Use the class System.IO.FileSystemWatcher; it will notify to any changes in file structure you subscribe to.

—SA


看看此文章 [ ^ ].这篇文章是用c#编写的,但应该可以给您一个想法.

您可以将文件处理放入后台工作器中,然后在处理每个文件(或使用哪种逻辑)之前,您可以检查是否CancellationPending并停止了线程并向用户报告了必要的进度.
Take a look at this article[^]. The article is in c# but should give you an idea.

You can put your file processing in the background worker and before you process each file (or what ever logic you use) you could check if CancellationPending and stop the thread and also report the necessary progress to the user.


这篇关于vb.net计时器和后台工作者的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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