c# continue and complete executing code after wpf application exit - c#

Background: I am working on a c# WPF application where, at some stage, user instructs the application to do some work. The work may take substantial amount of time and the user may not wish for the work to be completed and may exit the WPF application - with the intention of returning at a later time to review the results - which the application gets from a DB.
Question: Looking for is a way for the WPF application to finish the work - regardless of whether the WPF application has exited or not.
Choice 1: I could write a worker application that does the work - and start a process from my WPF application that executes this worker application.
Choice 2: Spawn a thread / backgroundwoker that does the work. This would have the additional advantage of presenting the results to the user, should the application still be active.
I would much prefer choice 2, if it were feasible. Would appreciate any advice - other alternatives, things to watch, relevant links etc. Almost all links I have seen are focused on successfully terminating threads prior to application exit, and none that I have found talks of the opposite scenario.

Related

Restore async loop on app relaunch

I have a UWP app that needs to check for new data (from network) every x seconds. I do this by starting a new async thread. The app can be minimized or even navigated from in Windows Phone, and this suspends the app.
How do I restore the loop when the user goes back to the app again (it appears to be still open, at the same Page)? This can be done either by re-navigating to the Page in question on relaunch or by restoring the loop. What's the easiest way to do so?
Generally when your app is suspended, if you don't cancel your tasks, the OS will do it. Therefore it's recommended that you will send a suitable signal to your tasks along with saving some progress for resuming operation.
However, in UWP there is something like ExtendedExecution - it should allow your app to run in background up to 10 minutes (as far as I know this is the limit for battery powered devices - not 100% sure about this, and it of course may change on various scenarios).
Nevertheless, I think that checking for changes in few seconds interval may be a bad idea. Maybe you can change the design little and use BackgroundTasks which are designed for background processing.
Most of information about app states you will find at MSDN.

How to use Time Trigger in background task in Windows 8 JS Metro App

I have a requirement where I have to call a service in background after every let say 1 hour to get some informations from server. I am working on JavaScript Metro Application. I have tried the background task and used Time Trigger and I have scheduled it to get triggered in every 15 minutes. It get called first time and then it is never called. I didn't close the background task because I want it to run all the time and call the service at scheduled time.
I have used the Microsoft Background task sample for reference.
Please tell me what should be the best approach to call a service in background.
How to use Time Trigger and Why Time Trigger doesn't get called after first time?
Please share code sample or walkthrough if any.
Thanks
First thing you should do is to close the background task properly as instructed in the documentation - if your tasks don't behave nicely, platform might suspend and refuse to run them for some time. You should let the platform handle triggering of the events based on the triggers and conditions you define instead of trying to bend the system. Also, remember that there's CPU and data usage quotas for background tasks present, one can't do massive amount of processing in background tasks - if the quotas are exceeded, tasks will get suspended. Be also sure that the background task works and doesn't throw errors.
In general, my recommendation is that one shouldn't rely solely on background tasks to fetch the information since it's not guaranteed that they manage to do it on time, so better to prepare for downloading the needed data in the foreground app as well. This obviously depends on the use case: if the data fetched in background tasks is not critical but more like nice-to-have, there's much less to worry about.
The TimeTrigger requires the app being added to the lock screen (see docs), but I guess you already meet this requirement since you've managed to get the task running once.
For debugging the background tasks, please take a look at Event Viewer, see detailed instructions. That page also contains some tips about common problems. The Event Viewer entry mentioned in that document is often a valuable resource for figuring out problems with the bg task execution. My guess is that you'll see errors there related to not closing the task properly.

messagebox causes poor responsiveness windows phone 8

When I run my app in store test kit (Automated test-Windows phone application analysis) I get a red mark near responsiveness as app responsiveness is poor, I am using a background thread to call a web service and display a messagebox with particular result from web service.
I have tried using separate thread and dispatcher for displaying the message box, still I get the same results in store kit.I am sure that other parts of my code are not causing it, when I comment out messagebox.show(), the app passes responsiveness test.
There is one more link I found relevant http://social.msdn.microsoft.com/Forums/wpapps/en-us/dfd3bc28-6a90-4a77-92f7-c42982eafa40/app-poor-responsiveness-in-wp7?forum=wpdevelop it says this would not affect the certification process,Can some one please confirm this or provide more details of how do I go about making my app responsive?
Message boxes are synchronous, when you display one then the UI thread stops processing the other elements of your interface. That why an automated testing tool will detect it as a lack of responsiveness, even though it's on purpose. Don't worry about it, it won't affect the certification process.

What options do I have for long running processes in my C# Console Application?

