Solved

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

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

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

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

It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
Migrating to Microsoft Office 365 is becoming increasingly popular for organizations both large and small. If you have made the leap to Microsoft’s cloud platform, you know that you will need to create a corporate email signature for your Office 365…
As a trusted technology advisor to your customers you are likely getting the daily question of, ‘should I put this in the cloud?’ As customer demands for cloud services increases, companies will see a shift from traditional buying patterns to new…

920 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

17 Experts available now in Live!

Get 1:1 Help Now