Working with Multiple BackGroundWorker with Different Task or Operation - c#

As a .NET developer i have been working 3 C# Windows applications (database updation, crawling data, and downloading an Excel file for importing to database). I have create these 3 projects separately and these 3 application run in background and show every notification on desktop only.
My questions:
How to run these 3 applications on single application?
How to run my second application in background? How to start it when i shutdown and restart the system or laptop?
My 3rd application downloads and imports. once i start it downloading and importing process is once a week or a month, so i have a date picker select today date (06/10/2018 saturday) and week option. How to continue when i shutdown and restart my system?
How to continue where it stopped after i shutdown or restart my application?
These 3 applications are running on internet. How to pause and and run when internet available only?
How to show windows form applications notification on web application?

First: Yes it is possible, do it on 3 separate threads in one process (application). More here
Second: Simply Start application on startup and i would implement in my application code to see if there is one more instance of application running and if it is i would close this one.
Third: Store next download date in some database or txt file. On application start check if(nextDownloadDate > today) //Which mean it should have been updated and if it is true update what you need and update next download date. This way if you turn off your pc (app) and you startup pc again (automatically run app) it will check if it should have been updated and if it is it will do the job
Fourth: Too broad. Try googling or thinking about way of storing data in database/txt/xmal each time you make step and again on application run check from that files where you have stopped
Google it. Not that hard. "How to check if computer have internet access".
Too broad. Think yourself.
Few items i haven't answered since this question is too broad and should be closed but since i see you are new one i made some advises up there for you.

Related

C# - An approach for an automatic updater for a windows app

