I’ve been having a lot of problems with threads lately, so I wrote a lock-related tool (reflection is used in the middle of the project, if you think reflection will affect the project, don’t use it).
C# LockHelp is a LockHelp tool
The code is open source, at the following link:
Gitee.com/daolizhe/cu…
The environment
The.net framework 2.0 +
Nuget: cuyan. Lock
Using cuyan.lockgo;
Call code and instructions
// The thread calls the lock and reflects it to the display action method :ShowAction,
(LockThis = 0, current thread lock)(LockString = 1, string lock)(LockObject = 2, object lock)(LockStaticObject = 3, state object lock)
//"LockTest" is the name of the program used for reflection,
//" locktest. Test" the namespace needed for reflection,
//"Program" class name used for reflection,
//"ShowAction" is the name of the method used for reflection,
//"null" (first) class constructor needed for reflection (creation argument),
//"null" (second) the method needed for reflection (the argument at call),
new Thread(() => new LockTool(Id, threadNo).Start(lockType, "LockTest"."LockTest.Test"."Program"."ShowAction".null.null)).Start();
Copy the code
A complete case, you can also clone the code down to see the source code (this case at least uses the string lock, others will be fully updated later)
Or visit gitee.com/daolizhe/cu…
using cuyan.lockgo;
using System;
using System.Threading;
namespace LockTest.Test
{
class Program
{
static string Id1 = "1";
static string Id2 = "2";
static string Id3 = "3";
static void Main()
{
// Tests for string locks
Test(LockType.LockString);
// Wait for input (to prevent the thread from pushing out of the console after running)
Console.ReadKey();
}
/// <summary>
///test
/// </summary>
/// <param name="lockType">(LockThis = 0, current thread lock)(LockString = 1, string lock)(LockObject = 2, object lock)(LockStaticObject = 3, static object lock)</param>
static void Test(LockType lockType)
{
Console.WriteLine("-- Test the same value --");
Go(Id1, 1, lockType);
Go(Id1, 2, lockType);
Go(Id1, 3, lockType);
Thread.Sleep(5000);
Console.WriteLine("-- Test different values --");
Go(Id1, 1, lockType);
Go(Id2, 2, lockType);
Go(Id3, 3, lockType);
}
/// <summary>
///Thread lock operation method
/// </summary>
/// <param name="Id">String lock: ID</param>
/// <param name="threadNo">The current thread value</param>
/// <param name="lockType">(LockThis = 0, current thread lock)(LockString = 1, string lock)(LockObject = 2, object lock)(LockStaticObject = 3, static object lock)</param>
static void Go(string Id, int threadNo, LockType lockType)
{
// The thread calls the lock and reflects it to the display action method :ShowAction,
(LockThis = 0, current thread lock)(LockString = 1, string lock)(LockObject = 2, object lock)(LockStaticObject = 3, static object lock)
//"LockTest" is the name of the program used for reflection,
//" locktest. Test" the namespace needed for reflection,
//"Program" class name used for reflection,
//"ShowAction" is the name of the method used for reflection,
//"null" (first) class constructor needed for reflection (creation argument),
//"null" (second) the method needed for reflection (the argument at call),
new Thread(() => new LockTool(Id, threadNo).Start(lockType, "LockTest"."LockTest.Test"."Program"."ShowAction".null.null)).Start();
}
/// <summary>
///Show the actions
/// </summary>
public void ShowAction()
{
Console.WriteLine($"{DateTime.Now}: start operation");
Thread.Sleep(1000);
Console.WriteLine($"{DateTime.Now}: Operation complete..."); }}}Copy the code