firstly developed simple xml sender sends xml data file web server coded loop therefore 1 xml file @ time.
now want make multi threaded means enter lets 3 file names of xml files , thread created each file , sent webserver in parallel. below current implementation, ive tried messing around adding worker threads here , there. edit* question how mulithread type of program?
below current implementation:
using system; using system.collections.generic; using system.io; using system.linq; using system.net; using system.text; using system.threading; using system.threading.tasks; using system.xml; namespace xmlsender { class program { private static string serverurl; static void main(string[] args) { console.writeline("please enter url send xml file"); serverurl = console.readline(); list<thread> threads = new list<thread>(); string filename = ""; { console.writeline("please enter xml file wish send"); thread t = new thread(new parameterizedthreadstart(send)); t.start(filename = console.readline()); threads.add(t); } while (filename != "start"); //ends if user enters empty line foreach (thread t in threads) { t.join(); } } static private void send(object data) { try { //servicepointmanager.servercertificatevalidationcallback = delegate { return true; }; httpwebrequest request = (httpwebrequest)webrequest.create(serverurl); byte[] bytes; //load xml data document xmldocument doc = new xmldocument(); doc.load((string)data); string xmlcontents = doc.innerxml; //send xml data webserver bytes = encoding.ascii.getbytes(xmlcontents); request.contenttype = "text/xml; encoding='utf-8'"; request.contentlength = bytes.length; request.method = "post"; stream requeststream = request.getrequeststream(); requeststream.write(bytes, 0, bytes.length); requeststream.close(); // response webserver httpwebresponse response; response = (httpwebresponse)request.getresponse(); stream responsestream = response.getresponsestream(); string responsestr = new streamreader(responsestream).readtoend(); console.write(responsestr + environment.newline); } catch (exception e) { console.writeline("an error occured" + environment.newline + e); console.readline(); } } } }
try this: every file entered, new thread gets created , uploads file ("..." indicates code not have changed). static variable serverurl not way, of course, serves purpose here. method used can non-static. can use way exit loop creating threads, in case user must enter empty line
to wait until threads have finished, use somethign thread.join(), , store created threads in list
list<thread> threads = new list<threads>(); ... threads.add(new thread(...)); ... foreach (thread t in threads) t.join();
here changed code
class program { private static string serverurl; static void main(string[] args) { console.writeline("please enter url send xml file"); serverurl = console.readline(); string filename = ""; { console.writeline("please enter xml file wish send"); thread t = new thread(new parameterizedthreadstart(send)); filename = console.readline(); if(filename != "") t.start(); } while (filename != ""); //ends if user enters empty line } static private void send(object url) { try { //servicepointmanager.servercertificatevalidationcallback = delegate { return true; }; httpwebrequest request = (httpwebrequest)webrequest.create(serverurl); byte[] bytes; //load xml data document xmldocument doc = new xmldocument(); doc.load((string)url); string xmlcontents = doc.innerxml; ... console.write(responsestr + environment.newline); } catch (exception e) { console.writeline("an error occured" + environment.newline + e); console.readline(); } } }
Comments
Post a Comment