I was sending a pictureframe to a client in synchronous mode which meant, the program gets blocked until the client is found or transfer is complete. Consequently, I shifted to asynchronous mode. Now as far as i can see, there's nothing wrong with the client/server bit of the code but the part, where I am copying the picture frame from a clipboard throws this exception. It's utterly preposterous ! Apparently NOTHING gets copied from the clipboard and the IDataObject ends up having a null status. This never happened in the synchronous communication mode, hell, i didnt even touch the frame grabbing portion of the code. Anybody know whats wrong

NullReferenceException was unhandled.
Quilnux
I am sorry I am unable to help you with the information you posted.
All I can tell is you are trying to use/pass null reference object.
Good luck!
pexxx
vua_rua
Which part of your code was changed to run asynchronously The copy-from-clipboard-part, or only the send to the client
12HH
Most probably you check clipboard BEFORE data is copied.
In async mode probably data is sent to clipboard a bit after you checked.
Another issue is you do not check if you have appropriate data in clipboard before start working with it.
clipboard should be used as last chance. Isn;t there another approach to pass data
HTH
incendy
Johansson Johan
Only the send to the client part was changed, for example, In converting from sync to async, "tcplistener.AcceptTcpClient" changed to "tcplistener.BeginAcceptTcpClient"; so on and so forth. Point being, you can think of the copy data from clipboard into an IDataObject as being a completely different function which was never altered.
Hmmm, one thing that has caught my attention though is this; when communicating asynchronously, we need a callback delegate(whatever that is). Now I've made the copy-from-clipboard-function my call back delegate from within which i call the EndAcceptTcpClient method. This however should have no bearing upon the IDataObject ... right I mean, whatever has to go wrong should go wrong with the socket coding right, not the rest of the stuff. Here's the code:
public
static ManualResetEvent tcpClientConnected = new ManualResetEvent(false);TcpListener listener = null;
private IDataObject rawImage;
Image finalImage;
private
void timer1_Tick(object sender, EventArgs e){
false;timer1.Enabled =
listener = new TcpListener(localAdd, 13000);
startServer(listener);
timer1.Enabled = true;
}
private
void startServer(TcpListener listener){
tcpClientConnected.Reset();listener.Start();
listener.BeginAcceptTcpClient(new AsyncCallback(showImage), listener);
tcpClientConnected.WaitOne();
}
private
void showImage(IAsyncResult ar){
try{
TcpListener listener = (TcpListener)ar.AsyncState;TcpClient client = listener.EndAcceptTcpClient(ar);
SendMessage(hWnd, WM_CAP_GET_FRAME, 1, 0);
SendMessage(hWnd, WM_CAP_EDIT_COPY, 1, 0);
rawImage = Clipboard.GetDataObject();
//EXCEPTION THROWN IN THE VERY NEXT LINE; rawImage NULL
finalImage = (Bitmap)rawImage.GetDat(DataFormats.Bitmap);
picCapture.Image = finalImage;
MemoryStream ms = new MemoryStream();
NetworkStream ns = client.GetStream();
finalImage.Save(ms, ImageFormat.Jpeg);
Byte[] photo = new Byte[ms.Length];
photo = ms.ToArray();
long input;
input = photo.Length;
Byte[] output = BitConverter.GetBytes(input); //Send the size of the image
ns.Write(output, 0, output.Length);
//Send the imagens.Write(photo, 0, photo.Length);
ns.Flush();
true;ms.Flush();
tcpClientConnected.Set();
timer1.Enabled =
} catch
{
MessageBox.Show("An error occured while connecting to the device, make sure it's plugged in properly");}
}