I'm working on a console app that kicks off a fairly long running process (2-3 minutes). What options are available for displaying progress, or even just writing a "." to screen every few seconds so the user knows the app hasn't stopped responding?
You have (at least) 3 ways to do this:
Simplest: On program start, set a global variable to false, then start a thread that writes a dot to the console, Thread.Sleep()s a second, repeats until the variable is set to true. On your main thread do your work, when finished set the variable to true and then Join() the other thread.
Still simple: Add another global variable to the mix, type int. In your worker thread increase it, whenever some progress is made, on the console writer thread reduce interval to say 250ms, but draw a dot only if progress counter has changed. This gives an idea of how fast your app progresses
A bit advanced: Create a boolean as in first step plus an AutoResetEvent, let the console writer thread repeatedly wait on the event, when app makes progress Set() the event. When finished set boolean to true and Set() again before Join()ing. The advanced part in this is to know, in what portions to report progress.
The simplest approach is if your application has an incremental loop then you could put a simple full stop on the the console. Not nice, but it does give an indication of "I'm alive".
But that approach can result in too much info (too many characters going to console) or too few as your hooking into a bit of the code that does not really have that responsibility.
So, perhaps a nicer way that you can reuse is to create a console user class that is run in a thread with a 1 second tick. It can use backspacing and the good old | / | / - sequence to give the impression of something rotating. It can also check for key presses to allow the user to exit.
Another option, depending on the nature of your users, is to use a logging framework like NLog. That way your implementation is UI independent and you can see what is happening in more or less detail at run time in the console, from another machine, whatever.

Opinion on User Experience - C# Winforms

I’ve got a process which will take a little under 5 seconds to complete. The user will most likely notice the program flicker for a few seconds after pushing the “go” button.
My question is:
Is this something that would normally be dumped onto a background worker, or is there another .NET method for handling small tasks, or is this something that shouldn’t be a concern?
FYI:
The process opens a user specified excel file, processes an unknown number of lines (max 1.5 million due to excel I believe), and queries a database (very quick query). So at the worst case scenario the user uploads a 1.5 million row excel file and is running on a very slow internet connection.
If you don't want the user to be able to do anything while the file is being uploaded, then you don't need to put it on a different thread.
If you want the user to be able to go on to other tasks while the file is uploading, put it on a different thread.
As a general rule of thumb, if I have a situation where I absolutely don't want the user to do anything while a long-running process is going, I disable the controls on the form until the task is complete, and usually use a status indicator to show that progress is happening.
My personal guideline for whether or not to allow user interaction is if the results of a process could be altered by a user action in mid-stream.
For example, one program that we have parses a bunch of queries on a highly normalized database (normalized to the point where reporting is sloooow) into "reportable" tables, and I don't want the user altering data in one of the source tables while the query is running, because it will give goofy results.
If there is no harm in allowing user interaction while the process is occuring, then put it in another thread.
Edit
Actually, on reading #UrbanEsc and #archer's comments, I agree with them. Still put it on a different thread and freeze the controls (and include a progress indicator where possible).
I would push this to a background worker. Doing so will keep the UI responsive. If the process ever does lag for more than a few seconds, users start getting nervous ...especially when the lagging process causes the UI to be 'frozen'.
From a user experience point of view it might be best to hand the job over to a different thread or an asynchronous worker and tell the user that his request is being processed in the background. Once the worker finishes, a success/failure message can be handled and shown to the user as required.
The cheapest way to handle the problem is to turn the cursor into an hourglass during the processing. That tells user please wait, I'm busy.
According to the budget (time and/or effort) you're willing to throw in it, using a backgroundworker and some reporting GUI is certainly a plus. But it's up to you according to your app.
For example, I'm currently modifying an in-house app that has 3 users. In that case, the hourglass is OK: All 3 of them will quickly learn they just have to wait. Don't get me wrong: this app is damn important. Without it, the small company that uses it would just die. But if I ask them for 2 hours of extra budget for a nice and tested little GUI, background thread, blah vs an hourglass, what do you think they'll say?
On the other hand, if it's an important operation in your flagship product, of course be nice to your users! Don't hesitate: background thread. Especially if the operation may actually take much longer than those 5 seconds.
Conclusion: Be pragmatic!
I would put it into a background worker or fire of a task if you are in .NET 4.0, for example:
void OnButtonClick(...)
{
new TaskFactory().StartNew(() => { /* your excel and query code */ });
}
I'll vote for the background worker process, since a frozen UI is like a frozen application, and most of users will think your application isn't doing anything at all.
UI thread for a progress bar or some animation, info text noticing what's going on + background worker thread = win
I think every process not related with the UI itself should be started as a separate thred or, in this case, as a bg worker. This will help to maintain the app healthy and easy to improve/fix in the future.
Also, as a user or tester, I really hate flicking and freezing windows...
Regards.
A general rule of thumb is any operation that takes a second or longer to complete requires some form of feedback to the user. This can be a progress bar, message, etc. Anything longer then that then the user becomes frustrated (not sure if they did something wrong, hate waiting, etc).
For operations like this that can take longer based on the environment (number of apps, available memory, data size, hard drive speed, etc) they should ALWAYS be put on a background thread and pipe messages back to the UI. I love the BackGroundWorker for this.

Categories

Resources