How To Fix Indy TIdTCPServer Freezing When Sending Text To TIdTCPClient? - indy10

Cannot send text from TIdTCPServer To TIdTCPClient, the server hanging (Not Responding), it just freezes when trying to send text to the client.
I Started Recently using Indy TIdTCPClient and TIdTCPServer, i have the client application working well when sending and receiving response from the server form,
then issue is from the server it doesn't send data, and when i try to send data like Indy creators provided in their Doc, it just freezes and then stops responding (crashes) :(, the weird thing is that the server sends back the response on Execute Event, and not sending data with my send function, so here is my code that i use:
Server Execute Event:
void __fastcall TServerMain::IdTCPServer1Execute(TIdContext *AContext)
{
UnicodeString uMessage;
uMessage = AContext->Connection->IOHandler->ReadLn();
MessageDisplay1->Lines->Add(uMessage);
AContext->Connection->IOHandler->WriteLn("Response OK!"); // i can receive the response from the client
}
Server Send Function:
void TServerMain::itsSendMessage(TIdTCPServer *itsName, UnicodeString uMessage) {
TIdContextList *Clients;
TIdContext *icContext;
if ( uMessage.Length() != 0 && itsName->Active ) {
Clients = itsName->Contexts->LockList();
for (int i = 0; i < Clients->Count; i++) {
icContext = (TIdContext*)Clients->Items[i];
icContext->Connection->IOHandler->WriteLn(uMessage);
}
itsName->Contexts->UnlockList();
}
} // this function doesn't send text to the clients however, it just hangs the application for ever.
Additional Note: The TIdTCPServer stops sending text even from it's OnExecute event when a client is disconnects!
UPDATE:
void __fastcall TMyContext::AddToQueue(TStream *AStream)
{
TStringList *queue = this->FQueue->Lock();
try {
queue->AddObject("", AStream);
this->FMessageInQueue = true;
}
__finally
{
this->FQueue->Unlock();
}
}
void __fastcall TMyContext::CheckQueue()
{
if ( !this->FMessageInQueue )
return;
std::unique_ptr<TStringList> temp(new TStringList);
TStringList *queue = this->FQueue->Lock();
try {
temp->OwnsObjects = true;
temp->Assign(queue);
queue->Clear();
this->FMessageInQueue = false;
}
__finally
{
this->FQueue->Unlock();
}
for (int i = 0; i < temp->Count; i++) {
this->Connection->IOHandler->Write( static_cast<TStream*>(temp->Objects[i]), static_cast<TStream*>(temp->Objects[i])->Size, true );
}
}
Server Send Function:
void __fastcall TServerMain::IdSendMessage(TIdTCPServer *IdTCPServer, TStream *AStream)
{
if ( !IdTCPServer->Active )
return;
TIdContextList *Clients = IdTCPServer->Contexts->LockList();
try {
for (int i = 0; i < Clients->Count; i++) {
static_cast<TMyContext*>(static_cast<TIdContext*>(Clients->Items[i]))->AddToQueue(AStream);
}
}
__finally
{
IdTCPServer->Contexts->UnlockList();
}
}
Client Receive Function:
void __fastcall TReadingThread::Receive() {
TMemoryStream * ms = new TMemoryStream();
this->IdTCPClient1->IOHandler->ReadStream(ms);
ms->Position = 0;
ClientMain->Image1->Picture->Bitmap->LoadFromStream(ms);
delete ms;
}
This function is Synchronized in a TThread.
This is how i send a TBitmap using TMemoryStream:
void __fastcall TServerMain::CaptureDesktop()
{
// Capture Desktop Canvas
HDC hdcDesktop;
TBitmap *bmpCapture = new TBitmap();
TMemoryStream *Stream = new TMemoryStream();
try {
bmpCapture->Width = Screen->Width;
bmpCapture->Height = Screen->Height;
hdcDesktop = GetDC(GetDesktopWindow());
BitBlt(bmpCapture->Canvas->Handle, 0,0,Screen->Width, Screen->Height, hdcDesktop, 0,0, SRCCOPY);
bmpCapture->SaveToStream(Stream);
Stream->Position = 0;
IdSendMessage(IdTCPServer1, Stream);
}
__finally
{
ReleaseDC(GetDesktopWindow(), hdcDesktop);
delete bmpCapture;
delete Stream;
}
}

