When you lock a thread for single-threaded operation, you should use the following method of locking:
Private ReadOnly Static New lock (Thislock) {...}
//classPrivate voidWritelog (Exception e) {FileStream fs=NewFileStream ("D:/ceshixiancheng.txt", Filemode.append); //get byte array byte[] data =Newutf8encoding (). GetBytes (E.message); //Start WritingFs. Seek (0, Seekorigin.end); Fs. Write (data,0, data. Length); varstr ="\ r \ n"; byte[] Data1 =Newutf8encoding (). GetBytes (str); Fs. Write (Data1,0, Data1. Length); //empties the buffer, closes the streamFS. Flush (); Fs. Close (); }//call when using:Writelog (NewException ("Threads:"+ Thread.CurrentThread.ManagedThreadId +"come on in! "));
View Code
The above is a test that locks the thread at the time of the second kill, prevents the inventory overflow, tests whether the thread is locked, normally, after the thread locks, the contents of the output to the TXT document are paired, one in and out. If not then the county seat is not locked. Also note that you need to give the TXT document permission, otherwise you will be prompted not to have permission.
Lock the thread while in the project and write the ID of the thread operation to the TXT file (example)