Lock the thread while in the project and write the ID of the thread operation to the TXT file (example)

Source: Internet
Author: User

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)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.