简体   繁体   中英

Calling static method from static method would be thread safe in C#?

I have a static class which contains methods only and none of them share static fields since i don't have any. I am calling one static method from another static method in the same static class.
Is is thread safe? it is a Asp.Net MVC 4 application.

Example code:

public static void RecordHasUserIDOnly(int WebRequestID, int UserID)
{
        _RecordWebRequest(WebRequestID, null, UserID);
}

public static void Record(int WebRequestID, int UserID, int UserLogID)
{
        _RecordWebRequest(WebRequestID, UserLogID, UserID);
}

public static void _RecordWebRequest(int WebRequestID, int? UserLogID,int? UserId )
{
        b_l_webrequest B_L_WebRequest = new b_l_webrequest();
        B_L_WebRequest.rq_id = WebRequestID;
        B_L_WebRequest.log_id = UserLogID;
        B_L_WebRequest.user_id = UserId;
        B_L_WebRequest.rq_time = DateTime.Now;

        PerRequestLevelObjects.Get_Database_Npoco().Insert<b_l_webrequest>(B_L_WebRequest);
}

You do share a static field or property:

public static void _RecordWebRequest(int WebRequestID, int? UserLogID,int? UserId )
{
   b_l_webrequest B_L_WebRequest = new b_l_webrequest();
   ...
   PerRequestLevelObjects.Get_Database_Npoco().Insert<b_l_webrequest>(B_L_WebRequest);
}

So it all depends on how thread-safe PerRequestLevelObjects is.

It has little to do with "calling static method from static method", or in which classes they are. The only issue for thread-safety is shared data.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM