L
lsavidge
Guest
I have a Windows form application. It is called via the task scheduler in Windows. If I pass it a command line parameter it runs without the form loading. My problem is that at the end I need to close the form. The trouble is I am calling a web service and posting data to it in a async task. During the send, the task continues and the application finishes before the async web service call responds. So I have this constructor for the form load only when a specific command line parameter is passed in.
public frmForwarder(AppParams appParams)
{
InitializeComponent();
this.FormClosing += frmForwarder_FormClosing;
LoadSettings();
logger.WriteLog(Resources.sLogLineBreak);
logger.WriteLog("Headless mode");
RunHeadless();
logger.WriteLog("End of run");
logger.WriteLog(Resources.sLogLineBreak);
if (bHidden)
{
SaveSettings();
//Environment.Exit(0);
}
}
If I uncomment the Environment.Exit(0); the application quits before RunHeadless has completed. The code for RunHeadless is and the RunUpdateFunction here:
private async void RunHeadless()
{
Hide();
bHidden = true;
iReturnCode = OpenDB();
if (iReturnCode == 0)
{
await RunUpdateFunction("customer");
}
}
private async Task RunUpdateFunction(string psEndFunction)
{
logger.WriteLog("Running update function: " + psEndFunction);
switch (psEndFunction.ToLower())
{
case "customer":
CreatePMCustomer();
iReturnCode = await SendCustomer(new HttpMethod("POST"), Resources.sEndpointURL + Resources.sEndpointCustomerUpdate, pmCustomer);
logger.WriteLog("Return code from customer update: " + iReturnCode.ToString());
break;
}
}
You can ignore CreatePMCustomer as that just creates the XML objects that I need to send. The SendCustomer function is here:
public async Task<int> SendCustomer(HttpMethod method, string requestUri, TMCustomerXML payload = null)
{
HttpContent content = null;
string sTMResponse = "";
int statusNumber = 200; // Assume Ok
XmlSerializer xmlSerializer = new XmlSerializer(payload.GetType());
importResult xmlImportResult = new importResult();
XmlSerializer xmlResponse = new XmlSerializer(xmlImportResult.GetType());
StringWriter textWriter = new Utf8StringWriter();
string sResponseText = "";
// Serialize the payload if one is present
if (payload != null)
{
xmlSerializer.Serialize(textWriter, payload);
string payloadString = textWriter.ToString();
content = new StringContent(payloadString, Encoding.UTF8, "application/xml");
}
using (HttpClientHandler httpClientHandler = new HttpClientHandler { Credentials = new NetworkCredential(sTMLogon, sTMPassword) })
using (HttpClient httpClient = new HttpClient(httpClientHandler))
{
HttpRequestMessage request = new HttpRequestMessage(method, requestUri)
{
Content = content
};
try
{
HttpResponseMessage response = await httpClient.SendAsync(request);
sTMResponse = await response.Content.ReadAsStringAsync();
xmlImportResult = (importResult)xmlResponse.Deserialize(new StringReader(sTMResponse));
statusNumber = (int)response.StatusCode;
sResponseText = "HTTP Response: " + response.ReasonPhrase + " - Response status: " + xmlImportResult.status;
lblStatus.Text = sResponseText;
logger.WriteLog(sResponseText);
switch (statusNumber)
{
case 200:
break;
case 404:
break;
}
}
catch (Exception ex)
{
lblStatus.Text = ex.Message;
}
}
return statusNumber;
}
If I comment out the Environment.Exit(0) the form will load and wait and things work. If I uncomment it it quits before the httpClient.SendAsync(request) has had time to get the response.
How can I get the code to wait for the response and not do it asynchronously so the response comes back and the application can close normally after the response has come back?
Continue reading...
public frmForwarder(AppParams appParams)
{
InitializeComponent();
this.FormClosing += frmForwarder_FormClosing;
LoadSettings();
logger.WriteLog(Resources.sLogLineBreak);
logger.WriteLog("Headless mode");
RunHeadless();
logger.WriteLog("End of run");
logger.WriteLog(Resources.sLogLineBreak);
if (bHidden)
{
SaveSettings();
//Environment.Exit(0);
}
}
If I uncomment the Environment.Exit(0); the application quits before RunHeadless has completed. The code for RunHeadless is and the RunUpdateFunction here:
private async void RunHeadless()
{
Hide();
bHidden = true;
iReturnCode = OpenDB();
if (iReturnCode == 0)
{
await RunUpdateFunction("customer");
}
}
private async Task RunUpdateFunction(string psEndFunction)
{
logger.WriteLog("Running update function: " + psEndFunction);
switch (psEndFunction.ToLower())
{
case "customer":
CreatePMCustomer();
iReturnCode = await SendCustomer(new HttpMethod("POST"), Resources.sEndpointURL + Resources.sEndpointCustomerUpdate, pmCustomer);
logger.WriteLog("Return code from customer update: " + iReturnCode.ToString());
break;
}
}
You can ignore CreatePMCustomer as that just creates the XML objects that I need to send. The SendCustomer function is here:
public async Task<int> SendCustomer(HttpMethod method, string requestUri, TMCustomerXML payload = null)
{
HttpContent content = null;
string sTMResponse = "";
int statusNumber = 200; // Assume Ok
XmlSerializer xmlSerializer = new XmlSerializer(payload.GetType());
importResult xmlImportResult = new importResult();
XmlSerializer xmlResponse = new XmlSerializer(xmlImportResult.GetType());
StringWriter textWriter = new Utf8StringWriter();
string sResponseText = "";
// Serialize the payload if one is present
if (payload != null)
{
xmlSerializer.Serialize(textWriter, payload);
string payloadString = textWriter.ToString();
content = new StringContent(payloadString, Encoding.UTF8, "application/xml");
}
using (HttpClientHandler httpClientHandler = new HttpClientHandler { Credentials = new NetworkCredential(sTMLogon, sTMPassword) })
using (HttpClient httpClient = new HttpClient(httpClientHandler))
{
HttpRequestMessage request = new HttpRequestMessage(method, requestUri)
{
Content = content
};
try
{
HttpResponseMessage response = await httpClient.SendAsync(request);
sTMResponse = await response.Content.ReadAsStringAsync();
xmlImportResult = (importResult)xmlResponse.Deserialize(new StringReader(sTMResponse));
statusNumber = (int)response.StatusCode;
sResponseText = "HTTP Response: " + response.ReasonPhrase + " - Response status: " + xmlImportResult.status;
lblStatus.Text = sResponseText;
logger.WriteLog(sResponseText);
switch (statusNumber)
{
case 200:
break;
case 404:
break;
}
}
catch (Exception ex)
{
lblStatus.Text = ex.Message;
}
}
return statusNumber;
}
If I comment out the Environment.Exit(0) the form will load and wait and things work. If I uncomment it it quits before the httpClient.SendAsync(request) has had time to get the response.
How can I get the code to wait for the response and not do it asynchronously so the response comes back and the application can close normally after the response has come back?
Continue reading...