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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T20:35:53+00:00 2026-06-13T20:35:53+00:00

I have 1 static class and 1 field and 2 methods within it: static

  • 0

I have 1 static class and 1 field and 2 methods within it:

 static class MyClass{

       private static HttpClient client = new HttpClient();
       private static string SendRequestToServer(int id)
       {
          Task<HttpResponseMessage> response = client.GetAsync("some string");
          responseTask.ContinueWith(x => PrintResult(x));
          return "some new value";
        }

        private static void Print(Task<HttpResponseMessage> task)
        {
            Task<string> r = task.Result.Content.ReadAsStringAsync();
            r.ContinueWith(resultTask => Console.WriteLine("result is: " + resultTask.Result));
        }
 }

The question is, if many threads start using MyClass and its methods, would it cause some problems?

  • 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-06-13T20:35:54+00:00Added an answer on June 13, 2026 at 8:35 pm

    All the resources accessed through these methods need to be thread-safe. In your case, they are not. If you look at the HttpClient documentation, it states:

    Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.

    You’re calling an instance method (client.GetAsync), which is not be guaranteed to be thread-safe, so that could potentially cause problems for you.

    To mitigate this, you could:

    • create a new (local) HttpClient on each call.
    • synchronize access to client (e.g. using a lock).

    Also, I can’t tell you if PrintResult will be thread-safe, but Console.WriteLine should be thread-safe.

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

Sidebar

Related Questions

I have a private static readonly field in my class: public class MyClass {
I have this small class looking like this: private static int field1 = -
If I have a static class with a static field such as: private static
I have a private static field in my Controller class in an MVC web
I have a static field in a non static class. public class DBtools {
I have the following class.... @XmlType @XmlRootElement(name = milestones) @XmlAccessorType(XmlAccessType.FIELD) public static class Circle
I have the following in a C# class: public static readonly SortedDictionary<string, string> Fields
I have public static class A { public static string ConnString; } [Serializable] public
I have the following class, listed below with static field of arrays. Could you
I have an class with instance functions (or methods?). From within an instance, I

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.