从Global.asax中的aspx.cs页面调用函数 [英] Call function in aspx.cs page from global.asax

查看:523
本文介绍了从Global.asax中的aspx.cs页面调用函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我得到了code inglobal.asax页:

i got code inglobal.asax page:

void Application_Start(object sender, EventArgs e) 
{
    // Code that runs on application startup
    System.Timers.Timer timScheduledTask = new System.Timers.Timer();

    // Timer interval is set in miliseconds,
    // In this case, we'll run a task every minute
    timScheduledTask.Interval = 60 * 1000;

    timScheduledTask.Enabled = true;

    // Add handler for Elapsed event
    timScheduledTask.Elapsed += new System.Timers.ElapsedEventHandler(timScheduledTask_Elapsed);
}

void timScheduledTask_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
{
    // Over here i want to call  function that i have in asp.net page 
}

在这里我的问题,在 timScheduledTask_Elapsed 的功能,我想打电话的功能,我在asp.net页面(Contacts.aspx.cs)。

my problem here that in timScheduledTask_Elapsed function, i want to call to function that i have in asp.net page (Contacts.aspx.cs).

不知道如何调用该函数?​​?

Any idea how to call this function??

推荐答案

这将需要的页面的静态方法为应用程序类无法知道当前页面实例的方式。

It would need to be a static method of the page as the application class has no way of knowing the current page instance.

一个粗略的例子可以看到下面。

A rough example can be seen below.

somepage.aspx:

public class SomePage : Page
{

    public static void DoSomething()
    {
        ...
    }

}

global.asax:

void Application_Start(object sender, EventArgs e) 
{
    ...

    // Add handler for Elapsed event
    timScheduledTask.Elapsed += new System.Timers.ElapsedEventHandler(timScheduledTask_Elapsed);


}

void timScheduledTask_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
{
    SomePage.DoSomething(); 
}

但是,这并不意味着任何东西DoSomething的()决不能依赖于特定页面的任何实例。

However, this does mean that anything in DoSomething() must not rely on anything instance specific in the page.

我会重新考虑你的方法。

I would rethink your approach.

另外请注意,定时器是一个坏主意在这里恕我直言,如果应用程序卸载你的计时器将永远不会被调用,所以你不能真正依靠他们。

Also note that timers are a bad idea here IMHO, if the application process unloads your timers will never get called so you cannot really rely on them.

这篇关于从Global.asax中的aspx.cs页面调用函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