I have a SilverLight button that gets a comment from a text box to be saved in my database and then closes the window.
SilverLight:
Task task = new Task();
private void saveChangeBtn_Click(object sender, RoutedEventArgs e)
{
string commentTxtBxValue = commentTxtBx.Text;
task.SaveComment(commentTxtBx.Text);
commentTxtBx.Text = commentTxtBxValue;
HtmlPage.Window.Invoke("closeWindow", null);
}
The SilverLight calls SaveComment()
in the model of my SilverLight project which creates a URI and, using this URI, it sends the comment text to my MVC controller with UploadStringAsync()
.
Model Code:
public void SaveComment(string Comment)
{
// Create post string
StringBuilder postData = new StringBuilder();
postData.AppendFormat("?{0}={1}", "id", this.PageBreakId);
postData.AppendFormat("&{0}={1}", "comment", Comment);
string dataString = postData.ToString();
byte[] data = Encoding.UTF8.GetBytes(dataString);
// Configure client
WebClient client = new WebClient();
client.DownloadStringCompleted += new DownloadStringCompletedEventHandler(client_DownloadStringCompleted);
client.UploadStringCompleted += new UploadStringCompletedEventHandler(client_UploadStringCompleted);
string baseUri = HtmlPage.Document.DocumentUri.ToString().ToLower();
baseUri = baseUri.Substring(0, baseUri.IndexOf("currentPage"));
Uri uri = new Uri(baseUri + "SaveComment", UriKind.Absolute);
try
{
// Start upload
client.UploadStringAsync(uri, dataString);
}
catch (Exception e)
{
System.Diagnostics.Debug.WriteLine(e.Message);
}
}
Finally, my MVC Controller saves it to the database.
MVC Controller:
[AcceptVerbs(HttpVerbs.Post)]
public ActionResult SaveComment()
{
//Save to Database
}
My problem is that, it seems, my SilverLight code HtmlPage.Window.Invoke("closeWindow", null);
closes the page and ends code execution before it has a chance to finish. If I put a break point on my MVC controller it will only sometimes get triggered. I've noticed that FireFox will nearly always hit it but IE8 will almost never hit it.
What can I do to ensure my code will finish before I close the page?
Thank you,
Aaron