停留在foreach循环中 [英] Stay in foreach loop

查看:94
本文介绍了停留在foreach循环中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用foreach循环处理文件夹中的图像.但我也想让代码停留在循环中,并等待新的图像.我该怎么做?感谢您的帮助.

是否有任何循环或类似foreach的用法?


dcan

i''m using foreach loop for processing the images in a folder. But also i want to code stays in the loop and waits for a new image. how can i do it? thanks for your helps.

edit : is there any loop or something like foreach for this usage?


dcan

推荐答案

您可以看看
http://msdn.microsoft.com/en-us/library/system.io. filesystemwatcher.aspx [ ^ ]
您可以在观察到的文件夹中检测到新文件时运行代码,而不必陷入循环之中,这不是一个好主意.

在OP评论(1)之后
我重复一遍:NOT一个好主意是保持未定义的循环,只是等待一个新图像.如果您真的更喜欢这样做,请检查其他解决方案,但建议您不要这样做.

如果您只想使用最后的图像,则仍然可以使用FileWatcher.一种方法可能是:
You might have a look to
http://msdn.microsoft.com/en-us/library/system.io.filesystemwatcher.aspx[^]
you could run your code when you detect a new file in the observed folder instead of staying in a loop, what can be not such a good idea.

Edit after OP comment (1):
I repeat it: It is NOT a good idea to stay in the loop undefined, just waiting for a new image. If you really prefer to do it that way... then check the other solutions, but you are adviced not to do it.

If you only want to work with the very last images, you can still use the FileWatcher. One way could be:
- When Filewatcher says that there is a new file, save the timestamp. 
- Run your Foreach but asking in first place the "last change date" of current file.
   - If the date of the current file is much older, jump to next. 
   - If the timestamp of the file is (let's say) in a +-5 minutes range from the Filewatcher event...
      - check if the file is a picture you are interested in
         - if yes... then process it.
         - if not... check the next one.



如果您在那台计算机上更改操作系统的日期和时间,那可能会遇到一些问题.最坏的情况:在更改日期和时间后,它将一次性处理该文件夹的所有图片.
其他可能性可能是保留已完成的名称的列表(要求提供名称,而不是时间戳).



That could face some problems if you change the date and time of the OS in that computer. Worst case scenario: It would process all pictures of the folder one time after a change of Date&Time.
Other possibility could be to mantain a List of the names that are done (asking for name, instead of timestamp).


不是100%肯定您要问的是什么,但我相信以下内容会做:

Not 100% sure of what you are asking but I believe the below will do:

bool stayInLoop = true;
do
   foreach (Image myImage in AllTheseImages)
   {

      //process your images

   }
   if (someCondition = true)
   { stayInLoop = false;}
while (stayInLoop)


我想我想您应该从集合中删除该图像,例如:

I guess I thought you would remove the image from the collection like:

bool stayInLoop = true;
do
   foreach (Image myImage in AllTheseImages)
   {
 
      //Do whatever you need to do with the image then remove it
      AllTheseImages.Remove(myImage);

   }
   if (someCondition = true)
   { stayInLoop = false;}
while (stayInLoop)



这样,每次循环时,它只会处理添加到集合中的新图像.

如果您无法编辑收藏集,则可以尝试以下操作:



That way each time through the loop it''ll only handle new images added to the collection.

If you can''t edit the collection you could try something like:

bool stayInLoop = true;
int i = 0 ;
int highestCountSoFar = 0;
do
   i = 0;
   foreach (Image myImage in AllTheseImages)
   {
 
      i = i +1;
      if (i > highestCountSoFar)
      {
         highestCountSoFar = i;
         //process the photo
      }

   }
   if (someCondition = true)
   { stayInLoop = false;}
while (stayInLoop)



但是,这效率很低,因为您每次都要重新检查整个集合.



But, that''s pretty inefficient since your going to reexamine the entire collection each time.


这篇关于停留在foreach循环中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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