如题.希望给点例子代码.
因为在服务器上不让运行服务

解决方案 »

  1.   

    那也要写exe啊,用asp.net哪行啊
      

  2.   

    不行。
    只有第一次访问web应用的时候,web应用对象才被实例化。因此你说的那种东西与asp.net无缘。你可以让一个远程程序不断访问你的asp.net网站,不过这有点......
      

  3.   

    可以的,用System.Threading.Timer 参考www.CommunityServer.org,这里有一个开源的社区CommunityServer2.1,里面有完整的例子。
      

  4.   

    代码很多,这是其中一个主要的文件,最好你能下载CS2.1运行起来研究,否则不太容易看得懂。//------------------------------------------------------------------------------
    // <copyright company="Telligent Systems">
    //     Copyright (c) Telligent Systems Corporation.  All rights reserved.
    // </copyright> 
    //------------------------------------------------------------------------------using System;
    using System.Threading;
    using System.Xml;
    using System.Xml.Serialization;
    using CommunityServer.Components;namespace CommunityServer.Configuration
    {
        /// <summary>
        /// Configurable IJob descritpion
        /// </summary>
        [Serializable]
        [XmlRoot("job")]
        public class Job : IDisposable
        {
            public event EventHandler PreJob;
            public event EventHandler PostJob;        private void OnPreJob()
            {
                try
                {
                    if(PreJob != null)
                        PreJob(this,EventArgs.Empty);
                }
                catch(Exception ex)
                {
                    EventLogs.Warn("PreJob listener failed", "Jobs", 807,ex, CSContext.Current.SettingsID);
                }
            }        private void OnPostJob()
            {
                try
                {
                    if(PostJob != null)
                        PostJob(this,EventArgs.Empty);
                }
                catch(Exception ex)
                {
                    EventLogs.Warn("Post listener failed", "Jobs", 807,ex, CSContext.Current.SettingsID);
                }
            }        public Job(Type ijob, XmlNode node)
            {
                _node = node;            _jobType = ijob;            XmlAttribute att =  node.Attributes["enabled"];
                if(att != null)
                   this._enabled = bool.Parse(att.Value);            att = node.Attributes["enableShutDown"];
                if(att != null)
                   this._enableShutDown = bool.Parse(att.Value);            att = node.Attributes["name"];
                if(att != null)
                    this._name = att.Value;            att = node.Attributes["seconds"];
                if(att != null)
                {
                    _seconds = Int32.Parse(att.Value);
                }            att = node.Attributes["firstRun"];
                if(att != null)
                {
                    _firstRun = Int32.Parse(att.Value);
                }            att = node.Attributes["minutes"];
                if(att != null)
                {
                    try
                    {
                        this._minutes = Int32.Parse(att.Value);
                    }
                    catch
                    {
                        this._minutes = 15;
                    }
                }            att = node.Attributes["singleThread"];
                if(att != null && !Globals.IsNullorEmpty(att.Value) && string.Compare(att.Value,"false", false) == 0)
                    _singleThread = false;
            }                #region Private Members
            
            private IJob _ijob;
            private bool _enabled = true;
            private Type _jobType;
            private string _name;
            private bool _enableShutDown = false;
            private int _minutes = 15;
            private Timer _timer = null;
            private bool disposed = false;
            private XmlNode _node = null;
            private bool _singleThread = true;
            private DateTime _lastStart;
            private DateTime _lastSucess;
            private DateTime _lastEnd;
            private bool _isRunning;
            private int _seconds = -1;
            private int _firstRun = -1;        protected int Interval
            {
                get
                {
                    if(_firstRun > 0)
                        return _firstRun * 1000;                if(_seconds > 0)
                        return _seconds * 1000;                return Minutes * 60000;
                }
            }        #endregion
            /// <summary>
            /// Creates the timer and sets the callback if it is enabled
            /// </summary>
            public void InitializeTimer()
            {
                if(_timer == null && Enabled)
                {
                    _timer = new Timer(new TimerCallback(timer_Callback), null,Interval, Interval);
                }
            }        /// <summary>
            /// Internal call back which is responsible for firing IJob.Execute()
            /// </summary>
            /// <param name="state"></param>
            private void timer_Callback(object state)
            {
    //            Guid id = (Guid)state;
    //            if(id != Jobs.Instance().CurrentID)
    //            {
    //                this.Dispose();
    //                return;
    //            }            if(!Enabled)
                    return;           _timer.Change( Timeout.Infinite, Timeout.Infinite );            _firstRun = -1;                        ExecuteJob();                        if(Enabled)
                    _timer.Change( Interval, Interval);
                else
                    this.Dispose();        }        public void ExecuteJob()
            {
                OnPreJob();            _isRunning = true;
                IJob ijob = this.CreateJobInstance();
                if(ijob != null)
                {
                    _lastStart = DateTime.Now;
                    try
                    {
                        ijob.Execute(this._node);
                      _lastEnd = _lastSucess = DateTime.Now;
                         
                    }
                    catch(Exception)
                    {
                        this._enabled = !this.EnableShutDown;
                        _lastEnd = DateTime.Now;
                    }
                }
                _isRunning = false;            OnPostJob();
            }
      

  5.   

    #region Public Properities
         
            public bool IsRunning
            {
                get{return _isRunning;}
            }        public DateTime LastStarted
            {
                get{ return _lastStart;}
            }        public DateTime LastEnd
            {
                get{ return _lastEnd;}
            }        public DateTime LastSuccess
            {
                get{ return _lastSucess;}
            }
      
            public bool SingleThreaded
            {
                get{ return _singleThread;}
            }        /// <summary>
            /// Named type of class which implements IJob
            /// </summary>
            public Type JobType
            {
                get {  return this._jobType; }
               
            }        public int Minutes
            {
                get{return _minutes;}
                set{_minutes = value;}
            }        
            /// <summary>
            /// On an exception, should this job be deactivated
            /// </summary>
            public bool EnableShutDown
            {
                get {  return this._enableShutDown; }
            }        
            /// <summary>
            /// Name of Job
            /// </summary>
            public string Name
            {
                get {  return this._name; }
            }
            
            /// <summary>
            /// Is this job enabled
            /// </summary>
            public bool Enabled
            {
                get {  return this._enabled; }
            }
            /// <summary>
            /// Attempts to create an instance of the IJob. If the type
            /// can not be created, this Job will be disabled.
            /// </summary>
            /// <returns></returns>
            public IJob CreateJobInstance()
            {
                if(Enabled)
                {
                    if(_ijob == null)
                    {
                        
                        if(_jobType != null)
                        {
                            _ijob = Activator.CreateInstance(_jobType) as IJob;
                        }
                        _enabled = (_ijob != null);                    if(!_enabled)
                            this.Dispose();
                    }
                }
                return _ijob;
            }
            #endregion        #region IDisposable Members        public void Dispose()
            {
               if(_timer!= null && !disposed)
               {
                   lock(this)
                   {
                       _timer.Dispose();
                       _timer = null;
                       disposed = true;
                   }
               }
            }        #endregion
        }
    }
      

  6.   

    写个web service
    然后再找一个装可以服务的机器,写个windows服务,定时去调用web service
      

  7.   

    没有原理,因为根本是基于web应用程序已经被访问的前提下的。上述所有东西都必须首先由某个客户访问一下网站。并且,你知道现在的大多说个人asp.net网站,特别是租空间的网站,你要假设每隔10分钟应用程序就会停止一次,也就是所谓的Timer(即使已经)启动之后每隔10分钟就会因为各种原因就会被停掉。
      

  8.   

    luckbird真是个热心的朋友啊,呵呵不过要看题目,是asp.net  并且不能写服务的,写服务程序当然轻松搞定了,也不用那么麻烦
      

  9.   

    我想想也不可能.本来是已经没希望了,还不如每天手动去执行一下.主要是看了思归大哥在一个贴中的回复让我有了点希望.我想过利用sql server的job的,不过好像不会去执行url的,只能执行sql脚本.谢谢各位!!
      

  10.   

    讲述一个static的定时程序不必那么神秘,实用是最主要的。一个timer可以这样使用:        static void doit(object state)
            {
            }然后在类型的static实例化方法中写:       System.Threading.Timer tm=new System.Threading.Timer(
                new System.Threading.TimerCallback(doit),null,90000,90000);这就可以每隔90秒钟执行一次doit方法。你可以在global.ascx或者任何类中写这些。测试的时候可以把90000改为小一些的值,并且在你的主程序中写
      System.Threading.Thread.Sleep(0);
    这样就可以看到主程序执行的时候doit被并行执行了。
      

  11.   

    如果你的服务器不然运行服务,那么按说通常也不会启动Sql Server的Agent服务。
      

  12.   

    sql server服务不起来,怎么读数据库的数据啊,作业是可以起来的,主要是能不能执行跳转url
      

  13.   

    你具体是想实现什么样的功能呀?sp1234(通往自由的桥就要修好啦)
    讲述一个static的定时程序不必那么神秘,实用是最主要的。一个timer可以这样使用:
    -----------------
    我因为对这一块也不怎么熟悉,所以只好把CS上的代码直接贴上来,让你见笑了,哈哈~
      

  14.   

    在你的网站上增加一个myTestTimer.cs文件,写:public class myTestTimer
    {
        static void doit(object state)
        {
            System.IO.File.AppendAllText("c:\\test_timer.txt","\r\n"+ DateTime.Now.ToString());
        }    static myTestTimer()
        {
            System.Threading.Timer tm = new System.Threading.Timer(
                 new System.Threading.TimerCallback(doit), null, 2000, 2000);
        }
    }然后随便找一个网页在page_load里写上一句:  myTestTimer t=new myTestTimer();就可以了。
      

  15.   

    当然你可以在网站的global.ascx中写:
    static void doit(object state)
    {
        System.IO.File.AppendAllText("c:\\test_timer.txt", "\r\n" + DateTime.Now.ToString());
    }static System.Threading.Timer tm;void Application_Start(object sender, EventArgs e)
    {
        tm = new System.Threading.Timer(new System.Threading.TimerCallback(doit), null, 2000, 2000);
    }
      

  16.   

    按说,这中关于thread、timer的问题是 .net framework 论坛应该讨论的东西。我一般这里只回答asp.net问题的 :)framework是asp.net的基础之一。
      

  17.   

    sp1234(通往自由的桥就要修好啦)
    sql  server的作业里可不可以实现跳转的功能?
    luckbird(luckbird) 
    我想不通过客户端的触发而实现自动定时执行程序,而不需要在服务器写服务.
      

  18.   

    sql  server的作业里可不可以实现跳转的功能?
    ——————————————————————————————————————
    通常如果你的网站被禁止启动你的windows服务,那么大概都会禁止sql server的agent服务。如果允许,并且系统dba管理员也帮你配置和管理,dba通常也会将sql server中一堆非常不安全的存储过程(例如xp_cmdshell)删除掉,不可能允许你随意乱搞服务器。
      

  19.   

    编写一段多线程程序,然后进行   
      protected   void   Application_Start(Object   sender,   EventArgs   e)   
      {   
            Thread.Start();   
        
      }
      

  20.   

    你想执行什么程序?是一个EXE吗?如果是EXE,你在服务器上有执行的权限吗?如果不是EXE,只是调用一段代码或DLL的话,那可以按上面说的方法试一试。最后结果告诉一下哦,我也想知道。
      

  21.   

    本人写过JAVA版的定时调度器,在不用打开任务页面的情况下可以自动执行,而且可以动态控制每一个调度器的停止、重启和删除,并且可以添加不限数的新任务,还配有页面可视化管理,主前我主要用在工作流中。但是ASP。NET还没有办法不访问页面就执行,有谁知道的,可以告诉我一下。先谢谢了!
      

  22.   

    .利用定时器timer 服务器端采用C#语法: 1.在Global.asax文件中导入命名空间 %@ Import Namespace=System.Timers % 2.Global.asax文件中的Application_Start()方法内写如下代码: System.Timers.Timer objTimer = new Timer(); objTimer.Interval = 时间; //这个时间单位毫秒,  服务器端采用C#语法: 1.在Global.asax文件中导入命名空间 <%@ Import Namespace="System.Timers" %> 2.Global.asax文件中的Application_Start()方法内写如下代码: System.Timers.Timer objTimer = new Timer(); objTimer.Interval = 时间; //这个时间单位毫秒,比如10秒,就写10000 objTimer.Enabled = true; objTimer.Elapsed += new ElapsedEventHandler(objTimer_Elapsed); 3.Global.asax文件中添加一个方法 void objTimer_Elapsed(object sender, ElapsedEventArgs e) { //这个方法内实现你想做的事情。 //例如:修改Application的某一个值等等。 } 以上3步则可以在指定时间间隔执行这个objTimer_Elapsed()方法,即达到你要得效果。(功能我已经测试过)   3. 系统自带的计划任务加入你的程序