Answered by:
Accessing MainPage member

Question
-
I am doing some socket programming in C++ metro app. This is mainly based on the DataGramSocket sample in the Samples collection. But instead of using a separate class for handling the MessageRecived event of DatagrasmSocket I am using a function OnMessage() in the MainPage itself for handling replies from socket. And from this method, once I got the value I needed from the socket reply, I am trying to display the value in a TextBlock in the MainPage by calling a function Display() which is also in the MainPage class. But when I try to update the TextBlock control with the value I got from the OnMessage() callback, the app crashes complaining about access violation. I am initilaising and calling the sokcet function BindServiceNameAsync() from MainPage::OnNavigatedTo(). I am getting the required value from socket anyway and socket is working fine. What could be wrong? . The call to Display() is working fine and when debugging I can see that the control passes to MainPage::Display() and the crash is exactly at updating the TextBlock control. What wrong have I done?
public ref class MainPage sealed
{
void MainPage::OnNavigatedTo(NavigationEventArgs^ e)
{
DatagramSocket^ listener = ref new DatagramSocket();
listener->MessageReceived += ref new TypedEventHandler<DatagramSocket^, DatagramSocketMessageReceivedEventArgs^>(this, &MainPage::OnMessage);
task<void>(listener->BindServiceNameAsync(ServiceNameForListener->Text)).then([this] (task<void> previousTask)
{
});
}
void OnMessage(DatagramSocket^ socket, DatagramSocketMessageReceivedEventArgs^ eventArguments)
{
// I got the value i needede so post value to MaiPage
Display( val );
}
void Display( String^ val )
{
Outputtxt->Text = val; // crash here
}
private:
MainPage^ rootPage;
};
Sunday, August 5, 2012 6:46 AM
Answers
-
Is your socket callback running on the UI thread?
UI elements can only be accessed from the UI thread, although I'd expect a different error code. You can transition the call back to the dispatcher thread with CoreDispatcher.RunAsync.
--Rob- Proposed as answer by Jesse Jiang Tuesday, August 7, 2012 7:27 AM
- Marked as answer by Rob Caplan [MSFT]Microsoft employee, Moderator Tuesday, August 28, 2012 1:55 AM
Sunday, August 5, 2012 7:12 PMModerator -
Oops! I should have done it as follows:
String^ ValueToPost= "Hello";
Dispatcher->RunAsync( CoreDispatcherPriority::Normal, ref new DispatchedHandler([this, ValueToPost ] ()
{
CallMyFunc( ValueToPost );
}));It works..Thank you Rob !
- Marked as answer by Jesse Jiang Friday, August 24, 2012 6:58 AM
Tuesday, August 14, 2012 10:56 AM
All replies
-
Is your socket callback running on the UI thread?
UI elements can only be accessed from the UI thread, although I'd expect a different error code. You can transition the call back to the dispatcher thread with CoreDispatcher.RunAsync.
--Rob- Proposed as answer by Jesse Jiang Tuesday, August 7, 2012 7:27 AM
- Marked as answer by Rob Caplan [MSFT]Microsoft employee, Moderator Tuesday, August 28, 2012 1:55 AM
Sunday, August 5, 2012 7:12 PMModerator -
Thank you Rob . My socket callback is running on UI thread. I tried with CoreDispatcher but that too is giving me access violation.
Monday, August 6, 2012 4:40 AM -
Oops! I should have done it as follows:
String^ ValueToPost= "Hello";
Dispatcher->RunAsync( CoreDispatcherPriority::Normal, ref new DispatchedHandler([this, ValueToPost ] ()
{
CallMyFunc( ValueToPost );
}));It works..Thank you Rob !
- Marked as answer by Jesse Jiang Friday, August 24, 2012 6:58 AM
Tuesday, August 14, 2012 10:56 AM