Solved

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

Posted on 2016-11-11
5
60 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 63

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 63

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

Guide to Performance: Optimization & Monitoring

Nowadays, monitoring is a mixture of tools, systems, and codes—making it a very complex process. And with this complexity, comes variables for failure. Get DZone’s new Guide to Performance to learn how to proactively find these variables and solve them before a disruption occurs.

Question has a verified solution.

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

Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
Many of us here at EE write code. Many of us write exceptional code; just as many of us write exception-prone code. As we all should know, exceptions are a mechanism for handling errors which are typically out of our control. From database errors, t…
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…

679 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