Code stops to execute somewhere in these 6 Lines (Global.asax)


I have put this code in the Global.asax file where I use a timer of 10 seconds to execute the function t_Elapsed.

However when I run the code, it seems that the code "Gets stuck" somewhere in the lines before the f.Demand() or on that line because if I take all these lines away, the code that comes under will execute. The "Folder1" exists, so that should not be any problem. I really wonder why this doesn´t work and what could cause it ?

 

<%
@ Application Language="C#" %>

<script runat="server">

protected void Application_Start(object sender, EventArgs e)

{

//Code that runs on Application StartUp

System.Timers.Timer t = new System.Timers.Timer(10000);

t.Enabled = true;

t.Elapsed += new System.Timers.ElapsedEventHandler(t_Elapsed);

 

 

}

 

void t_Elapsed(object sender, System.Timers.ElapsedEventArgs e)

{

 

System.Security.Permissions.FileIOPermission f = new System.Security.Permissions.FileIOPermission(System.Security.Permissions.FileIOPermissionAccess.AllAccess, Server.MapPath("Folder1"));

f.AddPathList(System.Security.Permissions.FileIOPermissionAccess.AllAccess | System.Security.Permissions.FileIOPermissionAccess.AllAccess, Server.MapPath("Folder1"));

f.Demand();

 

//Code that comes below this Line will Not execute because it seems that something is getting stuck in the code above

}

 

</script>