I have a windows app (written in C#...) that is installed on multiple remote workstations, the installation is being done by a MSI package.
An updated installation with changes to the program is being conducted from time to time, at this point, for each update, for each station we need to go to where the .MSI is located copy it and run it and only than start the app.
I would like that: whenever a user tries to start the app a background process will be initialized that will compare the installed version with the most recent version on the .msi location and if an update in needed will run the installation and than start the app.
The problem is that it can't be done from within the program since the program cannot be running when the installation/upgrade is taking place. Another consideration is that not all the stations operating at the same time so I can't schedule a timely upgrade, and it can't be done on stations boot since sometimes the updates needs to be done while the station is already operating(the station has several functions beside my app).
I have considered several approaches, it seems like a windows service could do the trick but I don't know if it could be "bound" to the initialize of the program and if it could suspend the program to do the necessary checks and updates and only than to restart the app.
I am open to all ideas so please don't feel obligated to my ideas...
at this point, for each update, for each station we need to go to where the .MSI
is located copy it and run it and only than start the app.
Fire the guy pretending to be a system administrator.
Unless you have done something odd in your installer - it should be doable with your standard software distribution package. Heck, I can roll out updates with active directory ONLY and no third party software as long as the MSI allows administrative no ui installs.
You try to fix a non-problem. Software distribution is a solved solution for the last 15 to 20 years. MSI was particularly created to handle this issue because other approaches demonstrated issues.
So, whoever pretends to be the administrator on your company needs to get his act together and be one. Do nothing (except making a good MSI) and let the admin do his job.
Everything else just creates a lot of problems (at least in the cost side). And it is totally not needed.
I'm updating my answer,
This is what we did....
Create the Installer / Package ( you can install for all users here)
Generate Bootstrapper (https://msdn.microsoft.com/en-us/library/ms165429.aspx
You can use this tool create bootstrapper (http://www.softpedia.com/get/Programming/Other-Programming-Files/Bootstrapper-Manifest-Generator.shtml)
Add dependencies and other conditions in bootstrapper
Set the URL for updates
This will solve your problem. I was too quick to answer but this how we did.
Thanks.
Some comments and answers about the assumptions in the question:
"I would like that: whenever a user tries to start the app a background process will be initialized that will compare the installed version with the most recent version on the .msi location and if an update in needed will run the installation and than start the app."
The only time this is likely to be a useful approach is when the MSI is at a company's web server. The web site can host a web api that you send your ProductCode, Version, Upgrade to and it reports whether there is an upgrade, patch etc, and a location to download it from. In a company domain, just use AD, as has been said.
"The problem is that it can't be done from within the program since the program cannot be running when the installation/upgrade is taking place."
Why? Windows Installer will show FilesInUse dialogs for the end user to close down the running app. So this situation is already dealt with, and I'm not sure where you see a problem.
"...and if it could suspend the program to do the necessary checks and updates and only than to restart the app."
This is exactly what Restart Manager is for. Integrate your app with RM and Windows Installer will allow you to close down the app (saving whatever data you need to recover) and then restart you afterwards so you can recover your data and the user sees a minimal interruption. One example:
http://www.codeproject.com/Articles/772868/Restart-Manager-Support-For-Windows-Application
So I think TomTom's point is valid - there is no need to re-invent what AD does, or worry about how to update programs that are running, or how to restart a program after an installer update because all these problems were solved years ago.

C# use multithreading or run as separate application?

I have a windows application that reads from a database and populates multiple Listview containers depending on what items the user selects. Some of the data in a particular Listview is right-clickable with a MenuItem option to ‘Write Data to Excel’. This may take around 10 minutes to complete.
I have the Excel Interop portion written in stand-alone code or can I incorporate it into the application project. The Excel Interop app takes only 1 parameter to do its thing.
My question is… should I incorporate it into the Windows App and use multithreading or run it as a standalone app (which seems more efficient), which is called from the Windows App? And what is a good way of doing that where the Windows app starts the process and then can forget about it.
I think that the answer for this one is answered by the question : "What should happen if the user closes the main application during those 10 minutes ?"
If the file should still be wrote, then a standalone application is perfect, cause your threads won't survive.
If the file creation should be interrupted, then I see no reason not to use multithreading, as it seems simpler by default, in particular debugging this part of the code is way easier if it's in the same application.
I would recommend for the below flow:
We can create our own queue which will be either maintained in database or file system.
Then we can write a scheduler which will fetch all the unprocessed request and with the respect to response mark appropriate status of queued item.
Now You application can just call the queuing process and move forward from there.
Use another thread to write it.

How should I run a background service for creating export files for a web site in the Windows world?

I've created a shiny new ASP.Net MVC site and I have offloaded the 'save as' type of functionality to a helper program that can be run in the background on the server so that the website doesn't need to take all that load.
I created it so that it's easy to run from the command line and right now I have it running from a windows scheduler. The problem is that that has a granularity of 1 minute, which means that some unlucky users of the website will click on the link and get a 59s wait + the time to actually process the report.
I'm also slightly worried about the start up cost of my program. I'm assuming that it would be cheaper to keep the program running constantly. I am worried about the program dying and not being respawned though. With the windows scheduler option at least I don't have to worry about my program bailing so much.
How have you dealt with that?
I'd create a Windows Service and have that running instead of an exe via windows scheduler. You can then set it to auto-start. Then you can just have a timer within the service to poll for work every x seconds.
Another alternative is to use something like MSMQ. Your front end would just insert a message into the queue to represent the work to do, then have a .NET service listening to the queue (you can get it to process messages immediately they appear in the queue, or check the queue manually yourself every x seconds).
Either way, I think a Windows Service is the way to go.

How to restart my application if Windows Update forces a reboot?

At the office, when I leave for the night I very rarely log off or reboot. I simply lock my workstation and go home, leaving all my development tools exactly how I left them.
If Windows-Update rolls through and reboots my machine in the middle of the night I'm only slightly peeved because when I log back in the next morning, any MS Office application, or Visual Studio instance I had running will have already automatically restarted, opening whatever file(s)/projects/solutions I may have been working on.
My question is: How can I make my Windows Forms applications (C#) do this? Is there some way for my application to "register" that it wants to be restarted if the system automatically reboots?
I think the RegisterApplicationRestart Win32 API function might be what you're after, it's part of the Restart Manager API.
If you have Windows Vista or Windows 7, you can use the Managed Restart and Recovery API. The links on that page also point to some useful blog entries
http://channel9.msdn.com/posts/DanielMoth/Windows-Vista-Restart-amp-Recovery-APIs-from-managed-code/
A simple way is to add an entry to the following registry key :
HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\RunOnce
Just create a value containing the path of your app (optionally including command line arguments). The app will be run at the next startup, then the value will be deleted.
Step 1: Figure out a way to differentiate a windows-triggered restart from a standard one. One solution would be to try preprocessing messages. They're probably different for a windows-triggered restart...or at least they are in Vista in some cases :/
Step 2: If you detect it's a windows-triggered restart, add a scheduled, one-time task.

Scheduled .NET code execution in hosted environment?

I've got about 40 lines of .NET code in a console application that read and RSS feed and store information in a database. I need this code to execute every night for as long as the RSS feed exists (indefinitely). Currently, I just launch the console app. from my home computer.
Because I can't trust myself to remember to do this every night, I somehow need to have this code hosted. I'd like to somehow have this app. or code (it could easily be put in an ASP.NET page codebehind and triggered to execute when the page loads) run automatically without me having to run the console app. manually.
Any ideas?
EDIT: I don't want to run this code from my computer; I can't guarantee my computer will not be hibernating or connected to the Internet every night.
EDIT: Right now I'm thinking spawn a background thread using BackgroundWorker in Application_Start of the global.asax, have it download the RSS feed once a day, and Thread.Sleep() the rest of the time.
Although controversial, Jeff Atwood blogged about how they accomplished this for SO using cache expiration. Check it out at https://blog.stackoverflow.com/2008/07/easy-background-tasks-in-aspnet/.
You could use a similar method and schedule the cache to expire at the time you want the process to kick off.
You could use the Windows built-in task scheduler to start the console application.
I would suggest just using the 'at' command instead of getting this small app hosted somewhere.
C:\>at /?
The AT command schedules commands and programs to run on a computer at
a specified time and date. The Schedule service must be running to use
the AT command.
Email your webhost. Some managed hosts will set up a scheduled task for you.
They will likely have different policies regarding how to call a scheduled task (i.e. some may require it to be an aspx page and not an EXE)
Have you looked at Quartz Scheduler for .NET?
Quoth the tagline: Quartz.NET is a full-featured, open source job scheduling system that can be used from smallest apps to large scale enterprise systems.

Categories