简体   繁体   中英

C# multithreaded windows service, private or [ThreadStatic]

I'm in the process of converting a console application I wrote from a single windows service to a windows service that supports multithreading. However, this is my first go around with multithreads.

I was wondering if I should make every class private static instead of public static and for anything that has to remain public, attach the [ThreadStatic] attribute.

Basically the code does a lot of string work from HTML Crawling and then places select data in a mysql database.

The answer to your first question is no; adapting your code to support multiple threads does not (necessarily) mean you need to change member or type accessibility.

As to your second question, use the ThreadStatic on fields (not types) if you need each thread to have its own independent static field (otherwise all threads will share the same static field).

I don't know, it feels dirty.I would rather create some kind of management class to manage all objects and pass the values between them.

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