Why i'm getting exception [System.Runtime.InteropServices.COMException] = {"The requested resource is in use. (Exception from HRESULT: 0x800700AA)"}?

0

The exception is in the timer tick event. After the variable postsCounter is around 50 i'm getting the exception in the timer tick event it's getting to the catch there in the timer tick event.

Bot sure on what line it happen and why. Without using the try and catch and a break point i see in the program when it's running and postsCounter getting to around 50 i see a message popup window say " Message from webpage Out of memory at line: 1 "

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using mshtml;
using HtmlAgilityPack;
using System.Net;
using System.IO;

namespace WebSite_Login_And_Browsing
{
    public partial class Facebook_Post : Form
    {
        WebBrowser wb = new WebBrowser();
        int postsCounter = 0;
        StreamWriter w = new StreamWriter(@"e:\posts.txt");

        public Facebook_Post()
        {
            InitializeComponent();

            webBrowser1.ScriptErrorsSuppressed = true;
            webBrowser1.Navigate("https://www.facebook.com/");
            timer1.Enabled = true;
            label4.Text = DateTime.Now.ToString();
            w.WriteLine(label4.Text.ToString());
            w.WriteLine(Environment.NewLine);
            label5.Visible = false;
            label2.Visible = false;
        }

        private void webBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
        {
            try
            {
                if (e.Url.AbsoluteUri != webBrowser1.Url.AbsoluteUri)
                {
                    return;
                }


                wb = webBrowser1;

                foreach (HtmlElement he in wb.Document.All.GetElementsByName("xhpc_message"))
                {
                    he.SetAttribute("value", RandomString(10));
                }
                var elems = wb.Document.GetElementsByTagName("button");

                foreach (HtmlElement elem in elems)
                {
                    if (elem.InnerText == "Post")
                    {
                        elem.InvokeMember("click");
                    }
                }
                sent = true;
                postsCounter += 1;
                label2.Text = postsCounter.ToString();
                label2.Visible = true;
                if (postsCounter == 720)
                {
                    w.WriteLine(postsCounter.ToString());
                    w.WriteLine(Environment.NewLine);
                    label5.Text = DateTime.Now.ToString();
                    label5.Visible = true;
                    w.WriteLine(label5.Text.ToString());
                    w.Close();
                }
            }
            catch(Exception err)
            {
                string myerr = err.ToString();
            }
        }

        bool sent = false;
        int count = 0;

        private void timer1_Tick(object sender, EventArgs e)
        {
            try
            {
                count += 1;
                if (sent == true && count >= 60)
                {
                    count = 0;
                    sent = false;
                    webBrowser1.Navigate("https://www.facebook.com/");
                }
            }
            catch(Exception err)
            {
                string myerr = err.ToString();
            }
        }

        private StringBuilder builder;
        private static Random random = new Random((int)DateTime.Now.Ticks);
        private string RandomString(int size)
        {
            try
            {
                builder = new StringBuilder();
                char ch;
                for (int i = 0; i < size; i++)
                {
                    ch = Convert.ToChar(Convert.ToInt32(Math.Floor(26 * random.NextDouble() + 65)));
                    builder.Append(ch);
                }
            }
            catch(Exception err)
            {
                string myerr = err.ToString();
            }
            return builder.ToString();
        }
    }
}

at System.Windows.Forms.UnsafeNativeMethods.IWebBrowser2.Navigate2(Object& URL, Object& flags, Object& targetFrameName, Object& postData, Object& headers)
   at System.Windows.Forms.WebBrowser.PerformNavigate2(Object& URL, Object& flags, Object& targetFrameName, Object& postData, Object& headers)
   at System.Windows.Forms.WebBrowser.Navigate(String urlString)
   at WebSite_Login_And_Browsing.Facebook_Post.timer1_Tick(Object sender, EventArgs e)
c#
.net
winforms
asked on Stack Overflow Dec 27, 2015 by Daniel Hamutel

1 Answer

1

You never disable your timer. You have the count that increment always by one. You need to set some condition to prevent it to increment always.

This is your code:

private void timer1_Tick(object sender, EventArgs e)
    {
        try
        {
            count += 1;
            if (sent == true && count >= 60)
            {
                count = 0;
                sent = false;
                webBrowser1.Navigate("https://www.facebook.com/");
            }
        }
        catch(Exception err)
        {
            string myerr = err.ToString();
        }
    }

You need to do something like:

if(!sent) count += 1;

or

if(sent) count = 0 ;

or

if(sent) timer1.enabled = false;

Depending of what you want to do.

answered on Stack Overflow Dec 27, 2015 by ehh

User contributions licensed under CC BY-SA 3.0