Monday, July 5, 2010

Execute Process in Threading Pool


using System.Threading;

public class ImportData()
{
public void Import()
{
ThreadPool.QueueUserWorkItem(new WaitCallback(SendImportDataRequest), new ImportParameter()
{
PageURL = "ImportData.aspx",
CurrentUpload = enumUploadData.CustodianData,
XMLCustodianIds = sbCustodianId.ToString().Substring(1),
ImportDate = txtImportDate.Text.ToDateTimeValue(),
LoginContactId = this.LoginContactId,
LoginUserId = this.LoginUserId
});
}

public static void SendImportDataRequest(object Parameter)
{
ImportParameterDTO objParameter = (ImportParameterDTO)Parameter;
string urlEncodedUserInput = string.Format("StartImport={0}&CurrentUpload={1}&CustodianIds={2}&ImportDate={3}&LoginContactId={4}&LoginUserId={5}"
, (int)enumYesNo.Yes // 0
, objParameter.CurrentUpload // 1
, objParameter.XMLCustodianIds // 2
, objParameter.ImportDate.ToShortDateString() // 3
, objParameter.LoginContactId // 4
, objParameter.LoginUserId // 5
);

System.Net.WebRequest httpRequest = System.Net.WebRequest.Create(objParameter.PageURL);

httpRequest.Method = "POST";
httpRequest.ContentType = "application/x-www-form-urlencoded;charset=utf-8";
byte[] bytedata = System.Text.Encoding.UTF8.GetBytes(urlEncodedUserInput);
httpRequest.ContentLength = bytedata.Length;
System.IO.Stream requestStream = httpRequest.GetRequestStream();
requestStream.Write(bytedata, 0, bytedata.Length);
requestStream.Close();

try
{
System.Net.WebResponse res = httpRequest.GetResponse();
}
catch (Exception ex)
{
}
}
}

public class ImportParameter()
{
public string PageURL { get; set; }
public enumUploadData CurrentUpload { get; set; }
public string XMLCustodianIds { get; set; }
public DateTime ImportDate { get; set; }
public long LoginContactId { get; set; }
public long LoginUserId { get; set; }
}

No comments: