Firstly i developed a simple XML sender that sends xml data from a file to a web server which is coded as a loop therefore one XML file at a time.
Now i want to make it multi threaded which means that i enter lets say 3 file names of the XML files and a thread is created for each file and is sent to the webserver in parallel. Below is my current implementation, Ive tried messing around by adding worker threads here and there. Edit* So the question is how do i mulithread this type of program?
Below is my 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 the URL to send the XML File");
serverUrl = Console.ReadLine();
List<Thread> threads = new List<Thread>();
string fileName = "";
do
{
Console.WriteLine("Please enter the XML File you Wish to send");
Thread t = new Thread(new ParameterizedThreadStart(send));
t.Start(fileName = Console.ReadLine());
threads.Add(t);
}
while (fileName != "start"); //Ends if user enters an 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 from document
XmlDocument doc = new XmlDocument();
doc.Load((string)data);
string xmlcontents = doc.InnerXml;
//Send XML data to 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();
// Get response from 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();
}
}
}
}