|
Hi,
depends on the circumstances. If you have a Bitmap, then GetPixel(x,y) returns the color
at that location. There are ways to turn (part of) your screen into a bitmap, search for
"screen capture".
|
|
|
|
|
Luc is right in both of your points, unless you meant the location of the pixel clicked, in that case use the mouse event e.Location
All generalizations are wrong, including this one!
(\ /)
(O.o)
(><)
|
|
|
|
|
I'm developing an application in which multiple clients exchange data on a TCP link with a server using a custom protocol.
I've chosen the asynchronous approach to implement the server: i.e. my application listens for incoming connection with BeginAccept , and accepts them with EndAccept , receives data with BeginReceive/EndReceive and so on. I also keep a list of connected client state information, so that the users of my class can read several info about the clients (IP address, connection state etc.)
Now, I'm implementing several methods to forcibly disconnect a client, and to shutdown the whole server (disconnect all the clients and release server resources). Unfortunately, simply closeing the clients + server sockets does not work because the pending operations (EndAccept etc.) fail with various kind of exceptions (NullReference , ObjectDisposed etc.)
Is there a correct way to forcibly shutdown connections?
Regards,
Andrea
|
|
|
|
|
Hi Andrea,
I was hoping that Shutdown would be thread-safe. Obviously not, so you'll have to implement some control synchronization yourself.
You can do this with a ReaderWriterLock - or preferably ReaderWriterLockSlim, if you are targeting .NET 3.5. Something like this:
private volatile bool quit = false;
private ReaderWriterLock rwl = new ReaderWriterLock();
Socket clientSocket = null;
rwl.AcquireReaderLock(-1);
try
{
if ( quit ) return;
clientSocket = serverSocket.EndAccept(state);
}
finally
{
rwl.ReleaseReaderLock();
}
rwl.AcquireWriterLock(-1);
try
{
quit = true;
foreach ( var client in clientList )
{
client.Socket.Shutdown();
client.Socket.Dispose();
}
}
finally
{
rwl.ReleaseWriterLock();
}
Does that make sense?
Nick
----------------------------------
Be excellent to each other
|
|
|
|
|
As usual you save my day Nick! I'm currently refactoring my server code for better maintainability, as soon as possible I'll try your suggestion and let you know.
Thanks!
Andrea
|
|
|
|
|
You're welcome.
Metal76 wrote: and let you know
Please do - this is quite interesting
Nick
----------------------------------
Be excellent to each other
|
|
|
|
|
Hi Nick, before trying your suggestion, I was wondering: does using some kind of lock around socket I/O operations actually interfere with data exchange?
That is: as well as BeginReceive/EndReceive , in my server I also transmit data with BeginSend/EndSend . Suppose I acquire a lock over the socket while reading, and at the same time I have a trasmission going on over the same socket. What happens?
This is a situation I'm going to face in my code, which uses bidirectional full-duplex communications.
Regards,
Andrea
|
|
|
|
|
Hi Andrea,
I suggested using a reader/writer lock for this reason: in normal operation, the lock is acquired in reader mode. As you can have as many readers as you like at the same time, this does not effect normal operation.
Your problem was that calling Shutdown asynchronously while an EndReceive ( or EndSend ) was executing was throwing nasty exceptions. If you wrap the call to Shutdown in a writer acquire, it is guaranteed that it owns the lock in exclusive mode. This means that the EndXXX methods cannot be executing at the same time as Shutdown, which I think should solve your problem.
So, wrap all 'normal' calls on the Socket in reader acquires, and wrap the call to Shutdown in a writer acquire. Actually, now I think about it, you will probably have to wrap the calls to BeginXXX in reader acquires as well.
Nick
----------------------------------
Be excellent to each other
|
|
|
|
|
I chose a simpler approach than locks.
I keep a queuedIO count incremented for every "Beginxxx" call.
In the async callback delegates, the queuedIO count is decremented.
When the listener/socket is shutdown, all the pending IO completions
fail (as they should) throwing an exception on the "Endxxxx" calls
in the async callback delegate. I catch the expected exception and do
nothing in the exception handler. queuedIO count is still decremented.
The shutdown code waits for the queuedIO count to reach 0 before proceeeding.
The only lock required is on the queuedIO count variable, for which I use Interlocked
increments and decrements to insure atomic access. Thread.VolatileRead is used
to read the count while waiting for it to reach 0.
Works good for me
Mark
Mark Salsbery
Microsoft MVP - Visual C++
|
|
|
|
|
Hi Mark, at the moment my code implements a similar (but even simpler) approach: the EndXxx calls are wrapped in a try...catch block, only catching the SocketException and ObjectDisposed exception which seem to be the ones which get "normally" fired when I force the shutdown on the socket. Something like:
try
{
...
_Socket.BeginReceive(_ReceiveBuffer,
0,
_ReceiveBuffer.Length,
SocketFlags.None,
new AsyncCallback(ClientReceiveCallback),
null);
...
}
catch (SocketException sockExc)
{
SocketClose((SocketError)sockExc.ErrorCode);
}
catch (ObjectDisposedException objDispExc)
{
}
Do you think it could also be a valid approach? I feel a little uneasy to use exceptions to implement normal programming logic, but I could not find a "clean" way to forcibly shutdown sockets, even searching in CodeProject articles!
Regards,
Andrea
|
|
|
|
|
Metal76 wrote: I feel a little uneasy to use exceptions to implement normal programming logic, but I could not find a "clean" way to forcibly shutdown sockets
Hi Andrea,
I'd love to find a cleaner method, but I haven't, which is why I do
it that way I do include comments in my callback delegate code
to remind me that certain exceptions are expected during shutdown.
My server code was ported from C++, where I used an IO completion port.
The same problem existed there, except the function used by the completion handler
threads to wait for a completed IO (GetQueuedCompletionStatus) would indicate an error
instead of throwing an exception. I still used the queuedIOcount there, because I wanted
to wait for all pending IOs to be flushed from the completion port at shutdown before
continuing.
I'm pretty sure .NET's asynchronous IO uses IO completion ports as well, or the equivalent.
The difference is, .NET handles the pool of threads for you where in straight Win32, one
has to handle that manually. From everything I've seen, they chose to implement the shutdown
as an exception instead of using an error code. I guess an error code could have been
added to the IAsyncResult, but it wouldn't have been generic - IAsyncResults are used for more
than just socket accepts and receives. Instead, the specific "Endxxxx" method documentation
specifies the exception that will be thrown in error conditions. For example,
Socket.EndReceive() states "ObjectDisposedException...The Socket has been closed".
I don't see an alternative. The whole idea of asynchronous IO is "fire-and-forget".
Once you call "Beginxxxx", the IO request is out of your hands until completion.
They chose to implement exceptions instead of error codes - you don't get anything else
to work with AFAIK
Mark
Mark Salsbery
Microsoft MVP - Visual C++
|
|
|
|
|
Hi all,
Can i run windows application in Linux, which is developed in c#.net, Framework 2.0
Kindly reply if any body knows.....
Krishnraj
|
|
|
|
|
You can with Mono. Take a look at this article also.
Regards,
--Perspx
"I've got my kids brainwashed: You don't use Google, and you don't use an iPod." - Steve Ballmer
"Some people have told me they don't think a fat penguin really embodies the grace of Linux, which just tells me they have never seen an angry penguin charging at them in excess of 100mph." - Linus Torvalds
|
|
|
|
|
Not without a lot of work. Mono is available, but if you do anything at all with the file system, you may be out of luck.
I don't think you can run .Net apps in Wine or Crossover either.
"Why don't you tie a kerosene-soaked rag around your ankles so the ants won't climb up and eat your candy ass..." - Dale Earnhardt, 1997 ----- "...the staggering layers of obscenity in your statement make it a work of art on so many levels." - Jason Jystad, 10/26/2001
|
|
|
|
|
How to install mono framework in red hat?
i mean what are the steps to install mono on red hat?
Thanks in advance
Krishnraj
|
|
|
|
|
It depends on the app. Mono is an implementation of the ECMA CLI speceification, just like Microsofts .NET Framework. Mono is NOT a 100% compatible equivilent of .NET though.
|
|
|
|
|
Yes we can do that but we need Mono framework for that. It will much better work if you compile your code with mono framework on linux then it will work correctly.
|
|
|
|
|
Take a look at the mono project.
"The clue train passed his station without stopping." - John Simmons / outlaw programmer
"Real programmers just throw a bunch of 1s and 0s at the computer to see what sticks" - Pete O'Hanlon
"Not only do you continue to babble nonsense, you can't even correctly remember the nonsense you babbled just minutes ago." - Rob Graham
|
|
|
|
|
Hi
I'm working in a webserver (php code) and all works fine using the "Add Web Reference" option.
Question, HOW i can add a Web Reference with code??? (because ... well, maybe the host change the IP address).
NOTE: Using Nusoap, PHP and C#
|
|
|
|
|
One way is to use WSDL utility[^] and then modify the generated code the way you want.
The need to optimize rises from a bad design.
My articles[ ^]
|
|
|
|
|
Hallo
i run a .bat with Process.Start that performs some operations (partitions a disk and deploys an OS image to it) that doesn't return a progress information useful to determine the progression.
I would like to use it with a BackgroundWorker, but in every example i see that the DoWork method works upon some data returned by the process in order to determine the progression.
In my case it's not clear to me how to hook the process to the BGW component.
Could you give me some hints?
Thanks
modified on Saturday, November 8, 2008 3:10 AM
|
|
|
|
|
You can't do it unless you have the source code to the external process you're trying to run, and even then, it's going to be a pain in the ass.
"Why don't you tie a kerosene-soaked rag around your ankles so the ants won't climb up and eat your candy ass..." - Dale Earnhardt, 1997 ----- "...the staggering layers of obscenity in your statement make it a work of art on so many levels." - Jason Jystad, 10/26/2001
|
|
|
|
|
You can't show progress of the application but you can have a label which says work in progress while the bat file is running and than inform user when the bat file exits.
|
|
|
|
|
Or if he wants, a progress bar that recycles itself controlled by a BackgroundWorker .
"Why don't you tie a kerosene-soaked rag around your ankles so the ants won't climb up and eat your candy ass..." - Dale Earnhardt, 1997 ----- "...the staggering layers of obscenity in your statement make it a work of art on so many levels." - Jason Jystad, 10/26/2001
|
|
|
|
|
thank you, both of your suggestion are useful and can apply, but i have an option too: the batch file launches in sequence DISKPART (along with several SELECT DISK, SELECT PARTITION, FORMAT, etc commands) and IMAGEX:
both of them returns some messages, imagex in particular updates a percentual progression indicator.
Could i redirect these messages to the frontend?
|
|
|
|