Solved

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

Posted on 2016-11-11
5
55 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
ID: 41884051
please make it clear what is files 1 and what is your problem exactly
0
 
LVL 62

Expert Comment

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

Author Comment

by:zimmer9
ID: 41884068
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
ID: 41884176
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 33

Expert Comment

by:it_saige
ID: 41884196
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

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
Along with being a a promotional video for my three-day Annielytics Dashboard Seminor, this Micro Tutorial is an intro to Google Analytics API data.
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…

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