Solved

Convert something to C#

Posted on 2006-07-18
16
287 Views
Last Modified: 2010-04-16
How to convert this to C#?

If DateDiff("d", Application("LastScheduledRun"), Now()) > 0 Then
    ' This is where you put the commands you want to run on the
    ' schedule set up by the above condition.
    Response.Write "Do Something!"
       
    ' Reset our "LastScheduledRun" variable to the current date
    ' and time indicating we just did it.
    Application.Lock
    Application("LastScheduledRun") = Now()
    Application.UnLock
End If
0
Comment
Question by:m-jansen
  • 9
  • 6
16 Comments
 
LVL 9

Expert Comment

by:gdupadhyay
ID: 17130340
Hi,
Can u tell me what is : Application("LastScheduledRun")
0
 

Author Comment

by:m-jansen
ID: 17130405
Take a look at this link. This is where I got the code.

http://www.asp101.com/articles/john/schedule/default.asp
0
 

Author Comment

by:m-jansen
ID: 17130414
And read under Method 1: Faking It
0
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

 
LVL 35

Expert Comment

by:mrichmon
ID: 17130936
if(((TimeSpan)(Application["LastScheduledRun"] - DateTime.Now)).Days > 0)
{
   Response.Write("Do Something");

    Application.Lock();
    Application["LastScheduledRun"] = DateTime.Now;
    Application.Unlock();
}
0
 

Author Comment

by:m-jansen
ID: 17131104
       if (((TimeSpan)(Application["LastScheduledRun"] - DateTime.Now)).Days > 0) // Error      3      Operator '-' cannot be applied to operands of type 'object' and 'System.DateTime'
        {
            Response.Write("Do Something");

            Application.Lock();
            Application["LastScheduledRun"] = DateTime.Now;
            Application.UnLock();
        }
0
 
LVL 35

Accepted Solution

by:
mrichmon earned 125 total points
ID: 17131235
In that case you need to do

if(((TimeSpan)(Convert.ToDateTime(Application["LastScheduledRun"]) - DateTime.Now)).Days > 0)
{
   Response.Write("Do Something");

    Application.Lock();
    Application["LastScheduledRun"] = DateTime.Now;
    Application.Unlock();
}
0
 

Author Comment

by:m-jansen
ID: 17131709
Label4.Text = (((TimeSpan)(Convert.ToDateTime(Application["LastScheduledRun"]) - DateTime.Now)).Days).ToString();
Always gives me the value:
-732509
Why? It should be bigger than 0 the first time I run the script on the server?
0
 
LVL 35

Assisted Solution

by:mrichmon
mrichmon earned 125 total points
ID: 17132557
Oh.

I put the two dates backwards.  You should subtract DateTime.Now from the last run date for a positive number of days.

Also make sure that you initialize Application["LastScheduledRun"] the first time.

(((TimeSpan)(DateTime.Now - Convert.ToDateTime(Application["LastScheduledRun"]))).Days)
0
 

Author Comment

by:m-jansen
ID: 17145071
>Also make sure that you initialize Application["LastScheduledRun"] the first time.
I'm not sure about this.... When to do it? In the contructor? If I use the dev server in VS2005 does this work? Or must I use IIS to test this?
0
 

Author Comment

by:m-jansen
ID: 17145096
This code
Label4.Text = (((TimeSpan)(DateTime.Now - Convert.ToDateTime(Application["LastScheduledRun"]))).Days).ToString();
gives me
732511
After I refresh the page this one should be negative?
0
 
LVL 35

Expert Comment

by:mrichmon
ID: 17147125
No - it depends on what the original value is in the  Application["LastScheduledRun"] variable.

You can set this in your global.aspx file when the application first loads.  Or you could set it on the page that uses it.  It is really up to you as to where it goes or when to do it.

But this is getting beyond the scope of the question.

The code provided is the translation of your code converted to C#.  How you decide to implement it or put it in different places is really up to you.
0
 

Author Comment

by:m-jansen
ID: 17147772
how to initialize Application["LastScheduledRun"] ?
0
 

Author Comment

by:m-jansen
ID: 17148065
Application["LastScheduledRun"] gives me the current time when hitting refresh. I have placed Label5.Text = Application["LastScheduledRun"].ToString(); in the Page_Load. I thought this should give me the last time I ran the page. Is that right?
0
 
LVL 35

Expert Comment

by:mrichmon
ID: 17148470
If you set it to that then yes.
0
 

Author Comment

by:m-jansen
ID: 17152459
I'm not so sure about this one
Application["LastScheduledRun"]
yet

How long does this one contain what I have set it to? Until the server goes down?
0
 
LVL 35

Assisted Solution

by:mrichmon
mrichmon earned 125 total points
ID: 17154511
Until the application is re-started.

This could be until the server goes down, or the IIS service is restarted, or you change the web.config file in which case the application automatically re-sets.

I think this MSDN article may help answer a lot of questions you are having:
Application State Overview
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpguide/html/cpconapplicationstate.asp
0

Featured Post

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
This Micro Tutorial hows how you can integrate  Mac OSX to a Windows Active Directory Domain. Apple has made it easy to allow users to bind their macs to a windows domain with relative ease. The following video show how to bind OSX Mavericks to …
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…

773 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question