C# Application - Runs for long periods of time and UI goes greyed out and unresponsive for no apparent reason?

I am writing a data collection application that reads mailbox data from an exchange server, the application works well.  However after a while of running the interface goes all greyed out and all my buttons and interface elements become unresponsive.  I have regular calls to Application.DoEvents(); in the code when it has a fairly tight loop when it's processing the read data so that the interface remains responsive.

Internally it is just making calls to EWS Managed API and then storing the results into an access data file and writing data to text files for logging purposes.

Obviously I can't post the whole application here but I wondered I anyone had seen this behaviour before and could explain why this behaviour is occurring?

Here is an image of the screen when it is a) first running and b) when whatever occurs, has occurred:

a) The application in its normal state.
b) The application when it becomes greyed out.NB the mailbox Processed section has been blurred by me  to protect the innocent, the text in that section looks the same as the surrounding text in the "Number" and "No, Emails Processed" sections.

Thanks for your help.
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

AndyAinscowFreelance programmer / ConsultantCommented:
That is typical of you running out of memory.
SivAuthor Commented:
I am getting this when running in the VS environment and I have had no warnings about memory.  This is a grab of my Task Manager showing what it's using:
Memory usage of VS IDE and Application.
System memory usage.
Or do you mean something like resource handles or something like that being exhausted?

I noticed whilst grabbing the above memory dumps that the process view was occasionally showing "Not responding" just for a few seconds and then it went away again, could that be what is making the UI go grey.  If so, how do I stop that from happening?

Even if for the odd second or two whilst it's getting a gulp of data from the Exchange Server it is going unresponsive, I need the interface to stay non greyed out, as it is key that the user can intervene and pause or stop the application at any time.  In this state the application goes unresponsive even though as far as I can tell there is no reason for it to remain so once I have received the 20 records I am pulling down each time.
AndyAinscowFreelance programmer / ConsultantCommented:
Memory is a resource as are handles.  It is possible to run out of them, even on 64 bit machines with modern OP systems.  (Many years ago it was a much more common problem).
As I said this behaviour is typical of that problem but it isn't exclusive, your problem might be something else entirely.

>>as it is key that the user can intervene and pause or stop the application at any time

For you to be certain that the UI remains responsive then you must implement various things.
Have the time consuming task running in a separate thread is by far the most important.  Leave the main thread of the application for the UI updating.
Rather less important and can depend on what you are doing is to keep track of memory and resource handle usage. For example working with large images can result in problems because of, by design (!!!!!), the GC mechanism not releasing memory.
OWASP: Avoiding Hacker Tricks

Learn to build secure applications from the mindset of the hacker and avoid being exploited.

ste5anSenior DeveloperCommented:
Make your interaction with Exchange and AWS asynch. I would use explicit threads, but BackgroundWorker should do it also.
SivAuthor Commented:
I am new to C# (been a VB programmer for years) and I have been looking at the idea of using separate threads but have always been a but frightened of it as in the past when I have tried to use it in my VB apps it usually caused more troubles that it solved issues.

I am hoping that with the more modern VS environment and the DNF 4 which I am using for this application it may be less fraught.

You may have noticed I have a button on the UI to run calculations, part of what this application does is to provide reports on how many emails have been sent and received etc. so that users can improve their email behaviour.  I had started to write a routine that when the user clicked the button to initiate that or it occurs at a pre-set time during the week that it would run that process as a separate thread.

My code to call that routine looks like this:

if(DateTime.Now.DayOfWeek == DayOfWeek.Sunday)
        var th = new Thread (DoCalculations);


Open in new window

If I have followed the stuff on threading when I do the above the "DoCalculations" method I have created to handle the processing will kick in and run in parallel to the main data collection processing and just run until it completes.

So I am thinking all I do to split the running of the data collection off into a separate thread is to do a similar thing at application start up and instead of directly calling the data collection routines I would do a similar thing to the above?
AndyAinscowFreelance programmer / ConsultantCommented:
Sounds a good way to go about it.
Important - leave the UI stuff in the main thread of the application, just transfer the number/data crunching to separate threads.  Trying to do the reverse will work eventually but only after a lot more effort on your part.
ste5anSenior DeveloperCommented:
Make yourself familiar with BackgroundWorker. It is a pretty simple start in threaded programming.
ste5anSenior DeveloperCommented:
btw, as I see in your code snippet: How long should your program run?

I looks like you're implementing your own scheduling.. in this case consider creating a scheduled task which executes your application at this point in time.
Éric MoreauSenior .Net ConsultantCommented:
SivAuthor Commented:

Just read through the BackgroundWorker stuff and it does look pretty easy to follow.

Are you of the opinion that the reason my interface greys out is down to the fact that occasionally whilst the application is unresponsive, Windows alters my UI to show that it has become unresponsive, even though a few seconds after that event occurs we are back responsive again?

And, more importantly, when the code is running asynchronously with the main processing occurring in the background the UI will not grey out and allow the user to interact with it?

AndyAinscowFreelance programmer / ConsultantCommented:
Yes: windows does perform the greying out to indicate unresponsiveness.
Yes: putting code into another thread (eg. BackgroundWorker) will help keep a UI responsive to mouse/keyboard actions.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Éric MoreauSenior .Net ConsultantCommented:
SivAuthor Commented:
Thanks for the help guys, I am hopeful implementing the main process as a BackgroundWorker thread will stop the UI issues.
SivAuthor Commented:

Thanks for the link I will bookmark that as your site does look very useful generally, I already bookmarked it.

It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.