c # - programmet stopper uventet

Indlæg af Hanne Mølgaard Plasc

Problem



Hvorfor stoppede min ansøgning uventet? Jeg forsøgte alt for at løse fejlen, men jeg kan ikke finde løsningen. Dette er en Windows-applikation.


UPDATE:


hændelsesfremviser:


1


Faulting application viamura.crawlerapp.exe, version 1.0.0.0, stamp 4d9d9be8, faulting module imon.dll, version 2.70.16.0, stamp 455c9b06, debug? 0, fault address 0x0002474a.


2


Application: ViaMura.CrawlerApp.exe
Framework Version: v4.0.30319
Description: The process was terminated due to an unhandled exception.
Exception Info: System.AccessViolationException
Stack:
   at System.Net.UnsafeNclNativeMethods+OSSOCK.recv(IntPtr, Byte*, Int32, System.Net.Sockets.SocketFlags)
   at System.Net.Sockets.Socket.Receive(Byte[], Int32, Int32, System.Net.Sockets.SocketFlags, System.Net.Sockets.SocketError ByRef)
   at System.Net.Sockets.Socket.Receive(Byte[], Int32, Int32, System.Net.Sockets.SocketFlags)
   at System.Net.Sockets.NetworkStream.Read(Byte[], Int32, Int32)
   at System.Net.PooledStream.Read(Byte[], Int32, Int32)
   at System.Net.ConnectStream.ReadSingleByte()
   at System.Net.StreamChunkBytes.get\_NextByte()
   at System.Net.ChunkParse.GetChunkSize(System.Net.IReadChunkBytes, Int32 ByRef)
   at System.Net.ConnectStream.ProcessReadChunkedSize(System.Net.StreamChunkBytes)
   at System.Net.ConnectStream.ReadChunkedCallbackWorker(System.Net.NestedSingleAsyncResult, System.Net.ConnectStream)
   at System.Net.ConnectStream.ReadChunkedCallback(System.Object)
   at System.Threading.QueueUserWorkItemCallback.WaitCallback\_Context(System.Object)
   at System.Threading.ExecutionContext.Run(System.Threading.ExecutionContext, System.Threading.ContextCallback, System.Object, Boolean)
   at System.Threading.QueueUserWorkItemCallback.System.Threading.IThreadPoolWorkItem.ExecuteWorkItem()
   at System.Threading.ThreadPoolWorkQueue.Dispatch()
   at System.Threading.\_ThreadPoolWaitCallback.PerformWaitCallback()


For more information, see Help and Support Center at 


3


EventType clr20r3, P1 viamura.crawlerapp.exe, P2 1.0.0.0, P3 4d9d9759, P4 viamura.crawlerapp, P5 1.0.0.0, P6 4d9d9759, P7 e, P8 36, P9 system.exception, P10 NIL.


kode 1:


try
            {
                Application.EnableVisualStyles();
                Application.SetCompatibleTextRenderingDefault(false);
                Application.Run(new CrawlerApp());
            }
            catch (Exception e)
            {
                LogError.WriteError("Napaka: " + e.Message);
                LogError.WriteError("StackTrace: " + e.StackTrace);
            }


kode 2:


#region

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Configuration;
using System.Diagnostics;
using System.Linq;
using System.Net;
using System.Runtime.InteropServices;
using System.Windows.Forms;
using WebCrawler.Core.DataInterfaces;
using WebCrawler.Core.Entities;
using WebCrawler.Data;
using WebCrawler.Lib;
using WebCrawler.Logic;

#endregion

namespace ViaMura.CrawlerApp
{
    public delegate void WebSiteVisitedCallback(WebSiteVisitedEventArgs args);

    public partial class CrawlerApp : Form
    {
        #region Properties

        private IDaoFactory \_daoFactory;
        private IList<Agencies> agencies;
        private Agencies agency;
        private CrawlerManager manager;

        private IDaoFactory DaoFactory
        {
            get
            {
                if (\_daoFactory == null)
                {
                    \_daoFactory = new NHibernateDaoFactory(ConfigurationManager.AppSettings["NHibernateConfigPath"]);
                    return \_daoFactory;
                }
                else
                    return \_daoFactory;
            }
        }

        private string ConnectionString
        {
            get { return ConfigurationManager.ConnectionStrings["connectionString"].ConnectionString; }
        }

        #endregion

        #region Methods

        //static uint LOAD\_LIBRARY\_AS\_DATAFILE = 0x00000002;
        //static uint LOAD\_LIBRARY\_AS\_DATAFILE\_EXCLUSIVE = 0x00000040;
        //static uint LOAD\_WITH\_ALTERED\_SEARCH\_PATH = 0x00000008;

        public CrawlerApp()
        {           
            InitializeComponent();
            //LoadDLL();         
            Init();            
            LoadAgenciesData();            
        }

        /*[DllImport("kernel32.dll")]
        private static extern IntPtr LoadLibraryEx(string dllFilePath, IntPtr hFile, uint dwFlags);

        [DllImport("kernel32.dll")]
        public static extern bool FreeLibrary(IntPtr dllPointer);

        private IntPtr LoadWin32Library(string dllFilePath)
        {
            try
            {
                IntPtr moduleHandle = LoadLibraryEx(dllFilePath, IntPtr.Zero, LOAD\_WITH\_ALTERED\_SEARCH\_PATH);
                if (moduleHandle == IntPtr.Zero)
                {
                    // I'm gettin last dll error
                    int errorCode = Marshal.GetLastWin32Error();
                    throw new ApplicationException(
                        string.Format("There was an error during dll loading : {0}, error - {1}", dllFilePath, errorCode)
                        );
                }

                return moduleHandle;
            }
            catch (Exception exc)
            {
                throw new Exception(String.Format("Couldn't load library {0}{1}{2}", dllFilePath, Environment.NewLine, exc.Message), exc);
            }
        }*/

