|
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?
|
|
|
|
|
Usually not. They're usually dispaly using non-stream methods that your code would not be able to intercept or would be very difficult to process with any meaning.
A better option would be to get rid of the batch file and execute each command from your background worked. When you go from one step (command line) in the batch file to the next, you can update the progress bar. Keep in mind you will NOT be able to increment progress inside each of those steps.
|
|
|
|
|
Well this is just an idea ... you could use the Process.StandardOutput to get data from the cmd window(in which the bat runs) ... then you would have to output some "marks" from the .bat file in order to "signal" an update to the state of how far the things have gone. Check Process.StandardOutput on MSDN documentation.
|
|
|
|
|
This's how I would do it..
- If the process is well known to me, I would monitor something that it does all the time "like in the installation, the number of files it creates and the name of the final file created" and count those files while it's running and update my progress bar accordingly
- If this's not an option, then this's why we have a dump loading animation
All generalizations are wrong, including this one!
(\ /)
(O.o)
(><)
|
|
|
|
|
i'm trying both these approaches
1- i'm trying to use the wimgapi.dll to control the image restore
2- in the meanwhile i'll use a simply process.start and process.end for each of the .bat' step
|
|
|
|
|
i'm trying to monitor the total space written on disk, but that value is written only when process has completed and not while it's running: during the process execution, the GUI seems to be frozen.
how could I get it up?
modified on Sunday, November 16, 2008 4:17 PM
|
|
|
|