0

i am newbie. i am getting this exception last 2 days and not get solved :( it runs fine in vs 2008 but when i execute exe file it gives Exception after some time

the exception is

System.AccessViolationException: Attempted to read or write protected memory. This is often an indication that other memory is corrupt. at clienttesting.EventLoop.GetMessage(MSG& lpMsg, IntPtr hWnd, UInt32 wMsgFil terMin, UInt32 wMsgFilterMax) at clienttesting.EventLoop.Run() in D:\noman\windowsconsole\windowsconsole\Pr ogram.cs:line 196 at clienttesting.Program.Main() in D:\noman\windowsconsole\windowsconsole\Pro gram.cs:line 35

the code is

         public static void Run()
    {
        MSG msg = new MSG();

        sbyte ret;
        do
        {
            if ((ret = GetMessage(out msg, IntPtr.Zero, 0, 0)) != -1)
            {
                Thread.Sleep(1000);
                Console.WriteLine("the mesaaeg" + msg.Message.ToString());
                if (msg.Message == WM_QUIT)
                {
                    break;
                }
                if (ret == -1)
                {
                    break; //-1 indicates an error
                }
                else
                {

                    TranslateMessage(ref msg);

                    DispatchMessage(ref msg);
                }
            }
        } while (true);


    }

the Exception states

Noaman Akram
  • 2,671
  • 3
  • 18
  • 36
  • I'd have to say, if I found myself having to manually implement a windows message pump from C#, I'm doing something wrong. Why have you decided to pump messages? – Damien_The_Unbeliever Jun 20 '13 at 07:32
  • Also, your outer `if` condition means that the `if(ret==-1)` can never be true. – Damien_The_Unbeliever Jun 20 '13 at 07:37
  • i want to known user activities that what he is doing this is my task bro – Noaman Akram Jun 20 '13 at 07:38
  • before it i have done it with peekmessage but same exception occured public static void Run() { MSG msg; while (true) { while (PeekMessage(ref msg, 0, 0, 0, PeekMessageOption.PM_REMOVE)) { if (msg.message == WM_QUIT) { break; } TranslateMessage(ref msg); DispatchMessage(ref msg); } } } – Noaman Akram Jun 20 '13 at 07:39
  • You might do better to look at e.g. [Handling Messages in Console Apps](http://msdn.microsoft.com/en-us/magazine/cc163417.aspx) which leverages all of the code that's already available in .NET Framework and means you only have to write managed code. – Damien_The_Unbeliever Jun 20 '13 at 07:44

0 Answers0