        private void Init()
        {
            manager = new CrawlerManager(DaoFactory, ConnectionString);
            manager.OnWebSiteVisited += manager\_OnWebSiteVisited;

            timerSetTime.Start();
        }

        /*private void LoadDLL()
        {
            string XulRunnerPath = @"D:PROJEKTIcrawlerWebCrawlerSuiteNCrawlerGeckoEnginexulRunner";
            //string XulRunnerPath = @"D:DevelopmentOwnViaMuraWebCrawlerSuiteNCrawlerGeckoEnginexulRunner";

            string[] files = Directory.GetFiles(XulRunnerPath, "*.dll");
            foreach (var file in files)
            {
                LoadWin32Library(file);
            }
        }*/

        private void LoadAgenciesData()
        {
            agencies = DaoFactory.GetAgenciesDao().GetAll();
            agencies = agencies.OrderBy(a => a.LoadCrawlerDate).ToList(); //TODO performance OrderBy
        }

        private void timerSetTime\_Tick(object sender, EventArgs e)
        {
            lblCurrentTime.Text = DateTime.Now.ToString();

            foreach (var tmpAgency in agencies)
            {
                if (!tmpAgency.Active || bwCrawler.IsBusy) continue;

                DateTime? crawlerTime;
                if (tmpAgency.LoadCrawlerDate != null)
                {
                    crawlerTime = ((DateTime) tmpAgency.LoadCrawlerDate).AddMinutes(tmpAgency.CrawlerDelayInMinutes);
                }
                else
                    crawlerTime = null;

                if (crawlerTime == null || DateTime.Now > crawlerTime)
                {
                    try
                    {
                        txtStatus.Text = "Start crawler: " + tmpAgency.Name + " " + DateTime.Now + " " + txtStatus.Text + Environment.NewLine + txtStatus.Text;
                    }
                    catch (Exception ex)
                    {
                        txtStatus.Text = "Error: " + ex.Message;
                    }

                    if (tmpAgency.LoadCrawlerDate != null)
                    {
                        tmpAgency.LoadCrawlerDate = ((DateTime) tmpAgency.LoadCrawlerDate).AddDays(1);
                    }
                    else
                        tmpAgency.LoadCrawlerDate = null;

                    agency = tmpAgency;
                    bwCrawler.RunWorkerAsync();
                }
            }
        }

        private void manager\_OnWebSiteVisited(object source, WebSiteVisitedEventArgs args)
        {
            try
            {
                txtStatus.Invoke(new WebSiteVisitedCallback(WebSiteVisited), new object[] { args });
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
        }

        private void WebSiteVisited(WebSiteVisitedEventArgs args)
        {
            txtStatus.Text = args.Status + " " + args.VisitedUrl + Environment.NewLine + txtStatus.Text;

            if (args.Status)
            {
                lblNumberOfMatches.Text = (Convert.ToInt32(lblNumberOfMatches.Text) + 1).ToString();
            }
            else
            {
                lblNumberOfFailures.Text = (Convert.ToInt32(lblNumberOfFailures.Text) + 1).ToString();
            }

            if (txtStatus.Text.Split(new[] { Environment.NewLine }, StringSplitOptions.None).Length > 200)
            {
                txtStatus.Text = string.Empty;
            }
        }

        private void bwCrawler\_DoWork(object sender, DoWorkEventArgs e)
        {
            manager.ExtractDataFromAgency(agency);
        }

        private void bwCrawler\_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {
            txtStatus.Text = Environment.NewLine + "End and Saved last CrawledDate: " + DateTime.Now +
                             Environment.NewLine + txtStatus.Text;

            try
            {
                IAgenciesDao aDao = DaoFactory.GetAgenciesDao();

                agency = aDao.GetById(agency.idAgencies, false);
                agency.LoadCrawlerDate = DateTime.Now;

                aDao.SaveOrUpdate(agency);
                aDao.CommitChanges();
            }
            catch (Exception ex)
            {
                txtStatus.Text = ex.Message;
            }

            LoadAgenciesData();
        }

        #endregion
    }
}

Bedste reference


Kører du NOD32 anti-virus? Hvis ja, foreslår Google, at det kunne være problemet:


http://connect.microsoft.com/VisualStudio/feedback/details/93618/net-remoting-throws-system-accessviolationexception-when-closing-client[7]


http://social.msdn.microsoft.com/Forums/en-US/netfxremoting/thread/bc72660e-d60d-4b05-b217-d563a12cb8c1/[8]


http://www.eggheadcafe.com/software/aspnet/30803254/accessviolationexception-from-ossockrecv-when-using-webrequest.aspx[9]


http://www.ureader.com/msg/1434893.aspx[10]

Andre referencer 1


Du kan forsøge at tilføje ekstra parameterløs fangst:


catch (Exception e)            
{
    LogError.WriteError("Napaka: " + e.Message);
    LogError.WriteError("StackTrace: " + e.StackTrace);
}
catch
{
    LogError.WriteError("Untyped Exception");
}


I nogle tilfælde er undtagelser ikke af .net Undtagelsestype.
Selv om dette problem er specifikt for CLR 1.1: Kaste undtagelser, der ikke er undtagelser [11]


Se også diskussion ved Catch Exception behandling


Yderligere link: 'Hvis programmet bruger biblioteker skrevet på andre sprog, kan der være en undtagelse, der ikke er afledt af klassens undtagelse. Sådanne undtagelser kan håndteres af parameter-mindre fangstopgørelse.' [13]