Solved

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

Posted on 2016-11-11
5
62 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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 34

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

A new era in Cloud training has arrived.

A day that will go down in Cloud history.. But are you ready for it? Will you accept this Cloud challenge?

Question has a verified solution.

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

A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…

630 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