Click here to Skip to main content
15,444,721 members
Home / Discussions / C#
   

C#

 
AnswerRe: Whenever code shows "Console.WriteLine..." replace with textbox Pin
Afzaal Ahmad Zeeshan22-Jun-16 7:12
mveAfzaal Ahmad Zeeshan22-Jun-16 7:12 
PraiseRe: Whenever code shows "Console.WriteLine..." replace with textbox Pin
Eddy Vluggen22-Jun-16 8:04
professionalEddy Vluggen22-Jun-16 8:04 
GeneralRe: Whenever code shows "Console.WriteLine..." replace with textbox Pin
Agewiz22-Jun-16 12:02
MemberAgewiz22-Jun-16 12:02 
AnswerRe: Whenever code shows "Console.WriteLine..." replace with textbox Pin
Afzaal Ahmad Zeeshan22-Jun-16 12:04
mveAfzaal Ahmad Zeeshan22-Jun-16 12:04 
AnswerRe: Whenever code shows "Console.WriteLine..." replace with textbox Pin
BillWoodruff22-Jun-16 19:23
mveBillWoodruff22-Jun-16 19:23 
SuggestionRe: Whenever code shows "Console.WriteLine..." replace with textbox Pin
Richard Deeming23-Jun-16 2:09
mveRichard Deeming23-Jun-16 2:09 
GeneralRe: Whenever code shows "Console.WriteLine..." replace with textbox Pin
BillWoodruff23-Jun-16 3:05
mveBillWoodruff23-Jun-16 3:05 
GeneralRe: Whenever code shows "Console.WriteLine..." replace with textbox Pin
Richard Deeming23-Jun-16 7:14
mveRichard Deeming23-Jun-16 7:14 
It's not thread-safe because you're sharing a single StringBuilder object, without synchronizing access to the shared state.

Imagine two threads calling WriteString at the same time. You could end up with:

T1: Clear
T2: Clear
T1: Append "Message from T1"
T2: Append "Message from T2"
T1: Output "Message from T1Message from T2"
T2: Output "Message from T1Message from T2"

Or:

T1: Clear
T1: Append "Message from T1"
T2: Clear
T2: Append "Message from T2"
T1: Output "Message from T2"
T2: Output "Message from T2"

Or:

T1: Clear
T1: Append "Message from T1"
T2: Clear
T1: Output ""
T2: Append "Message from T2"
T2: Output "Message from T2"

Or any other sequence of calls which could result in incorrect output.

Also, the StringBuilder class isn't thread-safe, so you could end up corrupting the internal state of the object and getting weird output or exceptions.

And that's before you consider the possibility that the JIT compiler is free to reorder operations if doing so doesn't change the outcome of the code on a single thread:
C# - The C# Memory Model in Theory and Practice[^]
C# - The C# Memory Model in Theory and Practice, Part 2[^]

If you really needed to use a static instance of the StringBuilder class, you'd either need to add a lock block around the access, reducing the performance of your code; or you'd need to use Thread Local Storage[^] to have a single instance of the class per thread.



"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer


GeneralRe: Whenever code shows "Console.WriteLine..." replace with textbox Pin
BillWoodruff26-Jun-16 4:48
mveBillWoodruff26-Jun-16 4:48 
QuestionDevExpress.XtraGrid.Views.Grid.GridView changing the data by Mouse Scrolling Pin
po_saa22-Jun-16 5:46
Memberpo_saa22-Jun-16 5:46 
AnswerREPOST Re: DevExpress.XtraGrid.Views.Grid.GridView changing the data by Mouse Scrolling Pin
Dave Kreskowiak22-Jun-16 6:01
mveDave Kreskowiak22-Jun-16 6:01 
QuestionMeaning of errors logged by FileSystemWatcher.Error Pin
srikrishnathanthri22-Jun-16 1:24
Membersrikrishnathanthri22-Jun-16 1:24 
AnswerRe: Meaning of errors logged by FileSystemWatcher.Error PinPopular
Eddy Vluggen22-Jun-16 1:50
professionalEddy Vluggen22-Jun-16 1:50 
GeneralRe: Meaning of errors logged by FileSystemWatcher.Error Pin
srikrishnathanthri22-Jun-16 19:47
Membersrikrishnathanthri22-Jun-16 19:47 
GeneralRe: Meaning of errors logged by FileSystemWatcher.Error Pin
Eddy Vluggen23-Jun-16 1:31
professionalEddy Vluggen23-Jun-16 1:31 
QuestionThe database type “GenericDatabase” does not support asynchronous operations Pin
srikrishnathanthri22-Jun-16 1:13
Membersrikrishnathanthri22-Jun-16 1:13 
AnswerRe: The database type “GenericDatabase” does not support asynchronous operations Pin
Eddy Vluggen22-Jun-16 1:48
professionalEddy Vluggen22-Jun-16 1:48 
AnswerRe: The database type “GenericDatabase” does not support asynchronous operations Pin
Dave Kreskowiak22-Jun-16 3:44
mveDave Kreskowiak22-Jun-16 3:44 
Questionneed help to send multiple sms using excel file in c# windows application Pin
trilok pandey21-Jun-16 1:39
Membertrilok pandey21-Jun-16 1:39 
AnswerRe: need help to send multiple sms using excel file in c# windows application Pin
Dave Kreskowiak21-Jun-16 3:10
mveDave Kreskowiak21-Jun-16 3:10 
GeneralRe: need help to send multiple sms using excel file in c# windows application Pin
EveryNameIsTakenEvenThisOne21-Jun-16 21:37
professionalEveryNameIsTakenEvenThisOne21-Jun-16 21:37 
GeneralRe: need help to send multiple sms using excel file in c# windows application Pin
OriginalGriff21-Jun-16 21:54
mveOriginalGriff21-Jun-16 21:54 
GeneralRe: need help to send multiple sms using excel file in c# windows application Pin
Dave Kreskowiak22-Jun-16 3:42
mveDave Kreskowiak22-Jun-16 3:42 
GeneralRe: need help to send multiple sms using excel file in c# windows application Pin
EveryNameIsTakenEvenThisOne24-Jun-16 12:10
professionalEveryNameIsTakenEvenThisOne24-Jun-16 12:10 
QuestionAccess updated values using CLR tiggers Pin
srikrishnathanthri20-Jun-16 20:15
Membersrikrishnathanthri20-Jun-16 20:15 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.