Reputation: 47
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();
}
}
}
}
Upvotes: 0
Views: 1008
Reputation: 146
Try something like this: For every file you entered, a new thread gets created and uploads the file ("..." indicates code that does not have changed). The static variable serverUrl is not a good way, of course, but it serves it purpose here. The method used can also be non-static. You can use any way to exit the Loop creating the threads, in this case the user must enter an empty line
To wait until all threads have finished, use somethign like Thread.Join(), and store all created threads in a list
List<Thread> threads = new List<Threads>();
...
threads.add(new Thread(...));
...
foreach (Thread t in threads) t.Join();
Here the changed code
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();
string fileName = "";
do
{
Console.WriteLine("Please enter the XML File you Wish to send");
Thread t = new Thread(new ParameterizedThreadStart(send));
fileName = Console.ReadLine();
if(fileName != "")
t.Start();
}
while (fileName != ""); //Ends if user enters an 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 from 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();
}
}
}
Upvotes: 1