Solved

How would I write a message when there are no files to process in the foreach loop?

Posted on 2016-11-11
5
42 Views
Last Modified: 2016-11-11
I am writing a C# application.

I use:  foreach (string file in files1)
            {              
            }

How would I write a message when there are no files to process in the foreach loop?
0
Comment
Question by:zimmer9
5 Comments
 

Expert Comment

by:Omar Alaa
Comment Utility
please make it clear what is files 1 and what is your problem exactly
0
 
LVL 62

Expert Comment

by:Fernando Soto
Comment Utility
Please post the actual code and declarations of those variable not declared in the code.
0
 

Author Comment

by:zimmer9
Comment Utility
In the following function, how would I exit the application and inform the user that there are no files to process?


public static void ProcessDirectory(string TARGET_DIR_IN, StreamWriter sw1)
        {
            string[] files1 = Directory.GetFiles(TARGET_DIR_IN, "*_FRONT.TIF").Select(path => Path.GetFileName(path)).ToArray();
            string[] files2 = Directory.GetFiles(TARGET_DIR_IN, "*_REAR.TIF").Select(path => Path.GetFileName(path)).ToArray();
            ManualResetEvent[] doneEvents = new ManualResetEvent[files1.Length];
            _numerOfThreadsNotYetCompleted = files1.Length;
            int i = 0;

            foreach (string file in files1)
            {
                string checkPrefix = file.ToUpper().Replace("_FRONT.TIF", "");
                string checkBack = checkPrefix + "_Rear.TIF";
                string checkImage = checkPrefix + ".tif";
                file_tif_date = checkPrefix.Substring(0, 8);

                if (File.Exists(TARGET_DIR_IN + checkBack))
                {
                    if (!File.Exists(TARGET_DIR_IN + checkImage))
                    {

                        //doneEvents[ I ] = new ManualResetEvent(false);
                        ProcessImageThread chk = new ProcessImageThread(checkPrefix, TARGET_DIR_IN);
                        ThreadPool.QueueUserWorkItem(chk.ProcessImage, i);
                        i++;
                    }
                }
                else
                {              
                    sw1.WriteLine(checkBack + " file is missing");
                    if (sw1 != null) sw1.Close();
                    Environment.Exit(0);                    
                }
            }

            //Wait for all threads in pool to calculation...
            //WaitHandle.WaitAll(doneEvents);

            _doneEvent.WaitOne();

            //Console.WriteLine("All image processing are completed.");

            foreach (string file in files2)
            {
                string checkPrefix = file.ToUpper().Replace("_REAR.TIF", "");
                string checkFront = checkPrefix + "_FRONT.TIF";

                if (File.Exists(TARGET_DIR_IN + checkFront))
                {
                }
                else
                {
                    sw1.WriteLine(checkFront + " file is missing");
                }
            }

            if (sw1 != null) sw1.Close();
        }
0
 
LVL 62

Accepted Solution

by:
Fernando Soto earned 500 total points
Comment Utility
Hi zimmer9;

In both of the foreach loops it will continue to execute the foreach as long as there are items in the collection, in this case files1 and files2. So if you want to post some message when there are no more files to process in files1 then do it as the first statement after that foreach loop and the same goes for files2 foreach loop.
0
 
LVL 32

Expert Comment

by:it_saige
Comment Utility
Just check the count (or length) before you enter your loop and take the appropriate action, if any; e.g. -
public static void ProcessDirectory(string TARGET_DIR_IN, StreamWriter sw1)
{
	string[] files1 = Directory.GetFiles(TARGET_DIR_IN, "*_FRONT.TIF").Select(path => Path.GetFileName(path)).ToArray();
	string[] files2 = Directory.GetFiles(TARGET_DIR_IN, "*_REAR.TIF").Select(path => Path.GetFileName(path)).ToArray();
	ManualResetEvent[] doneEvents = new ManualResetEvent[files1.Length];

	if (files1.Count() == 0)
	{
		// You can generate a message or output or whatever to alert that there are no files to process
		Console.WriteLine("There are no front images to process");

		// Return from the method
		return;
	}

	_numerOfThreadsNotYetCompleted = files1.Length;
	int i = 0;

	foreach (string file in files1)
	{
		string checkPrefix = file.ToUpper().Replace("_FRONT.TIF", "");
		string checkBack = checkPrefix + "_Rear.TIF";
		string checkImage = checkPrefix + ".tif";
		file_tif_date = checkPrefix.Substring(0, 8);

		if (File.Exists(TARGET_DIR_IN + checkBack))
		{
			if (!File.Exists(TARGET_DIR_IN + checkImage))
			{
				//doneEvents[ I ] = new ManualResetEvent(false);
				ProcessImageThread chk = new ProcessImageThread(checkPrefix, TARGET_DIR_IN);
				ThreadPool.QueueUserWorkItem(chk.ProcessImage, i);
				i++;
			}
		}
		else
		{
			sw1.WriteLine(checkBack + " file is missing");
			if (sw1 != null) sw1.Close();
			Environment.Exit(0);
		}
	}

	//Wait for all threads in pool to calculation...
	//WaitHandle.WaitAll(doneEvents);

	_doneEvent.WaitOne();

	//Console.WriteLine("All image processing are completed.");

	if (files2.Count() == 0)
	{
		// You can generate a message or output or whatever to alert that there are no files to process
		Console.WriteLine("There are no back images to process");

		// Return from the method
		return;
	}

	foreach (string file in files2)
	{
		string checkPrefix = file.ToUpper().Replace("_REAR.TIF", "");
		string checkFront = checkPrefix + "_FRONT.TIF";

		if (File.Exists(TARGET_DIR_IN + checkFront))
		{
		}
		else
		{
			sw1.WriteLine(checkFront + " file is missing");
		}
	}

	if (sw1 != null) sw1.Close();
}

Open in new window


-saige-
1

Featured Post

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

In my previous article (http://www.experts-exchange.com/Programming/Languages/.NET/.NET_Framework_3.x/A_4362-Serialization-in-NET-1.html) we saw the basics of serialization and how types/objects can be serialized to Binary format. In this blog we wi…
Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
This video discusses moving either the default database or any database to a new volume.
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…

772 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now