我想问一个问题 我想放一段死循环程序到 网站里面去
要怎么弄才能达到以下条件
1.不影响其他使用者
2.不需要有人去点击页面触发
3.网站运行周期内 ,这段程序都要运行
4.特定网站使用者, 可以影响,操作这段程序
要怎么弄才能达到以下条件
1.不影响其他使用者
2.不需要有人去点击页面触发
3.网站运行周期内 ,这段程序都要运行
4.特定网站使用者, 可以影响,操作这段程序
解决方案 »
- 请推荐一款功能强大的cms(.net开源),您认为不错的管理系统
- asp.net如何隐藏音乐播放器
- 一美资外包公司面试,不会做,干脆把题拿回来了
- 生成解决方案有什么意义?
- 这语句有错吗?来看看
- 请教,基于asp与lotus notes的网络办公系统的区别?
- 使用DataAdapter.InsertCommand.CommandText,对它写sql语句以便直接更新数据源,但是不好用呀,请高手指点!!
- ASPX页面上有好几个控件,如何控制它们的Tab顺序?
- ASP.NET初学者的一个很菜的问题请大虾教下我!!
- 设计与应用ASP.NET Web Forms User Controls
- asp.net fileupload控件上传文件
- 水晶报表 导出excel 换页时 excel有一空行
while(true)
{
}
{}
但是又不想写到页面里面, 然后来一个用户, 跑一个实例 ,我只想从头到尾 整个网站内存里面只跑一个实例
2.不需要有人去点击页面触发->访问一个特定的页面
3.网站运行周期内 ,这段程序都要运行->最简单网站首页。必经之路
4.特定网站使用者, 可以影响,操作这段程序->你想多了我觉得别人的机子没死机前,你的服务器已经宕机了。
global.asax 设定了一个计时器
void Application_Start(object sender, EventArgs e)
{
// 在应用程序启动时运行的代码
System.Timers.Timer myTimer = new System.Timers.Timer(1000);
myTimer.Elapsed += new System.Timers.ElapsedEventHandler(OnTimedEvent);
myTimer.Interval = 1000;
myTimer.Enabled = true;
}
int i=2;
while(1>0)
{
i=i*i;
}
接收类
class received
{
event receivedEvent ev; //接收事件
}主程序
Main()
{
r=new received();//实例接收类
r.ev+=new receivedEvent(r_received);//绑定事件
}接收方法
void r_received()
{
接收...
}
显然你的思想是错误的,或者你对网络协议的了解太少
使用socket服务编程 用线程监听 做轮询
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
{
IPAddress _ip = IPAddress.Any;
Socket newsoc = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
newsoc.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, true);
IPEndPoint locaEp = new IPEndPoint(IPAddress.Any, _port);//建立连接
newsoc.Bind(locaEp);
newsoc.Listen(100);
allDone.Reset();
newsoc.BeginAccept(new AsyncCallback(onCall), newsoc);//继续接受其他客户端的连接
allDone.WaitOne();
}
catch (Exception ex)
{
//
}
} //监听回调
private void onCall(IAsyncResult ar)
{
allDone.Set();
Socket serverSoc = (Socket)ar.AsyncState;
Socket clent = serverSoc.EndAccept(ar);
try
{
if (serverSoc != null)
{
byte[] comes = new byte[1024];
EndPoint enp = clent.RemoteEndPoint;
serverSoc.BeginAccept(new AsyncCallback(onCall), serverSoc);
while (true)
{
int re = clent.Receive(comes, comes.Length, 0);
clent.Send(Encoding.ASCII.GetBytes("8"));
TCPParameter parm = new TCPParameter();
parm.Package = Encoding.UTF8.GetString(comes, 0, re).ToString().Trim();
parm.IpAddress = clent.RemoteEndPoint.ToString();
if (parm.Package.Length != 0)
{
Receive(parm.Package, parm.IpAddress);
}
}
}
}
catch (SocketException ex)
{
//
}
} //处理解析数据
private void Receive(string msg, string ip)
{
//
} #endregion #region 关闭与退出 //窗体关闭
private void TcpServer_FormClosing(object sender, FormClosingEventArgs e)
{
if (mythread != null)
{
mythread.Interrupt();
mythread.Abort();
GC.Collect();
}
} #endregion
}
}
http://blog.csdn.net/fengyarongaa/article/details/6631953
{
while (true)
{
Page_Loaded(sender,e);
}
}
Web程序不能控制 socket吗?
Web程序不能控制 socket吗?
protected void Application_Start(object sender, EventArgs e)
{
Thread ti = new Thread(test);
ti.Start();
} private void test()
{
int i = 1;
while (true)
{
//do some thing i++;
Thread.Sleep(100); }
}
你可以判断一下登陆用户或COKIES什么的,。然后后台把一个JS循环方法写到公有的字符串中,。
前台aspx页面引用这个字符串。,
这样的话,是你需要的效果吧,。,。
---------
没做测试,也不想做这样的测试,。
不能确定肯定能用。,
{}
你可以写个服务 或者跑个应用程序在上面~(就是 window应用程序~ 加个时间间隔 多长时间跑一次 太快了 是不太好)
并且每隔多少毫秒休眠一次以免CPU占用过高
{
记得休息一秒,
然后运行你的方法,事件。
}
换个名字就好理解了
1、写成应用程序,在服务器上跑
2、写成单个页面,在任何一台机器上(包括服务器),用windows自带的任务调度,每XX秒访问一次该页面。
CancellationTokenSource cts = new CancellationTokenSource();//提供循环取消机制
//启动任务
public void StartLoop()
{ CancellationToken ct = cts.Token; var loopTask = Task.Factory.StartNew(() => LoopBody(ct), ct);//启动一个任务 Task.WaitAll(loopTask);//等待任务完成 }
/制终止任务
public void StopLoop()
{
cts.Cancel();//取消当前任务
} //任务行为
private void LoopBody(CancellationToken ct)
{
while (true)
{
//判断是否有强制退出任务的请求
if (ct.IsCancellationRequested)
{
ct.ThrowIfCancellationRequested();
} //TODO:你的逻辑
//在这里抛出自定义事件
}
}
#endregion
using System.Collections.Generic;
using System.Text;
using System.Net;
using System.Net.Sockets;namespace Test
{
public sealed class MyTest
{
private static readonly MyTest _instance = new MyTest();
private Socket _socket;
private SocketAsyncEventArgs _asyncArgs;
private byte[] _buffer = new byte[1024];
private byte[] _tempbuffer;
/// <summary>
///
/// </summary>
private MyTest()
{ }
/// <summary>
/// 单例入口
/// </summary>
public static readonly MyTest Instance
{
get { return _instance; }
}
/// <summary>
/// 连接Socket(只调用一次)
/// </summary>
/// <param name="ip"></param>
/// <param name="port"></param>
public void Connect(string ip, int port)
{
this._asyncArgs = new SocketAsyncEventArgs();
this._asyncArgs.SetBuffer(this._buffer, 0, this._buffer.Length);
this._asyncArgs.Completed += new EventHandler<SocketAsyncEventArgs>(_asyncArgs_Completed);
this._socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
bool result = this._socket.ConnectAsync(this._asyncArgs);
if (!result)
{
bool result1= this._socket.ReceiveAsync(this._asyncArgs);
if (!result1)
{
this.ProcessReceive(this._asyncArgs);
}
}
}
/// <summary>
/// Socket事件
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void _asyncArgs_Completed(object sender, SocketAsyncEventArgs e)
{
switch (e.LastOperation)
{
case SocketAsyncOperation.Receive: this.ProcessReceive(e); break;
case SocketAsyncOperation.Connect: this.ProcessConected(e); break;
default: break;
}
}
/// <summary>
/// 处理连接事件
/// </summary>
/// <param name="e"></param>
private void ProcessConected(SocketAsyncEventArgs e)
{
if (e.SocketError == SocketError.Success)
{
bool result= this._socket.ReceiveAsync(e);
if (!result)
{
this.ProcessReceive(e);
}
}
}
/// <summary>
/// 处理接收
/// </summary>
/// <param name="e"></param>
private void ProcessReceive(SocketAsyncEventArgs e)
{
if (e.SocketError == SocketError.Success && e.BytesTransferred > 0)
{
byte[] bs = new byte[e.BytesTransferred];
Buffer.BlockCopy(this._buffer, 0, bs, 0, bs.Length);
this._tempbuffer = bs;
bool result = this._socket.ReceiveAsync(e);
if (!result)
{
this.ProcessReceive(e);
}
}
}
/// <summary>
/// 获取接收的数据
/// </summary>
/// <returns></returns>
public byte[] GetData()
{
return this._tempbuffer;
}
}
}
Socket 接收死循环,楼主自己判断该判断的条件
为何不考虑下这个? 不过要使用Task的话需要使用ContinueWith进行任务延续处理异常
{
//break 可以跳出来···
}