Groups | Blog | Home
all groups > dotnet remoting > november 2007 >

dotnet remoting : Writting/Reading to the Host Clipboard



BADER
11/11/2007 7:28:01 PM
Private sub Capture()
dim altscan as short
dim snapparam as short
altscan = MapVirtualKey(VK_MENU, 0)
keybd_event(VK_SNAPSHOT, snapparam, 0, 0)
picturebox1.image = Clipboard.GetDataObject.GetData(DataFormats.Bitmap)
End Sub



this part of the code works fine when calling directly in a Client App.

but when calling this Sub Remotly, it returns exception at the last Line
where getting the Image from the Clipboard

the exception is "Object Reference not set to an instance of an Object"

It is like nothing is been written to the Clipboard

Any Idea Why?
Josh Hawley
12/26/2007 3:20:00 PM
I am getting this exact problem. I have a situation where i make a remoting
call to the server. On the server it calls its clipboard, and all i get back
is null. every time. if i take the EXACT same code and paste it into a
windows forms test , it works perfectly.

There must be something wierd that happens when you get remoted.

Also it has the same behavior when the client and server are run on the same
machine, and separate machines.

[quoted text, click to view]
Josh Hawley
12/26/2007 7:50:01 PM
On a muse, i tried Clipboard.Clear();

That threw a threadstate exception telling me i had the wrong appt state.
This explains alot. I wish Clipboard.GetData() would throw an exception
instead of just returning null, it would have saved me half a day of beating
my head on my monitor. Since you cant change the appt state on a running
thread, and you cant create the thread that the remoting call comes in on,
your only option is to create another thread.



So here is what i did, it seems ugly to me, but works well even with alot of
calls from multiple clients. I'm sure i could improve it to have the other
thread loop, and a few other effiency things, but its not that important
where i'm using it.



1. lock (important because you never know everything when remoting is
involved)

2. create thread and SetAppartmentState

3. start thread

4. block until its done

5. get results, and return them (i used a property)



private Bitmap ReturnBMP = null;

private object BMPLock = new object();

private ManualResetEvent GetImageMRELock = new ManualResetEvent(false);

private Bitmap GetImage()

{

lock (this.BMPLock)

{

this.GetImageMRELock.Reset();

Thread t = new Thread(new ThreadStart(t_GetBMP));

t.SetApartmentState(ApartmentState.STA);

t.Start();

this.GetImageMRELock.WaitOne();

return this.ReturnBMP;

}

}



private void t_GetBMP()

{

IDataObject data = Clipboard.GetDataObject();

this.ReturnBMP = (System.Drawing.Bitmap)data.GetData(DataFormats.Bitmap);

this.GetImageMRELock.Set();

}



[quoted text, click to view]
AddThis Social Bookmark Button