protected void Application_Start(object sender, EventArgs e) { //Code that runs on Application StartUp System.Timers.Timer t = new System.Timers.Timer(10000); t.Enabled = true; t.Elapsed += new System.Timers.ElapsedEventHandler(t_Elapsed); } void t_Elapsed(object sender, System.Timers.ElapsedEventArgs e) { System.Security.Permissions.FileIOPermission f = new System.Security.Permissions.FileIOPermission(System.Security.Permissions.FileIOPermissionAccess.AllAccess, Server.MapPath("RouletteDataBase")); f.AddPathList(System.Security.Permissions.FileIOPermissionAccess.AllAccess | System.Security.Permissions.FileIOPermissionAccess.AllAccess, Server.MapPath("RouletteDataBase")); f.Demand(); //Code that runs on Application StartUp }
-1
Coding
5/4/2009 10:50:07 PM
asp.net.web-forms 93655 articles. 3 followers. Follow

4 Replies
958 Views

Similar Articles

[PageSpeed] 12

After the function executes, where you create Timer t, t goes out of scope and ceases to exist.  Try putting the timer in the Application object collection:

Application["MyTimer"] = t;


Steve Wellens

My blog
-2
SGWellens
5/4/2009 11:12:02 PM

I am not sure if I follow, do you meen that t goes out of scope after this line ---> t.Elapsed += new System.Timers.ElapsedEventHandler(t_Elapsed);

So I should write like below ? Though I am not really sure about the logic for that additional line. It seems that "MyTimer" is a keyvalue but I am not sure if I will refer to that somewhere...

To mention is if I take all the lines that has with System.Security.Permissions to do, then other code will work well with the timer. So in a way it seems that the timer do work anyway.

protected void Application_Start(object sender, EventArgs e)

{

//Code that runs on Application StartUp

System.Timers.Timer t = new System.Timers.Timer(10000); t.Enabled = true;

t.Elapsed += new System.Timers.ElapsedEventHandler(t_Elapsed);

Application["MyTimer"] = t;

 

 

}

-2
Coding
5/4/2009 11:23:17 PM

I did 2 tests to put the code like below and then using my timer in the first post. It seems that the timer I use works but something is breaking the code during the Security.Permissions lines. I beleive I do the Permissions correct.

void t_Elapsed(object sender, System.Timers.ElapsedEventArgs e)

{

System.Security.Permissions.FileIOPermission f = new System.Security.Permissions.FileIOPermission(System.Security.Permissions.FileIOPermissionAccess.AllAccess, Server.MapPath("Folder1"));

f.AddPathList(System.Security.Permissions.FileIOPermissionAccess.AllAccess | System.Security.Permissions.FileIOPermissionAccess.AllAccess, Server.MapPath("Folder1"));

f.Demand();

//The code below this line will Not execute at all here. It seems that something is "break" the code above

}

 

Then I tried to put the code that will execute in the beginning, before the Security.Permission lines and that code will now execute with 10 seconds intervals.

void t_Elapsed(object sender, System.Timers.ElapsedEventArgs e)

{

//This code will now execute with 10 seconds interval but not the below code

System.Security.Permissions.FileIOPermission f = new System.Security.Permissions.FileIOPermission(System.Security.Permissions.FileIOPermissionAccess.AllAccess, Server.MapPath("Folder1"));

f.AddPathList(System.Security.Permissions.FileIOPermissionAccess.AllAccess | System.Security.Permissions.FileIOPermissionAccess.AllAccess, Server.MapPath("Folder1"));

f.Demand();

}

2
Coding
5/4/2009 11:35:50 PM

I think I have found the problem why the code does not execute and that is for this line:

System.Security.Permissions.FileIOPermission f = new System.Security.Permissions.FileIOPermission(System.Security.Permissions.FileIOPermissionAccess.AllAccess, Server.MapPath("Folder1"));  

I have googled around and found this citat:

"The Global.asax file does not have the Page object as its default object. This means that you cannot simply call a method such as MapPath and expect it to work. Fortunately, in most cases, you can use a simple workaround. Instead of calling the MapPath method, you can call the Context.MapPath method"

It seems that Server.MapPath() cant be used. I am using the same code in a aspx.cs where I call this folder and that is not a problem. I have tried to change it to Context.Server.MapPath() and that does not work. I cant write Context.MapPath() as MapPath here isn´t a property.

 

It seems that you have to call a directory in another way when working with the Globax.asax but I dont know what to use ?

-2
Coding
5/5/2009 1:42:50 AM
Reply:

Similar Artilces:

how to change these windows form code to web form code?
hi, i'm a beginner of c#,i want to know how to change these windows form code to web form code //////////// private void buttonSign_Click( object sender, System.EventArgs e) { //get original message as byte array byte[] messagebytes = Encoding.UTF8.GetBytes( textOriginalMessage.Text); //create digest of original message using SHA1 SHA1 sha1 = new SHA1CryptoServiceProvider(); byte[] hashbytes = sha1.ComputeHash(messagebytes); //display hash bytes in hex format StringBuilder sb = new StringBuilder(); for (...

Global.asax.vb code not executing
I used the migration wizard to upgrade my projects to 2005. It did not bring in my global.asax or global.asax.vb files. Instead it created a new global.asax file without any codebehind it. So I dragged and dropped my global.asax.vb file into the project. It complained about this... Public Class Global because "Global" is a reserved word. So I changed it to ERSWeb which is the name of the project. It compiles fine but seems to completely ignore the code in the global.asax.vb file. I'm a little confused about the code in the Global.asax file... <%@ Application Codebehind="Global.asax.vb"...

Implementing .Net Form into an APS.net web form, can this be done
I am new here, but have been searching for a while, and may not have the correct lingo to find what I am looking for.  I am tasked with implementing a .exe application that was writen vb6 then converted to .net into a new website my team is developing. The idea is to put each of the 3 different forms in this .exe application of 3 different .aspx pages. I have attempted multiple things to get this into the page and even started to just rewrite it as an ASP.net web form, but even then I can't reuse any of the code since the System.Web.UI.Page doesn't inherit the sa...

PB 6.5 not executing some lines of code
Running a PB 6.5 program through the debbugger and it skips over several lines of code - not recognizing them as being executable so they won't process. Can you help? Thx. Please post to powersoft.public.powerbuilder.general This is for PB issues while being used within Jaguar/EAS Thanks A. lballman@sao.state.tx.us wrote: > > Running a PB 6.5 program through the debbugger and it skips over several > lines of code - not recognizing them as being executable so they won't > process. Can you help? Thx. -- __________________________ Adam Simmonds [Te...

change web form background property from code in vb.net
I have a web form in a vb.net project. The web form has both a design view and an HTML view. In the design view, the property box has a background property that can be set to an image file. This causes a background attribute to be added to the Body element in the HTML view. My question is, how do I modify the background property from program code? I can't find the object that has this property so I can set its value. On way to do it is to use 2 different style sheet and change the stylesheet in the pageload event depending on your condition. Look for an example in Report Starter Kit ...

How to start coding vb.net(web form and Crystal repotrs)
I use Crystal Report in Visual Studio.NET 2003..... Example at Northwind database I want a application that.....a form include a combobox that contain CustomerID.....If I choose one CustomerID and click OK button....a Report will display information about this Customer......I think that must use Paramater.....how to do that ???? (please help me step by step,I try it before but not success) please help me !!! There is sample code and tutorials on my blog entry: http://dotnetjunkies.com/WebLog/richard.dudley/archive/2004/05/03/12692.aspxIf my post is your answer, please mar...

i have 5 codes line.. how can i go to 2 code line from 5 code line ??
hello friends i use visual basic for visual web developer and i have below code.. i have 5 codes line.. how can i go to 2 code line from 5 code line ?? c = 0 second line If System.IO.File.Exists("~\resimler\") = True Then c = c +1i want to go to 2 code line from here End If Dim DestinationPath As String = "~\resimler\" & c.ToString & FileUpload1.FileNameMark as me if my question or my answer can be helpful for you :) hello friends i have solved this problem.. my code is below but i have new problem.. error codel line is FileUpload1.PostedFile.Save...

global.asax, object tag and custom web control behind code
I've got defined an object in global.asax like this: <object id="G_obj" class="MyClass" scope="Application" runat="server" /> and I want to access the global object in a custom web control file CS: public class admin : Control { protected override void Render (HtmlTextWriter writer) { writer.Write(G_obj.Text); } } But, I can't compile that CS file, because compiler don't know anything about a global object G_obj, which will be defined in global.asax in a concrete application. ??? In the other way I can't test a synt...

Web Form Fails To Load in VS.NET w/ sample code
I have a BasePage class and a StandardPage class. The BasePage inherits from the System.Web.UI.Page object, and then the StandardPage inherits from BasePage. System.Web.UI.Page | | BasePage | | StandardPage The BasePage has some Properties that access the Request object. Now I have an 'index.aspx' page with no codebehind and inherits the 'StandardPage' object through the PageDirective tags. When I try to view the 'index.aspx' in the designer, it breaks saying "accessor fails... Request has no instance' (Something like that) Anyway when I comment o...

When a button on my web form is pressed, code execution calls the page_load method first. WHY?
  Hello. I am working on a C#/ASP.NET web application. What I want to accomplish is the following: 1. When a page named "Qwan.aspx" is loaded, then set a global variable "myVar" in the Page_Load method for the code behind page Qwan.cs. 2. Display the web form Qwan.aspx on the screen. 3. A user presses a button on the web page Qwan.aspx. 4. Step 3 causes the button event handler method to be called. The problem here is that when step 3 is performed, code execution again goes to the page_load method of the Qwan.cs file. Why is this? Shouldn't code execution...

About difference in execution between Windows forms, and web forms?
One interviewer has asked me the following questionHow the execution differs from windows application to web application?Means,How windows forms will be executed?How web forms will be executed?and, what is the difference in execution between them?Ashok kumar  kak_mca wrote:One interviewer has asked me the following questionHow the execution differs from windows application to web application?And what was your answer to that question? Alister  kak_mca wrote:One interviewer has asked me the following questionHow the execution differs from windows application to web applicati...

windows forms in vb .net into web forms
we are creating a database driven website as part of our project. currently we have forms connecting to the database created in visual basic. these windows forms we need to convert into web forms which are asp .net compatible. can u give me an answer or show me a way. Hi, because of the difference in nature between web and win applications you'll need to reimplement at least the UI part. If you have a multitiered application in which the busines logic and data access are in different parts/assemblies you can reuse them. Grz, Kris.Read my blog. Handy Firefox plugins for web developers.Wor...

Converting System.Exception to System.Web.HttpCompileException in global.asax Application_Error
Hello.  I am capturing an exception in the above event handler using Server.GetLastError().  I would like to access the Results and SourceCode properties of an inner exception of type System.Web.HttpCompileException, but cannot do so, and am unable to implicitly convert a general exception to System.Web.HttpCompileException.  Any ideas?  Thank you in advance, Chris. Solved my own problem via the following code.  Feeling kind of embarrassed over the simple fix. if (lastErr.InnerException.GetType().ToString() == "System.Web.HttpCompileException") { ...

How can I coding --> auto refreshing Crystal report via vb.net-->web form
please help me I must show the report(real time) that data come from database --> (data are from another program) User open my webpage for looking the report-->update data How can I set report automatic. --> automatic refresh<----- thanks so much If your report is using the CrystalReportViewer, and you are not exporting the report to PDF or Excel, I would simply set a META Refresh in the header. Here is a reference: http://www.htmlhelp.com/reference/html40/head/meta.html This will cause the page to reload from the server at a specified interval....