RequestObj yourobject = new RequestObj();
Thread thread = new Thread(new ThreadStart((yourobject.method));
thread.Start();
using System;
using System.Threading;
using System.Multithreading;
void YourFunctName(object argument1)
{
//code here
}
ThreadPool.QueueUserWorkItem(YourFunctName, argument1);
Hey I see your code lets you multitask, whats the code to run several of the same httpwebrequests in multithreaded?
//list of urls you want to run in parallel
List<string> URLsList = new List<string>();
URLsList.Add("http://blabla.com");
URLsList.Add("http://blabla2.com");
URLsList.Add("http://blabla3.com");
URLsList.Add("http://blabla4.com");
URLsList.Add("http://blabla5.com");
//..... etc
Parallel.ForEach(URLsList, new ParallelOptions() { MaxDegreeOfParallelism = 2 },
(url, i, j) =>
{
//url = the url of the current thread
//j = thread number..
//==========================
//add your webclient request here...
//and do other processing etc...
});
First you need to include the namespace:
Code:using System.Multithreading;
Then you make a function that accepts an object:
Code:void YourFunctName(object argument1) { //code here }
then in the button even u want to make it start (adding threads and working)
then use:
Code:ThreadPool.QueueUserWorkItem(YourFunctName, argument1);
Do this for each task u want and they will be running simontanuously.
Cheers, wish u all the best
p.s. Threads can be a headache if you need a bit more advanced stuff.
Parallel.Foreach(listUrls,url=>{
//do what you need to do on the URL
//eg webBot.GetPage(url);
});
foreach(var url in listUrls){
var thread = new Thread ( ()=>{
//do your work here
});
thread.isBackground = true;
thread.start();
}