Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. .NET (Core and Framework)
  4. Do Events causing overflow

Do Events causing overflow

Scheduled Pinned Locked Moved .NET (Core and Framework)
graphicscsharpc++data-structures
3 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • E Offline
    E Offline
    earlgraham
    wrote on last edited by
    #1

    I’m writing a C# DLL that executes a web browser. I have to grab a static image when the browser is done updating. So I wrote the following code.

    public string SaveImageToFile()
    {
    int done = 0;
    String file = "";
    while (done < 20)
    {
    if (((webbrowser1.IsBusy == false) && (webbrowser1.ReadyState == WebBrowserReadyState.Complete)) || (done == 19))
    {
    Bitmap docImage = new Bitmap(webbrowser1.Width, webbrowser1.Height);
    webbrowser1.DrawToBitmap(docImage, new Rectangle(webbrowser1.Location.X,
    webbrowser1.Location.Y, webbrowser1.Width, webbrowser1.Height));
    file = "C:\\Rumper\\tempA" + num + ".bmp";
    docImage.Save(file, System.Drawing.Imaging.ImageFormat.Bmp);
    ++num;
    done = 20;
    }
    else
    {
    System.Threading.Thread.Sleep(100);
    Application.DoEvents();
    ++done;
    }
    }
    return file;
    }

    However when I call SaveImageToFile from my MFC application, I get a stackoverflow. If I comment out the DoEvents. No stack overflow and no image. I need to replace DoEvents, but I’m not sure what would be a better method. Thanks

    Programmer Glenn Earl Graham Austin, TX

    S 1 Reply Last reply
    0
    • E earlgraham

      I’m writing a C# DLL that executes a web browser. I have to grab a static image when the browser is done updating. So I wrote the following code.

      public string SaveImageToFile()
      {
      int done = 0;
      String file = "";
      while (done < 20)
      {
      if (((webbrowser1.IsBusy == false) && (webbrowser1.ReadyState == WebBrowserReadyState.Complete)) || (done == 19))
      {
      Bitmap docImage = new Bitmap(webbrowser1.Width, webbrowser1.Height);
      webbrowser1.DrawToBitmap(docImage, new Rectangle(webbrowser1.Location.X,
      webbrowser1.Location.Y, webbrowser1.Width, webbrowser1.Height));
      file = "C:\\Rumper\\tempA" + num + ".bmp";
      docImage.Save(file, System.Drawing.Imaging.ImageFormat.Bmp);
      ++num;
      done = 20;
      }
      else
      {
      System.Threading.Thread.Sleep(100);
      Application.DoEvents();
      ++done;
      }
      }
      return file;
      }

      However when I call SaveImageToFile from my MFC application, I get a stackoverflow. If I comment out the DoEvents. No stack overflow and no image. I need to replace DoEvents, but I’m not sure what would be a better method. Thanks

      Programmer Glenn Earl Graham Austin, TX

      S Offline
      S Offline
      Scott Page
      wrote on last edited by
      #2

      Is this method called from within an event handler? If so, calling DoEvents may be refiring the same event and calling your code again, and again while you are still inside of the While loop. I would suggest removing the event handler at the begining to the SaveImageToFile method, then adding the handler at the end. This can also be done within the Event handler method as well. I have had way to many headaches for this problem to hit me again, and 99% of the time, it's because the event is being fired before your code completes. Hope this helps, Scott Page

      "Some people spend an entire lifetime wondering if they made a difference. The Marines don't have that problem." ( President Ronald Reagan)

      E 1 Reply Last reply
      0
      • S Scott Page

        Is this method called from within an event handler? If so, calling DoEvents may be refiring the same event and calling your code again, and again while you are still inside of the While loop. I would suggest removing the event handler at the begining to the SaveImageToFile method, then adding the handler at the end. This can also be done within the Event handler method as well. I have had way to many headaches for this problem to hit me again, and 99% of the time, it's because the event is being fired before your code completes. Hope this helps, Scott Page

        "Some people spend an entire lifetime wondering if they made a difference. The Marines don't have that problem." ( President Ronald Reagan)

        E Offline
        E Offline
        earlgraham
        wrote on last edited by
        #3

        No I’m not calling from an event but there are several timer procs in the main MFC software. I have totally abandoned the DoEvent method. It causes to much strange stuff. Now I have to keep my webbrowser control going. I need it to paint the info. Thanks for thring to help

        Programmer Glenn Earl Graham Austin, TX

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups