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 1073025
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T20:56:16+00:00 2026-05-16T20:56:16+00:00

I am coding program on c# (C#, & object oriented programming newbie) I am

  • 0

I am coding program on c# (C#, & object oriented programming newbie)
I am trying to download pages in multiple threads. There are no problems when running only 1 thread. Problems starts when I run multiple.

It seems like all of the threads save the downloaded info in the same variable SockBuff, but I have no ideas what to do to solve this problem.

Suggestions ?

Here is the code

Program starts when I click button.

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        void TestaThread_1()
        {
            Random RandomNumber = new Random();
            int rand = RandomNumber.Next(99);

            HTTP cURL = new HTTP();
            cURL.CurlInit();
            String data = cURL.HTTPGet("http://google.com", "", "fails" + rand.ToString() + ".html");
            HTTP.save_file("fails" + rand.ToString()+ ".html", data);
        }

        private void button1_Click(object sender, EventArgs e)
        {
            Thread thread1 = new Thread(new ThreadStart(TestaThread_1));
            thread1.Start();

            Thread thread2 = new Thread(new ThreadStart(TestaThread_1));
            thread2.Start();

            Thread thread3 = new Thread(new ThreadStart(TestaThread_1));
            thread3.Start();

            Thread thread4 = new Thread(new ThreadStart(TestaThread_1));
            thread4.Start();

            Thread thread5 = new Thread(new ThreadStart(TestaThread_1));
            thread5.Start();
        }
    }

    class HTTP
    {
        public Easy easy;        
        public static string SockBuff;
        public string CookieFile = AppDomain.CurrentDomain.BaseDirectory + "cookie.txt";
        public string UserAgent = "Mozilla 5.0";
        public string Proxy = "";


        public void CurlInit()
        {
            Curl.GlobalInit((int)CURLinitFlag.CURL_GLOBAL_ALL);
        }


        public string HTTPGet(string URL, string Proxy, String FailaNosaukums)
        {
            easy = new Easy();
            SockBuff = "";

            try
            {
                Easy.WriteFunction wf = new Easy.WriteFunction(OnWriteData);

                easy.SetOpt(CURLoption.CURLOPT_URL, URL);
                easy.SetOpt(CURLoption.CURLOPT_TIMEOUT, "60");
                easy.SetOpt(CURLoption.CURLOPT_WRITEFUNCTION, wf);
                easy.SetOpt(CURLoption.CURLOPT_USERAGENT, UserAgent);
                easy.SetOpt(CURLoption.CURLOPT_COOKIEFILE, CookieFile);
                easy.SetOpt(CURLoption.CURLOPT_COOKIEJAR, CookieFile);
                easy.SetOpt(CURLoption.CURLOPT_FOLLOWLOCATION, true);

                if (URL.Contains("https"))
                {
                    easy.SetOpt(CURLoption.CURLOPT_SSL_VERIFYHOST, 1);
                    easy.SetOpt(CURLoption.CURLOPT_SSL_VERIFYPEER, 0);
                }

                if (Proxy != "")
                {
                    easy.SetOpt(CURLoption.CURLOPT_PROXY, Proxy);
                    easy.SetOpt(CURLoption.CURLOPT_PROXYTYPE, CURLproxyType.CURLPROXY_HTTP);
                }

                easy.Perform();
                easy.Cleanup();

            }
            catch
            {
                Console.WriteLine("Get Request Error");
            }

            return SockBuff;
        }


        public static Int32 OnWriteData(Byte[] buf, Int32 size, Int32 nmemb, Object extraData)
        {
            Random rand = new Random();
            int RandomNumber = rand.Next(1000);

            SockBuff = SockBuff + System.Text.Encoding.UTF8.GetString(buf);

            return size * nmemb;
        }

        static public void save_file(string file_name, string text_to_write)
        {
            StreamWriter MyStream = null;
            string MyString = text_to_write;

            try
            {
                MyStream = File.CreateText(file_name);
                MyStream.Write(MyString);
            }
            catch (IOException e)
            {
                Console.WriteLine(e);
            }
            catch (Exception e)
            {
                Console.WriteLine(e);
            }
            finally
            {
                if (MyStream != null)
                    MyStream.Close();
            }
        }
    }
}

I got another question.

Why can’t I save image files using save_file function correctly? Are there encoding problems in the buff ?

  • 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-16T20:56:16+00:00Added an answer on May 16, 2026 at 8:56 pm

    The problem is that your SockBuff is static, so there’s only one instance of that variable that’s shared by all instances of the HTTP class. You can probably solve the problem by removing the static.

    If you do that, you’ll also have to make your OnWriteData method an instance method (i.e. not static).

    You might also consider looking into using System.Net.WebClient and/or System.Net.HttpWebRequest classes instead of Libcurl.

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

Sidebar

Related Questions

I am coding a program where a form opens for a certain period of
I'm coding a windows program which is a WYSIWYG form generator. In a very
I am coding for a program whose detail is: 1. User enter the value
I am currently coding some chat program and now want to play music via
The program I'm coding now makes a pretty huge list of data items. Now,
I'm coding a Qt 4.8 program ( http://code.google.com/p/image-feature-detector/ ) with NetBeans 7.1 in Kubuntu
I almost done my coding for a specific program i have. I just need
I wrote my first program almost fifty years ago (yes, coding is still a
I am in Eclipse and want to debug a program. I'm coding in Java
So I am coding this client/server program. This code is from the client side.

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.