473,425 Members | 1,829 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,425 software developers and data experts.

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.Request.UserHostAddress + "1";

// Create a lock object to prevent 1 user from running task 1 //
multiple times. string threadLockKey = "thread1" +
this.Context.Request.UserHostAddress;
object threadLock = this.Context.Cache[threadLockKey];
if (threadLock == null)
{
threadLock = newobject();
this.Context.Cache[threadLockKey] = threadLock;
}

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

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

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

Feb 6 '07 #1
1 2452
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.Request.UserHostAddress + "1";

// Create a lock object to prevent 1 user from running task 1 //
multiple times. string threadLockKey = "thread1" +
this.Context.Request.UserHostAddress;
object threadLock = this.Context.Cache[threadLockKey];
if (threadLock == null)
{
threadLock = newobject();
this.Context.Cache[threadLockKey] = threadLock;
}

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

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

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

Feb 6 '07 #2

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

Similar topics

1
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...
3
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...
2
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...
2
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...
4
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...
3
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...
3
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...
0
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...
32
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. ...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
1
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...
0
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...
0
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...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

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.