Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6224873
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T08:44:34+00:00 2026-05-24T08:44:34+00:00

I have the following function inside my WCF service. This function is used to

  • 0

I have the following function inside my WCF service. This function is used to create html page. It creates it using WebBrowser control of windows forms.

 public Bitmap Convert(string htmlContent, int faceSheetWidth, int faceSheetHeight)
    {
        tempFileName = Guid.NewGuid().ToString().Replace("-", "");
        tempImageName = Guid.NewGuid().ToString().Replace("-", "");            
        try
        {
            width = faceSheetWidth;
            height = faceSheetHeight;

            SaveHtmlContent(htmlContent);

            //Thread m_thread = new Thread(new ThreadStart(GenerateWebSiteThumbnailImage));                
            Thread m_thread = new Thread(() =>
            {
                try
                {
                    GenerateWebSiteThumbnailImage();
                }
                catch (Exception ex)
                {
                    LoggingHelper.LogException(ex, Source.EDiscFacilityService);
                    LoggingHelper.LogException(new Exception("FaceSheet can not be created."), Source.EDiscFacilityService);
                }
            });        
            m_thread.SetApartmentState(ApartmentState.STA);
            m_thread.Start();
            m_thread.Join();
      }


 void GenerateWebSiteThumbnailImage()
    {
        WebBrowser m_WebBrowser = null;
        try
        {
            m_WebBrowser = new WebBrowser();
            m_WebBrowser.ScrollBarsEnabled = false;
            m_WebBrowser.Navigate(tempLocation);
            m_WebBrowser.DocumentCompleted += new WebBrowserDocumentCompletedEventHandler(m_WebBrowser_DocumentCompleted);
            while (m_WebBrowser.ReadyState != WebBrowserReadyState.Complete)
                Application.DoEvents();
        }
        catch {
        }
        finally
        {
            if(m_WebBrowser != null)
                m_WebBrowser.Dispose();
        }
    }


 void m_WebBrowser_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
    {
        WebBrowser m_WebBrowser = null;
        try
        {
            m_WebBrowser = (WebBrowser)sender;
            m_WebBrowser.ClientSize = new Size(this.width, this.height);
            m_WebBrowser.ScrollBarsEnabled = false;
            m_WebBrowser.Width = this.width;
            m_WebBrowser.Height = this.height;
            m_Bitmap = new Bitmap(m_WebBrowser.Bounds.Width, m_WebBrowser.Bounds.Height);
            m_WebBrowser.BringToFront();
            m_WebBrowser.DrawToBitmap(m_Bitmap, m_WebBrowser.Bounds);
            m_Bitmap = (Bitmap)m_Bitmap.GetThumbnailImage(width, height, null, IntPtr.Zero);
        }
        catch { }
    }

I have to lock calls to GenerateWebSiteThumbnailImage so that no 2 threads can access it concurrently. I have the following ways :-

  1. Define private object locker = new object() at class level and change the method as follows:-

    void GenerateWebSiteThumbnailImage()
    {            
        WebBrowser m_WebBrowser = null;
        try
        {
            lock (locker)
            {
                m_WebBrowser = new WebBrowser();
                m_WebBrowser.ScrollBarsEnabled = false;
                m_WebBrowser.Navigate(tempLocation);
                m_WebBrowser.DocumentCompleted += new WebBrowserDocumentCompletedEventHandler(m_WebBrowser_DocumentCompleted);
                while (m_WebBrowser.ReadyState != WebBrowserReadyState.Complete)
                    Application.DoEvents();
            }
        }
        catch {
        }
        finally
        {
            if(m_WebBrowser != null)
                m_WebBrowser.Dispose();
        }
    }
    
  2. Define private object locker = new object() inside GenerateWebSiteThumbnailImage function and use the same code as in point 1.

Please help me understand the difference in these two approaches and will this help me in achieving desired result.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-24T08:44:35+00:00Added an answer on May 24, 2026 at 8:44 am

    None of this will work. You need a lock object to be static, so shared between each instance of the class.

    private static object _locker = new Object();
    

    You cannot create a static variable in a function, so approach #2 is out. The first one will work only if the function resides in a singleton.

    If you want a proper lock, so that it can only be called once on the whole machine, you want a Mutex.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following markup inside a master page <html xmlns=http://www.w3.org/1999/xhtml > <head runat=server>
I have the following function: CREATE FUNCTION fGetTransactionStatusLog ( @TransactionID int ) RETURNS varchar(8000)
I have the following inside a function something(): if ($cod == 1000) { $message
If I have the following ajax call (inside a function) how do I go
I have the following in an aspx page inside a body tag: <form id=Form1
I have the following timer which periodically updates my page: var refreshId = setInterval(function()
I have the following function that is pulling data from a database. The ajax
So: I have the following function, adapted from a formula found online, which takes
Let's say I have the following function: sumAll :: [(Int,Int)] -> Int sumAll xs
I have the following Oracle function: function get_job_no return number is V_job_no number; begin

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.