以下是我的代码: //////////////////////////////////////////////////////////////// DataOperation.cs 
///////////////////////////////////////////////////////////// 
using System; 
using System.Data; 
using System.Configuration; 
using System.Web; 
using System.Web.Security; 
using System.Web.UI; 
using System.Web.UI.WebControls; 
using System.Web.UI.WebControls.WebParts; 
using System.Web.UI.HtmlControls; 
using Bluedreamwh; /// <summary> 
/// DataOperation 的摘要说明 
/// </summary> 
namespace Bluedreamwh 

    public class MessageEventArgs : EventArgs 
    { 
        private int MessageText; 
        public MessageEventArgs(int messageText) 
            : base() 
        { 
            MessageText = messageText; 
        } 
        public int Message 
        { 
            get { return MessageText; } 
        } 
    }     public class MessageMonitor 
    { 
        public delegate void MessageHandler(object sender,MessageEventArgs e); 
        public event MessageHandler OnMessageArrived;         public void MessageArrived(int message) 
        { 
            int iMessage; 
            iMessage = message; 
            if (iMessage != 0) 
            { 
                MessageEventArgs messageEventArgs = new MessageEventArgs(iMessage); 
                OnMessageArrived(this, messageEventArgs); 
            } 
        }     }     public class DataOperation 
    { 
      
        public DataOperation() 
        { 
            // 
            // TODO: 在此处添加构造函数逻辑 
            // 
        } 
        
        MessageMonitor monitor = new MessageMonitor();         public bool ConnectDll() 
        { 
            bool IsCon; 
            IsCon = false; 
            DataInterFace.CallBack myCallBack = new DataInterFace.CallBack(this.CallBackFunction); 
            try 
            { 
                IsCon = DataInterFace.DI_Startup("192.168.1.100", 6001, myCallBack, 0); 
                return true; 
            } 
            catch (Exception) 
            { 
                return false; 
            }         }         private void CallBackFunction(int dwEventID, int lReserved, int lpParameter) 
        { 
            if (dwEventID == 20) 
            { 
                monitor.MessageArrived(dwEventID); 
            } 
        } 
        
        public bool SendData(byte commandID, uint dwStationCode, byte byDeviceCode, uint uObjectCount, ushort wObjectID, byte byObjectLength, uint uiValue) 
        { 
            //DataInterFace.MONITOREDOBJECT[] stMonitoredObject=new DataInterFace.MONITOREDOBJECT[uObjectCount]; 
            DataInterFace.MAPAFRAMEITEM pstMAPAFrameItem = new DataInterFace.MAPAFRAMEITEM(); 
            DataInterFace.NPAFRAMEITEM pstNPAFrameItem = new DataInterFace.NPAFRAMEITEM(); 
            DataInterFace.MONITOREDOBJECT pstMonitoredObject = new DataInterFace.MONITOREDOBJECT(); 
            ushort usPacketID = 0; 
            pstMAPAFrameItem.byCommandID = commandID; 
            pstMAPAFrameItem.byResponsionFlag = 0xFF; 
            pstNPAFrameItem.dwStationCode = dwStationCode; 
            pstNPAFrameItem.byDeviceCode = byDeviceCode; 
            if (usPacketID == 32767) 
            { 
                usPacketID = 0; 
            } 
            else 
            { 
                usPacketID += usPacketID; 
            } 
            pstNPAFrameItem.wPacketCode = usPacketID; 
            pstNPAFrameItem.byInteractiveFlag = 0x80; 
            pstNPAFrameItem.byApplicationID = 1; 
            pstMonitoredObject.wObjectID = wObjectID; 
            pstMonitoredObject.byErrorCode = 0; 
            pstMonitoredObject.byObjectLength = byObjectLength; 
            pstMonitoredObject.Content.uiValue = uiValue;             int ObjectCount = DataInterFace.DI_Send(ref pstMAPAFrameItem, ref pstNPAFrameItem, ref pstMonitoredObject, uObjectCount); 
            if (ObjectCount < 0) 
            { 
                //Response.Write("数据发送失败!"); 
                return false; 
            } 
            return true;         } 
    } 
} //////////////////////////////////////////////////////////////////////////////////////// Global.asax 
//////////////////////////////////////////////////////////////////////////////////////// 
<%@ Application Language="C#"%> 
<%@ import Namespace="System.Timers" %> 
<%@ Import Namespace="Bluedreamwh" %> 
<%@ Import Namespace="System.Threading" %> 
<script runat="server"> 
    
    public static System.Threading.Timer timer; 
    private const int interval = 1000; 
    DataOperation dataOprate = new DataOperation(); 
    MessageMonitor monitor = new MessageMonitor(); 
    System.Timers.Timer aTimer = new System.Timers.Timer();     void Application_Start(object sender, EventArgs e) 
    { 
        monitor.OnMessageArrived += new MessageMonitor.MessageHandler(this.RecvMessage); 
        if (dataOprate.ConnectDll()) 
        { 
            Application["serverState"] = "True"; 
        } 
        else 
        { 
            Application["serverState"] = "False"; 
        } 
        aTimer.Elapsed += new ElapsedEventHandler(ScheduledWorkCallback); 
        aTimer.Interval = 1000; 
        aTimer.AutoReset = true; 
        aTimer.Enabled = true; 
    }     private void RecvMessage(object sender, MessageEventArgs e) 
    { 
        switch (e.Message) 
        { 
            case 10:               //执行完此句服务器就Down掉了,在数据服务器上可看到发出的数据和设备收到的数据 
                dataOprate.SendData(2, 0x13000001, 0, 1, 0x0101, 4, 0); 
                break; 
            case 20: 
                break; 
        } 
    }     private void ScheduledWorkCallback(Object sender, ElapsedEventArgs e) 
    { 
        
        int n = 1; 
        if (Convert.ToString(Application["TimerStart"])=="True") 
        { 
            Application["globalCounter"] = Convert.ToInt32(Application["globalCounter"]) + 1; 
        } 
        if (Application["QueryStep"] != null) 
        { 
            if (Convert.ToUInt32(Application["globalCounter"]) == Convert.ToInt32(Application["QueryStep"]) * n) 
            { 
                n += n; 
                //通知代码 
                monitor.MessageArrived(10); 
                
            } 
        } 
    }     private void CallBackFun(int dwEventID, int lReserved, int lpParameter) 
    { 
        if (dwEventID == DataInterFace.EVENT_NOTIFY_RECV) 
        { 
            //通知主页面有数据到达,并进行处理 
            monitor.MessageArrived(20); 
          
        } 
    }     void Application_End(object sender, EventArgs e) 
    { 
        //  在应用程序关闭时运行的代码 
        DataInterFace.DI_Cleanup();     } 
        
    void Application_Error(object sender, EventArgs e) 
    { 
        // 在出现未处理的错误时运行的代码     }     void Session_Start(object sender, EventArgs e) 
    { 
        // 在新会话启动时运行的代码     }     void Session_End(object sender, EventArgs e) 
    { 
        // 在会话结束时运行的代码。 
        // 注意: 只有在 Web.config 文件中的 sessionstate 模式设置为 
        // InProc 时,才会引发 Session_End 事件。如果会话模式设置为 StateServer 
        // 或 SQLServer,则不会引发该事件。     } 
      
</script> 问题是每次执行完红色代码(dataOprate.SendData(2, 0x13000001, 0, 1, 0x0101, 4, 0);)部分时,服务器就挂掉了,并出现内存写错误“...内存操作不能为Writen()等等,地址为0x03a...”我个人认为可能是因为以上操作不在同一线程的原因,因为将以上所有的代码(连接DLL,以及向设备发送数据过程)放到同一个页面(defualt.aspx)中就没有任何问题。麻烦各位帮忙看看,到底出了什么问题,该如何解决。谢谢!

解决方案 »

  1.   

    没有看出来哪里用了多线程。dataOprate 也是在golbal这个所在线程里面创建的
    那么也应该这个线程去执行
      

  2.   

    asp.net的线程是由iis来管理的,它的线程机制和FORM应用程序不一样,所以在asp.net的程序中试图和IIS抢多线程操作可能会导致异常。
    通常分配不同的APPLICATION POOL来给不同的ASP.NET应用程序。看你的代码似乎是某种add-on程序,你可以查看系统日志,这种情况系统日志会记录,或建立一个win form application试试。个人意见。
      

  3.   

    谢谢各位!
    在程序中定时器与Global是不在同一个线程里的,还有RecvMessage(object sender, MessageEventArgs e)看上去是在Global中定义的,定时器中monitor.MessageArrived(10)好像也只是发了消息,但实际上在执行时我过去的线程ID告诉我RecvMessage(object sender, MessageEventArgs e)和定时器是在同一个线程,跟Global是不在同一个线程中的,所以会出现如上的错误!麻烦各位!谢谢!