TIdTCPServer is a multi-threaded component, which you are not accounting for propery.
The server's various events are fired in the context of internal worker threads, not in the context the main UI thread. Your OnExecute code is not syncing with the main UI thread when accessing MessageDisplay1, which can cause all kinds of problems, including but not limited to deadlocks. You MUST sync with the main UI thread, such as with TThread::Synchronize() or TThread::Queue(). For example:
void __fastcall TServerMain::IdTCPServer1Execute(TIdContext *AContext)
{
String Message = AContext->Connection->IOHandler->ReadLn();
// see http://docwiki.embarcadero.com/RADStudio/en/How_to_Handle_Delphi_Anonymous_Methods_in_C%2B%2B
TThread::Queue(nullptr, [](){ MessageDisplay1->Lines->Add(Message); });
AContext->Connection->IOHandler->WriteLn(_D("Response OK!"));
}
Also, you have 2 threads (whichever thread is calling itsSendMessage(), and the OnExecute thread) that are not syncing with each other, so they can potentially write text to the same client at the same time, overlapping each other's text and thus corrupting your communications. When sending unsolicited messages from the server to a client, I usually recommend (depending on circumstances) that you queue the messages and let the client thread's OnExecute code decide when it is safe to send the queue. Another reason to do this is to avoid deadlocks if one client becomes blocked, you don't want to block access to other clients. Do as much per-client work in the client's own OnExecute event as you can. For example:
class TMyContext : public TIdServerContext
{
private:
TIdThreadSafeStringList *FQueue;
bool FMsgInQueue;
public:
__fastcall TMyContext(TIdTCPConnection *AConnection, TIdYarn *AYarn, TIdContextThreadList *AList = nullptr)
: TIdServerContext(AConnection, AYarn, AList)
{
FQueue = new TIdThreadSafeStringList;
}
__fastcall ~TMyContext()
{
delete FQueue;
}
void AddToQueue(const String &Message)
{
TStringList *queue = FQueue->Lock();
try
{
queue->Add(Message);
FMsgInQueue = true;
}
__finally
{
FQueue->Unlock();
}
}
void CheckQueue()
{
if (!FMsgInQueue)
return;
std::unique_ptr<TStringList> temp(new TStringList);
TStringList *queue = FQueue->Lock();
try
{
temp->Assign(queue);
queue->Clear();
FMsgInQueue = false;
}
__finally
{
FQueue->Unlock();
}
Connection->IOHandler->Write(temp.get());
}
bool HasPendingData()
{
TIdIOHandler *io = Connection->IOHandler;
bool empty = io->InputBufferIsEmpty();
if (empty)
{
io->CheckForDataOnSource(100);
io->CheckForDisconnect();
empty = io->InputBufferIsEmpty();
}
return !empty;
}
};
__fastcall TServerMain::TServerMain(...)
{
IdTCPServer1->ContextClass = __classid(TMyContext);
...
}
void __fastcall TServerMain::IdTCPServer1Execute(TIdContext *AContext)
{
TMyContext *ctx = static_cast<TMyContext*>(AContext);
ctx->CheckQueue();
if (!ctx->HasPendingData())
return;
String Message = AContext->Connection->IOHandler->ReadLn();
TThread::Queue(nullptr, [](){ MessageDisplay1->Lines->Add(Message); });
AContext->Connection->IOHandler->WriteLn(_D("Response OK!"));
}
void TServerMain::itsSendMessage(TIdTCPServer *itsName, const String &Message)
{
if ( Message.IsEmpty() || !itsName->Active )
return;
TIdContextList *Clients = itsName->Contexts->LockList();
try
{
for (int i = 0; i < Clients->Count; ++i)
{
static_cast<TMyContext*>(static_cast<TIdContext*>(Clients->Items[i]))->AddToQueue(Message);
}
}
__finally
{
itsName->Contexts->UnlockList();
}
}

