Showing posts with label netapplication. Show all posts
Showing posts with label netapplication. Show all posts

Tuesday, March 13, 2012

Thread Safety?

The following code hasbeen provided to me by a partner company for use in our asp.netapplication. Here is the class that I am questioning:

public class CurrentPageStyle
{
/// <summary>
/// The PageStyleRow for the current request.
/// </summary>
private static PageStylesDefinition.PageStyleRow currentpagestylerow;

/// <param name="ctxt">This gets the current pagestyle</param>
public CurrentPageStyle(HttpContext ctxt)
{
string keyword = ctxt.Request.QueryString["DView"];
if (keyword != null)
{
currentpagestylerow = TKConfiguration.Page_Styles_Definition.FindAll(keyword);
}
else
{
PageStylesDefinition.PageStylesRow toppsr = TKConfiguration.Page_Styles_Definition.GetFirst();
currentpagestylerow = TKConfiguration.Page_Styles_Definition.FindAll(toppsr.DefaultKeyword);
}
}

/// <summary>
/// This initializes the static member
/// </summary>
static CurrentPageStyle()
{
currentpagestylerow = null;
}

/// <summary>
/// Access to the current page style row for the request.
/// </summary>
public static PageStylesDefinition.PageStyleRow CurrentPageStyleRow
{
get
{
return currentpagestylerow;
}
set
{
currentpagestylerow = value;
}
}

Here is the usage from the global.asax file:

protected void Application_AcquireRequestState(object sender, EventArgs e)
{
CurrentPageStyle.CurrentPageStyleRow = null;
string[] pagestyle = Request.QueryString.GetValues('DView');
if (pagestyle != null)
{
if (pagestyle.Length != 0)
{
PageStylesDefinition.PageStyleRow psr = TKConfiguration.Page_Styles_Definition.FindAll(pagestyle[0]);
CurrentPageStyle.CurrentPageStyleRow = psr;
}
}

}

Myconcern is that the Application_AcquireRequestState event handler iscalled for every web request and it is setting a staticCurrentPageStyleRow which is then used later in the application. Myunderstanding is that there should only be one staticCurrentPageStyleRow per application and there can be web requests onmultiple threads so all of the threads will be sharing the sameCurrentPageStyleRow. Since there can be a differentCurrentPageStyleRow for each request, I think this is not thread safeand user 1 could end up getting user 2's CurrentPageStyleRow. I wantto make sure I am correct about this before reporting the issue to thepartner company. Am I right or am I off base?

Thanks,
Eric

In my modest opinion you should move that to an HttpModule to capture the request, and yes do not use an static class member! Why can't be an instance like the class?
Nice finding Eric! However some more people here may want to comment! Don't take my word for it

Al,

I am in the process of moving this to an HttpModule. As I said, the code was supplied by a partner company. I am in the process of adapting it to meet our needs. That was when I ran across this bit of code that does not appear to be thread safe. I am the only experienced .net developer on staff here, so I wanted someone else review the code and confirm my concerns before I reported it as a bug.

Thanks,
Eric

thread status created from asp.net

Hello,
I am sending 13000 emails alerts out using the thread from my asp.net
application. It is working smooth when I did the first and second
sending. but when I tried the third one, the server is so slow, the
thread is supposed to be abort normally each time.do you think it is
the thread I created from the first two times still alive, how could I
check thread status?
Thanks.
Ginnyyou should create a static collection to contain the threads, you can
harvest the status, or kill ones that ran too long. you might want to start
a management thread also.
-- bruce (sqlwork.com)
"ginny" <im_wang@.yahoo.com> wrote in message
news:1159292119.881690.324060@.b28g2000cwb.googlegroups.com...
> Hello,
> I am sending 13000 emails alerts out using the thread from my asp.net
> application. It is working smooth when I did the first and second
> sending. but when I tried the third one, the server is so slow, the
> thread is supposed to be abort normally each time.do you think it is
> the thread I created from the first two times still alive, how could I
> check thread status?
> Thanks.
> Ginny
>
bruce:
I am new to thread, how to create a static collection to contain the
threads?
The function in my thread is really simple, it loop 13000 times to send
13000 emails.
Thanks.
Ginny
(sqlwork.com) wrote:
> you should create a static collection to contain the threads, you can
> harvest the status, or kill ones that ran too long. you might want to star
t
> a management thread also.
> -- bruce (sqlwork.com)
> "ginny" <im_wang@.yahoo.com> wrote in message
> news:1159292119.881690.324060@.b28g2000cwb.googlegroups.com...

thread status created from asp.net

Hello,

I am sending 13000 emails alerts out using the thread from my asp.net
application. It is working smooth when I did the first and second
sending. but when I tried the third one, the server is so slow, the
thread is supposed to be abort normally each time.do you think it is
the thread I created from the first two times still alive, how could I
check thread status?

Thanks.

Ginnyyou should create a static collection to contain the threads, you can
harvest the status, or kill ones that ran too long. you might want to start
a management thread also.

-- bruce (sqlwork.com)

"ginny" <im_wang@.yahoo.comwrote in message
news:1159292119.881690.324060@.b28g2000cwb.googlegr oups.com...

Quote:

Originally Posted by

Hello,
>
I am sending 13000 emails alerts out using the thread from my asp.net
application. It is working smooth when I did the first and second
sending. but when I tried the third one, the server is so slow, the
thread is supposed to be abort normally each time.do you think it is
the thread I created from the first two times still alive, how could I
check thread status?
>
Thanks.
>
Ginny
>


bruce:

I am new to thread, how to create a static collection to contain the
threads?
The function in my thread is really simple, it loop 13000 times to send
13000 emails.

Thanks.

Ginny

(sqlwork.com) wrote:

Quote:

Originally Posted by

you should create a static collection to contain the threads, you can
harvest the status, or kill ones that ran too long. you might want to start
a management thread also.
>
-- bruce (sqlwork.com)
>
"ginny" <im_wang@.yahoo.comwrote in message
news:1159292119.881690.324060@.b28g2000cwb.googlegr oups.com...

Quote:

Originally Posted by

Hello,

I am sending 13000 emails alerts out using the thread from my asp.net
application. It is working smooth when I did the first and second
sending. but when I tried the third one, the server is so slow, the
thread is supposed to be abort normally each time.do you think it is
the thread I created from the first two times still alive, how could I
check thread status?

Thanks.

Ginny

Thread was being aborted thrown for background thread (win2003 ser

Hello,
Thread was being aborted exception is thrown by IIS for my asp.net
application on win2003 server. This error has not yet occured with the same
asp.net application on
windows 2000 professional computers that are also lot older and less
effective. And this error does not occure if this thread is run by console
application.
This asp.net gui application starts a background thread that mainly
reads data from ms access database (approximately 0,5 GB access db)
with oledb connection and then calculates something and writes to
another database.
I have tracked down that this exception is thrown by IIS in the
following code when the dataadapter should be filled with data from ms
access database or at least it seems that way after running three test trial
s.
OleDbDataAdapter dataadapter = new OleDbDataAdapter(command);
DataSet ds = new DataSet();
dataadapter.Fill(ds);
And this code should be totally OK as such since the same method is
run about thousand times before this exception is thrown and the
thread is terminated.
Has anyone run to same kind of problem ?
There are many questions in these newsgroups about direct and transfer
commands sending thread was being aborted, but this is not a case for
which that situation would apply. This is a new thread created by the
asp.net code and should run until the operation is finished, but does
not do that right now.
I read that IIS could unload application domain by throwing this
exception if it thinks that a crash,timeout,deadlock or memory has
been exceeded. But this should not do that since console application
on the same win2003 server runs just fine and on slower and older
win2000 machines the application also performs okey on IIS.
Also tried to change connection timeout parameter of IIS server, but
it did not help (from 120 to 1200)
Thank you in advance for reading and for possible answers that might
help me to figure a way out of this problematic situation.
BR,
JohannaWhat happens if you run this as a single thread?