Running a background task - asp.net

I currently have a very labor intensive function called "RestoreEmail" so I've put it into a thread on the webpage like this:
Task.Factory.StartNew(() => RestoreEmail());
However, I'm finding that if the person using the webpage doesn't sit and wait for it to process (they close the browser window or move to a different page) it doesn't restore the email and all is lost. I had this problem once a while back on a different project and I called an external page that then would process the function. Would this work better for my current situation? If I was to do this how exactly would I call the external page? I don't want the page to wait for the data to download because I presume it will yet again wait for the RestoreEmail to process.

Related

Run function on VB.NET, leave page, then have it email result to user?

I have an admin function that's rarely used that is built in VB.NET. It prepares a very long and complicated document and takes a very long time to process after user has hit the "Export PDF" button. What I would like to do is have ability for user to hit the Export button, leave the page and have it still going in the background and then when it's done, email the user to let them know report is ready with the URL of the file it generated.
I'm unsure if this is possible. It's my understanding that if you leave a page before a process is finished, it will interrupt/cancel the process. I can't reprogram my function in another language because it's just too complex for me to attempt to do that, so I need to stick with VB.NET.
I realize that it's not good practice to have a function that takes a long time on the server, but as I said, this is rarely used by a select amount of users and I'd like to make it more convenient for them.
Anyone know the best method/if any to get this accomplished?
Thanks!
I you can do one of these:
Create a child thread (spawn) and let that thread create and email PDF
Create a service and call that service to create and email PDF

display job-queue worker tasks synchronously in webpage

I have a asynchronous task in RQ-worker to download files from the internet, i want to monitor the progress in a webpage, i searched over internet and can not find any methods to synchronously display the progress or task states.
there's a way that is, i save the RQ-worker states in mysql, every time when my job-queue changed state,update row in mysql, in html webpage, set a timer, request the api every one or two seconds, and then refresh the status div.
I just want to know is there any other methods more elegant to solve this problem?

Background Task asp.net

I would like to create a background task which continuously inputs the location from a mobile to a database and in a website, I would like to get the same location immediately as it changes.
I am using an SQL Azure database. so pushing and polling are not an option. Also I am not sure if I can use a cache since the location continuously changes.
I think I have to create some infinite loop which carries on a task continuously. But how does this concept work?
Does this simply involve the create of a thread and a while(true) { ... } ?
I worked on a similar situation, and the approach I went for was to have an special page (/StartJob.aspx?AccessKey=xxxxxxxxxxxxx), that when hit with the right access key, would start a job cycle.
I then setup a "Cron Job" using www.setCronJob.com, to call this page at regular intervals. This service can notify you by email if it fails too.
Have a look at the timer control
http://msdn.microsoft.com/en-us/library/bb386404.aspx
sounds like something that could help out with achieving what you need :)

Preventing timers from running when something is already busy (using Qt)

In my mathematical application I am using timers to regularly perform certain actions. These actions can also be configured by my users. Now I don't want these actions to be executed if there is already another action busy.
E.g. if the user just started a complex calculation by selecting a menu entry, I don't want to execute the actions behind my timers.
Problem is that the user can execute an action via a lot of different ways (via the menu, by clicking somewhere, via popup menu, via drag-and-drop, ...). What I effectively want is to prevent the timers from going off if the application is currently not in the main event loop.
I will give a more concrete example to make it clearer:
At startup I create the timers
If a timer goes off, I execute some actions which, in practice, could access almost every bit in may application's data structure.
Now suppose the user starts a mathematical algorithm (via the menu, by clicking or by dragging elements on the screen, it doesn't matter how he started it).
The algorithm will perform lots of calculations (in the main thread). Since they are executed in the main thread, the timer events will not go off.
Now the algorithm shows a message box (could be a warning or a question).
While the message box is open, events are processed again, including my timer events, which could possibly perform incorrect calculations because there is already another algorithm running.
Reworking my application so that I move logic to a separate worker thread, or adding checks to all of my actions isn't possible at this moment. So please don't suggest to completely rework my application.
What I tried so far is the following:
Using postEvent to send an event, hoping that this event would only be executed in the main event loop. Unfortunately, also the message box's event loop seems to process posted events.
Using the QEvent::WindowBlocked and QEvent::WindowUnblocked events to see when a modal dialog was opened. In my timer-event-logic I can check whether we are between QEvent::WindowBlocked-QEvent::WindowUnblocked calls or not. Unfortunately, these events only work for modal dialogs created by Qt itself, not for other dialogs (e.g. the Windows MessageBox, or the system's printer configuration dialog). Also, this trick would not help if there would be other event loops created by sub routines.
What I actually need to solve my problem is a simple function, that:
If the application is handling an event in the main event loop returns true
If the application is handling an event in another [sub] event loop, it returns false
An alternative could be to return a level that indicates the 'depth' of the handled event.
Anyone suggestions?
You could hook into the event loop of your main thread/application using QAbstractEventDispatcher. Conditionaly filter out QTimer-events based on your application state.

How to update a page from a worker thread

I have a ASPX page which queries from a database. Once we have the dataset it is bound to a gridview and displayed on the page. All this happens in the Page_Load event.
Ofcourse this a simplistic approach. What is the best way to inform the user that data is being retrieved and when we have the data to update the page with the results in the dataset.
I want all this to happen within the same ASPX page and do not want to hop around pages to achieve this. I looked at update panels however it wasn't clear to me how this could be done with an update panel without having a control which triggers the update for the update panel. There are no controls on my page whhich initiate the database query, it occurs as the page is loaded.
If I do the query in a worker thread and then call the Update method on a UpdatePanel with the gridview as part of it, it doesn't work. Nothing happens.
Any thoughts or help? Thanks.
Well, this is a good question. Personally I have two pretty similar methods to do this:
Have a java script that will make an UpdatePanel reload with a short interval. This will create a series of post-backs to the server. During each post-back you should chek you worker thread and return immediately with the state report, usually one of error, pending, success + data
With a java script, make an asynchronous request to a web-service that will block until the data is fetched. This method brings no latency as compared to the previous one (the time between polls), but may suffer from some browsers/servers attitude to hanging open connections. This is normally solved by some interval (say, 1 minute) introduced, so that the hanging request will return with a message like need more time, in which case the java script should simply repeat the request.

Resources