日期:2014-05-18  浏览次数:20479 次

怎么写死循环程序
我想问一个问题 我想放一段死循环程序到 网站里面去 
要怎么弄才能达到以下条件
1.不影响其他使用者 
2.不需要有人去点击页面触发
3.网站运行周期内 ,这段程序都要运行
4.特定网站使用者, 可以影响,操作这段程序

------解决方案--------------------
线程中
while(true)
{
}
------解决方案--------------------
发现你的4条要求 我错了
------解决方案--------------------
看样子像是一个游戏的数据读取,显示在网页上是吗。。
------解决方案--------------------
global.asax 中写 可以实现你的要求
------解决方案--------------------
探讨
当有人访问的时候去查不行么~~~~~~

没人访问。。。你在哪里,不停的查啊~~刷啊~~` 也没意义啊~~~~~

------解决方案--------------------
依赖缓存可以用吗
------解决方案--------------------
给程序加1个接收的事件,当需要接收的时候把事件挂上,发现有信息就接收,没有信息的时候就是空闲的;
管理员操作的时候把事件卸下就可以了。
------解决方案--------------------
探讨

没理解引用:
给程序加1个接收的事件,当需要接收的时候把事件挂上,发现有信息就接收,没有信息的时候就是空闲的;
管理员操作的时候把事件卸下就可以了。

------解决方案--------------------
可以做成windows服务,使用开源框架 Topshelf
------解决方案--------------------
探讨

因为要不停的到一个socket里面查值,但是我不想再服务器上面写一个winform跑,
但是又不想写到页面里面, 然后来一个用户, 跑一个实例 ,我只想从头到尾 整个网站内存里面只跑一个实例

------解决方案--------------------
C# code

using System;
using System.Collections;
using System.Collections.Specialized;
using System.Text;
using System.Threading;
using System.Net.Sockets;
using System.Net;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters.Binary;
using System.IO;
using System.Data;
using System.Windows.Forms;
using System.Configuration;
using Microsoft.Win32;
using System.Diagnostics;
using System.Timers;

namespace WSGPSGateway
{
    public partial class TcpServer : Form
    {
        public TcpServer()
        {
            InitializeComponent();
        }

        #region 自定义字段

        public static ManualResetEvent allDone = new ManualResetEvent(false);

        /// <summary>
        /// 监听控件开启状态
        /// </summary>
        private bool State = true;

        /// <summary>
        /// 声明一个线程实例
        /// </summary>
        private Thread mythread;

        /// <summary>
        /// 服务器端Ip
        /// </summary>
        private int _port = Convert.ToInt32(ConfigurationManager.AppSettings["Port"]);

        /// <summary>
        /// 保存客户端所有回话的哈希表
        /// </summary>
        private Hashtable _transmit_tb = new Hashtable();

        /// <summary>
        /// 用于接受消息的线程
        /// </summary>
        private Thread _receviccethread = null;

        public struct TCPParameter
        {
            public string Package;
            public string IpAddress;
        }

        #endregion

        #region 监听代码块

        //窗体运行
        private void TcpServer_Load(object sender, EventArgs e)
        {
            this.WindowState = FormWindowState.Minimized;
            this.Opacity = 0; // 窗体透明度  
            Form.CheckForIllegalCrossThreadCalls = false;
            InitializeComponent();
            mythread = new Thread(Listen);
            mythread.Start();

            System.Timers.Timer atimer = new System.Timers.Timer();
            atimer.Elapsed += new System.Timers.ElapsedEventHandler(TimeEvent);
            atimer.Interval = 1000;
            atimer.Enabled = true;
            GC.KeepAlive(atimer); 
        }

        private object threadlock = new object();

        //启动监听
        private void BtnStart_Click(object sender, EventArgs e)
        {
            //多线程
        }

        //启动监听,轮询监听客户机请求并将客户端套接字存入转发表
        private void Listen()
        {
            try
            {