473,785 Members | 2,435 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

How to run only 1 single long running task in separate thread in ASP.NET

How do you run a long running task in ASP.NET in thread and
avoid multiple instances of this task from running?

I saw a solution somewhere but do you have a better one?

[WebMethod]
publicvoid StartTask1()
{
string processKey = this.Context.Re quest.UserHostA ddress + "1";

// Create a lock object to prevent 1 user from running task 1 //
multiple times. string threadLockKey = "thread1" +
this.Context.Re quest.UserHostA ddress;
object threadLock = this.Context.Ca che[threadLockKey];
if (threadLock == null)
{
threadLock = newobject();
this.Context.Ca che[threadLockKey] = threadLock;
}

// Only allow 1 running task per user. if (!
Monitor.TryEnte r(threadLock, 0))
return;

// Simulate a time-consuming task. for (int i = 1; i <= 100; i++)
{
// Update the progress for this task.
this.Context.Ca che[processKey] = i;
Thread.Sleep(10 0);
}

// The task is done. Release the lock. Monitor.Exit(th readLock);
}

Feb 6 '07 #1
1 2467
Here is one of many different approaches to this:
http://www.eggheadcafe.com/articles/20051223.asp
Peter
--
Site: http://www.eggheadcafe.com
UnBlog: http://petesbloggerama.blogspot.com
Short urls & more: http://ittyurl.net


"Anonieko" wrote:
How do you run a long running task in ASP.NET in thread and
avoid multiple instances of this task from running?

I saw a solution somewhere but do you have a better one?

[WebMethod]
publicvoid StartTask1()
{
string processKey = this.Context.Re quest.UserHostA ddress + "1";

// Create a lock object to prevent 1 user from running task 1 //
multiple times. string threadLockKey = "thread1" +
this.Context.Re quest.UserHostA ddress;
object threadLock = this.Context.Ca che[threadLockKey];
if (threadLock == null)
{
threadLock = newobject();
this.Context.Ca che[threadLockKey] = threadLock;
}

// Only allow 1 running task per user. if (!
Monitor.TryEnte r(threadLock, 0))
return;

// Simulate a time-consuming task. for (int i = 1; i <= 100; i++)
{
// Update the progress for this task.
this.Context.Ca che[processKey] = i;
Thread.Sleep(10 0);
}

// The task is done. Release the lock. Monitor.Exit(th readLock);
}

Feb 6 '07 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
3179
by: Craig Ringer | last post by:
Hi folks I'm a bit of a newbie here, though I've tried to appropriately research this issue before posting. I've found a lot of questions, a few answers that don't really answer quite what I'm looking for, but nothing that really solves or explains all this. I'll admit to being stumped, hence my question here. I'm also trying to make this post as clear and detailed as possible. Unfortunately, that means it's come out like a book. I...
3
3186
by: Craig C | last post by:
I have a Simple Windows Form which I placed a button and when the button is pressed it proceses data into a database and I update the task bar for each record. The task bar would show processing 1 of 200 and so on... The problem is if I click anyware on my form during this process it locks and I cant do anything with that form.( my process keeps running but the form stops updateing) Now if I dont touch the form. its happy and my task...
2
2443
by: Jeffrey Palermo, MCAD.Net | last post by:
I have some classes that control file processing in c#. The files come from a mainframe and may take some time for each to process. I use the Threadpool to process multiple files at once. Each instance of the file processor is spawned in its own AppDomain to isolate it (I'm using a separate AppDomain for a valid reason, so this thread isn't to discuss that). My process is a Windows service, but I can duplicate this problem as a regular...
2
2353
by: Robert Wilson | last post by:
Hi, I am running a separate thread in order to be able to poll devices on a MODBUS RTU network. I create a thread and look at the ThreadObj.IsAlive flag, and this work ok. I then might have to come back later to the thread an re run the task contained within it. When I come to re-run the thread, when I call to ThreadObj.Start it throws an exception. Ordinarily in C++ I would delete the ThreadObj and then re-create one with the oThread...
4
1785
by: Bill Thorne | last post by:
We have a COM object that has been wrappered for use in .NET and which can make calls which can take a while to execute. These are mainframe integration calls that might perform a lot of data entry and gathering, returning the results to the ASP.NET caller. I have tried an AsyncPage class (implements IHttpAsyncHandler and uses custom thread pool class from DevelopMentor.ThreadPool -http://staff.develop.com/woodring); I have tried the...
3
4352
by: lai_waiman | last post by:
Dear All, I have problems on doing some long lasting job in a web services. Let me first provide some background information first. I have a ASP.NET web page, which will call another Web Services to preform a task. Since that Web Service Task last for a quiet long time (eg. 1 hr). If I return the result after the long task, I am sure that the client browser will be timeout. So I decided to use threading inside the Web Service. My...
3
2111
by: =?Utf-8?B?cHJhZGVlcF9UUA==?= | last post by:
Hello All, I am developing a file uploading sever. I am planning to create this as a multithreaded application in which every file upload request will be done through a separate thread from the thread pool. While going through some articles on the internet, I came to know that it is a bad practice to allocate thread from thread pool for long running application. I would like to know what other alternatives are available in the...
0
1385
by: sirmoreno | last post by:
Hi, In my web site I have some long tasks that I want to call without delaying the page rendering - without making the thread that handels the page request wait for the long task to end. I found two solutions but I can't figure out which one is better? 1. Asynchronous OneWay WebService: - Declare a OneWay method for the long task inside an asmx file in my website and WebReference it.
32
1711
by: John Wright | last post by:
I have a long process I run that needs to be complete before the user can continue. I can change the cursor to an hourglass, but I want to update the Status Strip on the bottom during the process. I set the statusstrip label to "Downloading...", but it will not show up on the form. I need to display this message before it starts the process. I can put a thread.sleep for 1 second after I do the initial process, but that seems sloppy to me. ...
0
9480
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10147
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10091
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9950
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7499
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6739
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5381
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4050
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3645
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.