c# - Thread and object by reference -


i ask code thread safe? there problem attachment object. passed reference new thread mailhelper use , attachment object mixed between threads.

public static void start()  {     foreach (var message in messages)     {         //skip code         var filename = httpwebresponse.getresponseheader("filename");         var filestream = httpwebresponse.getresponsestream();         var attachment = new attachment(filestream, filename);          var thread = new thread(() =>         {             var dictionary = new listdictionary             {                 { "$url$", message.url }             };              mailhelper.sendmessage(dictionary,                 message.mail.headers.from.address,                  "emailconvertsuccess.txt",                 attachment)         });          thread.start();     } } 

no not working - it's not attachment (see darins answer) message object use iterator - have copy local instance before calling thread this:

var messagecopy = message; new thread(a =>         mailhelper.sendmessage(             new listdictionary { { "$url$", messagecopy .url } },             messagecopy.mail.headers.from.address,              "emailconvertsuccess.txt",             mailattachment)     ).start(attachment); 

if want pass parameter - darin did it's variant don't think needed)


Comments

Popular posts from this blog

delphi - How to convert bitmaps to video? -

jasper reports - Fixed header in Excel using JasperReports -

python - ('The SQL contains 0 parameter markers, but 50 parameters were supplied', 'HY000') or TypeError: 'tuple' object is not callable -