Run a Job in Background - c#

I need a block of code which run a job in background. Suppose the user click on the Submit button, then a job starts in background, in the mean time the user closes that window and run a different job, and the job should keep running.
Please provide some help in ASP.NET and VB.NET.
Thank You Very Much For Your Help

You may take a look at the ThreadPool.QueueUserWorkItem method which allows you to run some some method on a thread drawn from the thread pool. As an alternative you could use the Thread class to spawn a new thread manually if it is a long running task to avoid jeopardizing a thread from the pool which contains a limited number of threads and which are also used to service requests in ASP.NET applications.

You can do this by creating a windows service that host a wcf service, when the user click on the submit button you can send the request to the windows service and the windows service will run in the background event the user closes the window.

A BackgroundWorker may be the easiest place to start. Put your code in the DoWork event.

You might use the BackgroundWorker, depending on your context.

BackgroundWorker will run in the background and can be easily used to sync back updates to your GUI if needed.
Or use the Task Parallel Library...

Related

Service vs Thread

What should I use to make an application that will:
Ask the user for username and password
Authorize
Run an infinite loop in which it will fetch some data from the website every 10 seconds or so.
I want to be able to do some basic tasks in the meantime, or lock my screen without the thread getting killed. I don't want the service to continue running after I close the application, I just want to be sure the thread is never killed while it's running for a long time.
I also wanted to ask: Are services as easy to interact with as threads? Can I just pass a CancellationToken in it and cancel it when the user presses the stop button?
I also found the setThreadPriority, will it help in my case?
Services and Threads are totally different concepts. A Thread is a separate process that executes in parallel. A Service is a component of an app that doesn't have a UI and runs with a separate life cycle. A service does not run on its own thread, it runs on the UI thread (although it can launch a Thread if it wishes).
You use a Service if you want to do some task but not be bound to the Android Activity lifecycle. You use a Thread if you want to run in parallel. If you want both, then you use a Service that launches a Thread.
From what I'm reading (you don't want the Thread to continue after the Activity is finished), you want a Thread and not a Service.
A service can run in isolation (while your app is not necessarily running). A thread can be spun off from either your app itself, or a service.

GUI pauses until task is completed - should I use backgroundworker or is there a better solution? [duplicate]

I am creating winform to process (convert txt files to tiff) large amount of files. I put all the code behind a button (btnProcess). Is this a good idea? It works but I noticed when I go away from the winform and come back to this I see blank window until the process is complete. I heard about background worker. what is the purpose of background worker?
What you need here is multi-threading. That means that two (or more) threads of code would run in parallel. One of them would be the UI thread, the one responsible for drawing the window. In your case you are running your code in the UI thread and thus blocking the UI rendering while your code is running.
The purpose of the BackgroundWorker is to start an operation on a new thread and is what you need.
BackgroundWorker class
The BackgroundWorker class allows you
to run an operation on a separate,
dedicated thread. Time-consuming
operations like downloads and database
transactions can cause your user
interface (UI) to seem as though it
has stopped responding while they are
running. When you want a responsive UI
and you are faced with long delays
associated with such operations, the
BackgroundWorker class provides a
convenient solution.
The page I linked above contains a complete BackgroundWorker example.
It depends on your application. If this is a single purpose application that is not extremely long and the only problem is the screen doesn't paint. Which is what it sounds like to me, just throw an Application.DoEvents into the loop and be done with it.

Background thread that loads data in asp.net

I want to load data to my application domain using a thread so it will be executed at all times. How can I achieve this? I want the thread will start when iis starts and terminate when iis exits.
Note:
I want to do this so I can use disconnected architecture in my asp.net application.
Thanks in advance,
Kfir
Kfir,
The simplest way to do this is to launch a blocking background-worker during the App_Start() method on Global.asax and then to kill it using App_Stop() in Global.asax.
Have the thread sit on a WaitHandle until it's ready for more data and then signal it to wait back up. Or if you really wanted to code something ghetto, you could just have it poll on a fixed interval using a timer.

Performing periodic audits and best practice

I'm doing a windows form and would like an audit task to happen every 30 seconds. This audit is essentially checking a series of services on remote computers and reporting back into a richtextbox the status.
Current I have this running in an endless background thread and using an invoker to update the richtextbox in the main form.
Is this best practice? If I made an endless loop in my main form that would prevent any of my buttons from working, correct?
I'm just curious if every time I want to create a periodic audit check I have to create a new thread which checks the status or file or what have you?
Edit: I looked further into the Timer class and decided to go with System Timer as it proved to be better with a longer function. Thanks for pointing me in the right direction.
You should look into the Windows Forms Timer class. You don't want a loop in your main form. It's better to use the timer to fire events which can be processed asynchronously on another thread.
(I assume this is a winform application)
Invoking on the main thread is the way to go. But what about using a timer instead of an endless loop? It gives you more control. And a the timer function would execute on it's own thread.
It's good practice to let long going work execute on a background thread, so that the main thread can work with the UI.

Background Worker Thread in WebService context

Ok, so i have Call1 in a webservice that will start a bacground worker thread to start doing some processing, but would like to have another call (Call2) that will monitor the original Worker Thread via a reference?
Any suggestions on how to do this? I'd really like to stay away from a WinService to do my processing. As i need it to be more realtime.
I don't see why using a Service application should be a problem. Services run all the time and monitoring them can be done it real time.
But, if you really don't want to go that way then there are other options. It is possible to start a new thread, using the ThreadPool or by starting a new Thread manually, and that thread will run in the background of the application pool where your web service runs.
You may want to use a task scheduler library for this. Check out Quartz.net for this.
Be aware that the app domain where your web service runs may be killed at any time if IIS decides it is necessary, so there is no guarantee that the job will complete. Using a Service application will fix this.

Categories