Related

ZKEmkeeper: Events not triggering on Windows Service

I'm stucked for a while trying to use zkemkeeper sdk to use on a Windows Service that uses a InBios(Controller) for fingerprint.
i first connect to the device and then i add the event OnAttTransactionEx, someone can point me what i'm doing wrong.
Here is the code snippet
`
protected override void OnStart(string[] args)
{
Thread TT = new Thread(new ThreadStart(WorkedThread));
TT.IsBackground = true;
TT.SetApartmentState(ApartmentState.STA);
this.isServiceStarted = true;
TT.Start();
}
private void WorkedThread()
{
WriteToFile("Worker Thread Started.");
ZKemClient objZkeeper = new ZKemClient(filepath);
this.isDeviceConnected = objZkeeper.Connect_Net("19x.x.x.24x", 4370);
if (this.isDeviceConnected)
{
WriteToFile("Device connected.");
WriteToFile("While loop execution starting.");
while (true)
{
WriteToFile(filepath, "While loop execution started.");
}
}
else
{
WriteToFile("Failed to connect to Device.");
}
}
// ZMClient class
public bool Connect_Net(string IPAdd, int Port)
{
bool bResult = false;
try
{
// Actual SDK class
CZKEM objCZKEM = new CZKEM();
if (objCZKEM.Connect_Net(IPAdd, Port))
{
if (objCZKEM.RegEvent(1, 32767))
{
// [ Register your events here ]
objCZKEM.OnAttTransactionEx += new _IZKEMEvents_OnAttTransactionExEventHandler(zkemClient_OnAttTransactionEx);
}
bResult = true;
}
}
catch (Exception ex)
{
WriteToFile("Connect_Net() Exception->" + ex.Message);
}
return bResult;
}
`
After playing a lot with threading, found a solution.
For windows service - Add reference to System.Windows.Forms
Thread TT1 = new Thread(() =>
{
this.objCZKEM = new CZKEM();
Application.Run();
});
TT1.IsBackground = true;
TT1.SetApartmentState(ApartmentState.STA);
TT1.Start();
Simply continue with current thread
if (this.objCZKEM.Connect_Net(IP, 4370))
{
this.WriteToFile("ZKEM device connected");
if (this.objCZKEM.RegEvent(1, 32767))
{
this.WriteToFile("ZKEM device events registration started");
// [ Register your events here ]
this.objCZKEM.OnAttTransactionEx += new _IZKEMEvents_OnAttTransactionExEventHandler(zkemClient_OnAttTransactionEx);
this.WriteToFile("Done with ZKEM device events registration.");
}
You should not able to receive finger events from device.

Xamarin Android Receive and Send SMS more than 160 characters

I'm writting sms application, which get from server by restapi number and message where to send and also receive sms from receipent. I have issue when received sms has more than 160 characters. And issue is when I have more than 160 characters to send by SMS.
Receive sms code:
public override void OnReceive(Context context, Intent intent)
{
if (intent.HasExtra("pdus"))
{
var smsArray = (Java.Lang.Object[])intent.Extras.Get("pdus");
foreach(var item in smsArray)
{
var sms = SmsMessage.CreateFromPdu((byte[])item);
SendReceivedStatus(sms.OriginatingAddress, sms.MessageBody);
}
}
}
SendReceivedStatus(sms.OriginatingAddress, sms.MessageBody);
It's a my method that convert to json receipentNumber and message.
Now my send sms code:
var sent = PendingIntent.GetBroadcast(Application.Context, 0, new Intent("SMS_SENT"), 0);
SmsManager.Default.SendTextMessage(responseModel.receipent, null, responseModel.message, sent, null);
protected override void OnResume()
{
base.OnResume();
var smsSentReceiver = new SMSSentReceiver();
RegisterReceiver(smsSentReceiver, new IntentFilter("SMS_SENT"));
}
public override void OnReceive(Context context, Intent intent)
{
switch ((int)ResultCode)
{
case (int)Result.Ok:
SenderActivity.resultsms = true;
SendStatus();
break;
case (int)SmsResultError.GenericFailure:
SenderActivity.resultsms = false;
SendStatus();
break;
case (int)SmsResultError.NoService:
SenderActivity.resultsms = false;
SendStatus();
break;
case (int)SmsResultError.NullPdu:
SenderActivity.resultsms = false;
SendStatus();
break;
case (int)SmsResultError.RadioOff:
SenderActivity.resultsms = false;
SendStatus();
break;
}
}
How to pack these messages?
Since SmsMessage.CreateFromPdu((byte[])) is deprecated since Android API level 19, here I also provide method to receive message more than 160 characters after KitKat version, here is my demo:
[BroadcastReceiver(Enabled = true, Label = "SMS Receiver")]
[IntentFilter(new[] { "android.provider.Telephony.SMS_RECEIVED" })]
public class SMSReceiver : BroadcastReceiver
{
public override void OnReceive(Context context, Intent intent)
{
Bundle bundle = intent.Extras;
if (bundle != null)
{
if (Build.VERSION.SdkInt >= BuildVersionCodes.Kitkat)
{
SmsMessage[] msgs = Telephony.Sms.Intents.GetMessagesFromIntent(intent);
var smstext = new StringBuilder();
foreach (var msg in msgs)
{
smstext.Append(msg.DisplayMessageBody.ToString());
}
Console.WriteLine(smstext.ToString());//output the received sms
}
else
{
var smsArray = (Java.Lang.Object[])bundle.Get("pdus");
SmsMessage[] messages = new SmsMessage[smsArray.Length];
for (int i = 0; i < smsArray.Length; i++)
{
messages[i] = SmsMessage.CreateFromPdu((byte[])smsArray[i]);
}
StringBuilder content = new StringBuilder();
if (messages.Length > 0)
{
foreach (var message in messages)
{
content.Append(message.DisplayMessageBody.ToString());
}
}
Console.WriteLine(content.ToString());//output the received sms
}
}
Toast.MakeText(context, "Received intent!", ToastLength.Short).Show();
}
}
I've tested my demo on Android 6.0 device and it works fine, I now can't find a Android 4.0 device for testing, but I think the method should work here. Any problem about this issue, please leave a comment.

Learning Delegates and Event Handlers, having issue with message not being shown

I am getting a crash course in delegates and event handlers and I have been following a tutorial on the subject and trying to plug in what I have learned into a socket server program I am creating.
I am trying to decouple my server from knowing about the AlertConnectionOpened class here:
namespace AlertConnectionOpened
{
//This is a subscriber class
public class AlertConnectionOpened
{
public void OnConnectionOpened(string message)
{
Console.WriteLine("Connection is opened");
}
}
}
So I am using a delegate in my server class to accomplish this.
namespace Server
{
public class RunServer
{
// State object for reading client data asynchronously
public class StateObject
{
// Client socket.
public Socket workSocket = null;
// Size of receive buffer.
public const int BufferSize = 1024;
// Receive buffer.
public byte[] buffer = new byte[BufferSize];
// Received data string.
public StringBuilder sb = new StringBuilder();
}
public class AsynchronousSocketListener
{
// Thread signal.
public ManualResetEvent allDone = new ManualResetEvent(false);
public AsynchronousSocketListener(int port)
{
}
//This defines the delegate
//Agreement between publisher and subscriber
//object, source of event or class publishing or sending data,
//second param is any additional data we need to send.
public delegate void ConnectionOpenEventHandler(string Message);
//Indicates something has happened and finished.
//Event defined here, based on delegate
public event ConnectionOpenEventHandler ConnectionOpened;
//Raise the Event, need a method to do this.
//Responsible for notifying subscribers
protected virtual void OnConnectionOpened()
{
if (ConnectionOpened != null)
ConnectionOpened("Connection Opened");
}
public void StartListening()
{
// Data buffer for incoming data.
byte[] bytes = new Byte[1024];
int port = 11000;
// Establish the local endpoint for the socket.
// The DNS name of the computer
// running the listener is "host.contoso.com".
IPHostEntry ipHostInfo = Dns.Resolve(Dns.GetHostName());
IPAddress ipAddress = ipHostInfo.AddressList[0];
IPEndPoint localEndPoint = new IPEndPoint(ipAddress, port);
// Create a TCP/IP socket.
Socket listener = new Socket(AddressFamily.InterNetwork,
SocketType.Stream, ProtocolType.Tcp);
// Bind the socket to the local endpoint and listen for incoming connections.
try
{
listener.Bind(localEndPoint);
//backlog of how many clients to take in
listener.Listen(100);
while (true)
{
// Set the event to nonsignaled state.
allDone.Reset();
// Start an asynchronous socket to listen for connections.
Console.WriteLine("Waiting for a connection...");
listener.BeginAccept(
new AsyncCallback(AcceptCallback),
listener);
// Wait until a connection is made before continuing.
allDone.WaitOne();
OnConnectionOpened();
}
}
catch (Exception e)
{
Console.WriteLine(e.ToString());
}
Console.WriteLine("\nPress ENTER to continue...");
Console.Read();
}
public void AcceptCallback(IAsyncResult ar)
{
// Signal the main thread to continue.
allDone.Set();
// Get the socket that handles the client request.
Socket listener = (Socket)ar.AsyncState;
Socket handler = listener.EndAccept(ar);
// Create the state object.
StateObject state = new StateObject();
state.workSocket = handler;
handler.BeginReceive(state.buffer, 0, StateObject.BufferSize, 0,
new AsyncCallback(ReadCallback), state);
}
public void ReadCallback(IAsyncResult ar)
{
String content = String.Empty;
// Retrieve the state object and the handler socket
// from the asynchronous state object.
StateObject state = (StateObject)ar.AsyncState;
Socket handler = state.workSocket;
// Read data from the client socket.
int bytesRead = handler.EndReceive(ar);
if (bytesRead > 0)
{
// There might be more data, so store the data received so far.
state.sb.Append(Encoding.ASCII.GetString(
state.buffer, 0, bytesRead));
// Check for end-of-file tag. If it is not there, read
// more data.
content = state.sb.ToString();
if (content.IndexOf("<EOF>") > -1)
{
// All the data has been read from the
// client. Display it on the console.
Console.WriteLine("Read {0} bytes from socket. \n Data : {1}",
content.Length, content);
Random rand = new Random();
content = rand.ToString();
// Echo the data back to the client.
Send(handler, content);
}
else {
// Not all data received. Get more.
handler.BeginReceive(state.buffer, 0, StateObject.BufferSize, 0,
new AsyncCallback(ReadCallback), state);
}
}
}
public void Send(Socket handler, String data)
{
// Convert the string data to byte data using ASCII encoding.
byte[] byteData = Encoding.ASCII.GetBytes(data);
// Begin sending the data to the remote device.
handler.BeginSend(byteData, 0, byteData.Length, 0,
new AsyncCallback(SendCallback), handler);
}
public void SendCallback(IAsyncResult ar)
{
try
{
// Retrieve the socket from the state object.
Socket handler = (Socket)ar.AsyncState;
// Complete sending the data to the remote device.
int bytesSent = handler.EndSend(ar);
Console.WriteLine("Sent {0} bytes to client.", bytesSent);
handler.Shutdown(SocketShutdown.Both);
handler.Close();
}
catch (Exception e)
{
Console.WriteLine(e.ToString());
}
}
public static int Main(String[] args)
{
AsynchronousSocketListener a = new AsynchronousSocketListener(0);
a.StartListening();
return 0;
}
}
}
}
Here is also my main program:
namespace AppStart
{
class ServerStart
{
static void Main(string[] args)
{
RunServer.AsynchronousSocketListener svr1 = new RunServer.AsynchronousSocketListener(11000);//publisher
//Correct the port number so a second server can open
RunServer.AsynchronousSocketListener svr2 = new RunServer.AsynchronousSocketListener(350);
svr1.StartListening();//publisher
//This creates the subscriber
var alertConnectionOpened = new AlertConnectionOpened.AlertConnectionOpened();//subsciber
//make publisher register the handler for the event.
svr1.ConnectionOpened += alertConnectionOpened.OnConnectionOpened; //POinter to method
svr2.StartListening();
Console.ReadLine();
}
}
}
From the little I understand of this my call to OnConnectionOpened();, should be showing the message that there is now a connection, but it isn't.

Looking to write Bluetooth 'hcitool' equivelant in Windows

I have used Bluez Bluetooth stack in Linux which comes with a handy utility 'hcitool'. Looking to build something like that in Windows with same or equivalent functionality. Specifically, 'hcitool name < MAC >', which shows if the specified device is within range.
Any guidance will be appreciated.
I have Windows SDK v7 with Visual Studio 2010, using C/C++
thanks.
Using my 32feet.NET library something like the following.
EDIT 3rd March: I've now added code to directly lookup the device by address rather than by using device discovery; so that's a simple 'new BluetoothDeviceInfo(...)'.
See if that finds the device you want. This requires the remote device to only be in "Connectable" mode whereas the former requires it to be in "Discoverable" mode. (BTW I've left the discovery code in place.)
EDIT 8th March: Now does a connect (using the SDP API) to check that the device is in range (and in connectable mode).
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using InTheHand.Net.Bluetooth;
using InTheHand.Net;
using InTheHand.Net.Sockets;
using System.Diagnostics;
using System.Net.Sockets;
namespace hcitool
{
partial class Program
{
static bool infoRatherThanName;
static BluetoothAddress _searchAddress;
static int Main(string[] args)
{
if (args.Length < 1) {
Console.WriteLine("Please specify command.");
return 2;
}
var cmd = args[0];
switch (cmd) {
case "name":
infoRatherThanName = false;
break;
case "info":
infoRatherThanName = true;
break;
//-
case "dev":
return ShowRadios();
//case "auth":
// return CauseAuth(GETADDRESS());
default:
throw new NotImplementedException("Command: '" + cmd + "'");
}
if (args.Length < 2) {
Console.WriteLine("Please specify device address.");
return 2;
}
var addrS = args[1];
_searchAddress = BluetoothAddress.Parse(addrS);
//
var dev = new BluetoothDeviceInfo(_searchAddress);
bool isInRange = GetCanConnectTo(dev);
if (isInRange) {
PrintDevice(dev);
} else {
Console.WriteLine("Can't see that device.");
}
//
Console.WriteLine("simple");
return Simple();
//return Fancier();
}
//----
private static int ShowRadios()
{
BluetoothRadio[] list;
try {
list = BluetoothRadio.AllRadios;
} catch (Exception) {
return 1;
}
Debug.Assert(list.Length != 0, "Expect zero radios case to raise an error.");
foreach (var curR in list) {
Console.WriteLine("* {0} '{1}'", curR.LocalAddress, curR.Name);
Console.WriteLine("{0}", curR.SoftwareManufacturer);
Console.WriteLine("{0}", curR.Manufacturer);
Console.WriteLine("{0}", curR.Mode);
}//for
return 0;
}
private static int CauseAuth(BluetoothAddress addr)
{
BluetoothSecurity.PairRequest(addr, null);
return 0;
}
//----
static int Simple()
{
BluetoothDeviceInfo[] devices;
BluetoothDeviceInfo foundDev = null;
var cli = new BluetoothClient();
// Fast: Remembered/Authenticated
devices = cli.DiscoverDevices(255, true, true, false, false);
SimpleCheckDevice(devices, ref foundDev);
if (foundDev == null) {
// Slow: Inquiry
cli.DiscoverDevices(255, false, false, true, false);
SimpleCheckDevice(devices, ref foundDev);
}
//
if (foundDev != null) {
return 0;
} else {
return 1;
}
}
private static void SimpleCheckDevice(IEnumerable<BluetoothDeviceInfo> devices,
ref BluetoothDeviceInfo foundDev)
{
foreach (var cur in devices) {
if (cur.DeviceAddress == _searchAddress) {
foundDev = cur;
PrintDevice(cur);
}
}//for
}
private static void PrintDevice(BluetoothDeviceInfo cur)
{
Console.WriteLine("* Found device: '{0}' ", cur.DeviceName);
if (infoRatherThanName) {
try {
var vs = cur.GetVersions();
Console.WriteLine(vs.Manufacturer);
Console.WriteLine(vs.LmpVersion);
Console.WriteLine(vs.LmpSubversion);
Console.WriteLine(vs.LmpSupportedFeatures);
} catch (Exception ex) {
Console.WriteLine("Failed to get remote device versions info: "
+ ex.Message);
}
}
}
//----
private static bool GetCanConnectTo(BluetoothDeviceInfo device)
{
bool inRange;
Guid fakeUuid = new Guid("{F13F471D-47CB-41d6-9609-BAD0690BF891}");
try {
ServiceRecord[] records = device.GetServiceRecords(fakeUuid);
Debug.Assert(records.Length == 0, "Why are we getting any records?? len: " + records.Length);
inRange = true;
} catch (SocketException) {
inRange = false;
}
return inRange;
}
}
}

BackKey not working when a Thread is running. Why?

PageA Navigated to PageB
There is a thread is running for HttpWebRequest.
Back Key is invalid when the Thread is running.
PageB Code:
protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e)
{
Debug.WriteLine("OnNavigatedTo");
//base.OnNavigatedTo(e);
DoWork();
}
void DoWork()
{
t = new Thread(new ThreadStart(() =>
{
request = WebRequest.Create("http://www.google.com") as HttpWebRequest;
request.BeginGetResponse(new AsyncCallback(AsyncBack), request);
}));
t.IsBackground = true;
t.Start();
}
void AsyncBack(IAsyncResult ias)
{
HttpWebRequest req = (HttpWebRequest)ias.AsyncState;
using (HttpWebResponse res = req.EndGetResponse(ias) as HttpWebResponse)
{
this.Dispatcher.BeginInvoke(() =>
{
this.PageTitle.Text = res.ContentLength.ToString();
long length = res.ContentLength;
for (long i = 0; i < length; i++)
{
//here imitate a long time for working
Debug.WriteLine(i);
if (i == length)
{
break;
}
}
Debug.WriteLine(res.ContentLength);
});
}
}
the Back Key is invalid until Method AsyncBack() is done.
'Back Key is invalid' Is that the app is not back to PageA when you touch the Back Key until Method AsyncBack() Done.
Why? Help me?
Why the bloody hell are you wrapping a async request in a custom thread? That doesn't even remotely make sense.
Then again, your question doesn't make much sense either, but most likely the error is related to the request is attempting to invoke a operation, via. the dispatcher, on the wrong page.
In your code you block UI thread for a long time, so you can't navigate page back, because it happens also on UI thread, put into Dispatcher only code that can't be executed not on UI.
void DoWork()
{
HttpWebRequest request = WebRequest.Create("http://www.googl.com") as HttpWebRequest;
request.BeginGetResponse(new AsyncCallback(AsyncBack), request);
}
void AsyncBack(IAsyncResult ias)
{
HttpWebRequest req = (HttpWebRequest)ias.AsyncState;
using (HttpWebResponse res = req.EndGetResponse(ias) as HttpWebResponse)
{
this.Dispatcher.BeginInvoke(() =>
{
this.PageTitle.Text = res.ContentLength.ToString();
});
long length = res.ContentLength;
for (long i = 0; i < length; i++)
{
long i_ = i;
//here imitate a long time for working
Thread.Sleep(10);
this.Dispatcher.BeginInvoke(() =>
{
this.PageTitle.Text = i_.ToString();
});
if (i == length)
{
break;
}
}
Debug.WriteLine(res.ContentLength);
}
}

Resources