C# windows (WinForms) child application locks sockets [closed] - c#

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 6 years ago.
Improve this question
I have two windows C# WinForms applications that behave the same way related to this issue. The behavior exhibits if the applications are ran in this order:
Main application starts up and part of its normal working process opens some sockets; mixture of tcp udp and multicast.
At some time pressing a button it starts another application using C# process related libraries. The second app starts ok and all is ok.
It is known and acceptable that if I start a second instance of my main app the second instance won't be able to use the sockets the first one owns.
The issue that I have is that if I shutdown the first app and restart, it won't be able to acquire the sockets anymore... until the child app is also shutdown.
Note that the second app doesn't use ANY sockets whatsoever, but somehow windows keeps the sockets locked until the second app shuts down.
I HAVE THIS QUESTION RELATED TO PROGRAMMING IN C# WIN FORMS WHICH IS ON THE TOPIC NOW WITH CAPITAL LETTERS FOR PEOPLE WHO CAN NOT DISTINGUISH ON/OFF TOPIC:
how do I solve this so that I don't need to shutdown the second app for the first one to be able to acquire its sockets (WHICH -i know - ARE FREE).

This is normal handle inheritance behavior. Unfortunately the .Net Process.Start is passing true as bInheritHandles to CreateProcess (NB. an open request to allow control of this behavior exists: Make Process.Start have a option to change handle inheritance). As a work around, use the native CreateProcess instead, see When System.Diagnostics.Process creates a process, it inherits inheritable handles from the parent process.

Related

Do something before an application starts

I've been looking for a way to do some stuff before an application starts and stop the started application and execute after "the stuff" is completed.
Like an Antivirus applications, when you open an application which may be dangerous or has no certification, it executes a scanning and only after that execution allow the application to starts.
Already tried with watchers and WMI (C#) but no success, since the calling event happens before the application starts and there is no way to cancel the opening.
If there is a name for that technique or someone knows an example code in C++ or C# or even any other language.
For those who are downvoting the question at least have the F** balls to explain why is downvoting...
It's a legitimate question! Maybe i miss explain it but still a legitimate question. Ohh I forgot, in many years of your career you already had to know everything... What would happen if you lost internet connection for three days? Are you still be productive? Maybe you guys had the API and documentation injected by Tank from Matrix... That's how NEO learn to fight.
Thanks
One option (which might be used by antiviruses, but not sure) is described here:
https://www.codeproject.com/Articles/11985/Hooking-the-native-API-and-controlling-process-cre
Basically, hooking the functions NtCreateFile(), NtOpenFile() or NtCreateSection() (the last one being mentioned as the preferred).
However, the hook must be done from inside a kernel mode driver, which might be a "slight inconvenience" (especially under 64-bit Windows, where the drivers must be signed AFAIK).
Some options also mentioned here: How does a Windows antivirus hook into the file access process?

Static variables in .net web api server [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 7 years ago.
Improve this question
I have a web api server which basically responds to 2 requests: stop and start.
On start it does several things, for example, initializing timers that perform a method every X seconds, and on stop it just stops the timers.
In order to achieve that, I created a singleton class which handles the logic of the operations. (It needs to be singleton so the timers and more variables will exist only once for all the requests).
Server is running fine, but recently I got a AccessViolationException while accessing Globalconfiguration.AppSettings in order to retrieve a value from my webconfig file.
I found out by looking at my logs that the singleton class finalizer was called, even though I didn't restart the server.
The finalizer calls a method which I regularly use and works fine in other scenarios, and this method uses the GlobalConfiguration class which threw the exception.
I tried to find the cause for this without success.
So basically there are two bugs here:
1. Why was the finalizer called out of the blue? The server could run for a week.
2. The AccessViolationException.
Perhaps the bugs are related? If my application memory was somehow cleaned would it cause the finalizer to be called and an exception accessing the GlobalConfiguration? Just a theory....
Or perhaps maybe I don't handle the singleton properly? But, after reading about static variables in c# web servers I see that they should exist while the application exist, and as such the bug might not be related with handling the singleton. I do handle it OK - the singleton has a private static field which holds the actual instance and the initialization occurs via locking and double checking to prevent multiple threads creating the singleton.
Is my approach OK? Do you see any possible bug that I didnt expect in this behavior, or do you know of any behavior of the .net framework that could cause my static singleton to be destroyed?
For your first question as to why the finalizer was called the obvious explanation is that it didn't have any active GC roots, which caused the GC to place it on the finialization queue. As to why the GC didn't find an active root to your object there are two possibilities:
You have a bug in your code and are not holding onto a reference to the singleton
Your IIS application pool is being recycled (I'm assuming you're hosting the app in IIS)
In its default configuration IIS automatically restarts each application pool every 1740 minutes (29 hours) which in turn causes the current app domain for your application to be unloaded. In .net static variables exist per app domain which means that when the app domain is unloaded there are no longer any active GC roots to the singleton, which in turn means that the singleton is eligible for garbage collection and by extension finalization.
With regards to the AVE you're getting you need to remember that when your finalizer is executed everything you know is wrong. By the time your finalizer is executed the GlobalConfiguration object may have also been GC'd/finalized and whatever handle it had to the web.config may have been destroyed.
It's also important to note that by default when IIS recycles your application pool it waits for the next HTTP request before it actually recreates the application pool. This is good from a resource utilization perspective as it means that if your application is not getting any requests it will not be loaded into memory, however in your case it also means that none of your timers will exist until your app receives an HTTP request.
In terms of solving this problem you have a couple of options:
Disable IIS's automatic restart. This solves your immediate issue however it raises the question of what happens if the server gets restarted for some other reason. Does your application have some way of persisting state (e.g. in a database) so that if it was previously started it will continue when it comes online?
If so, you would also want to enable auto-start and pre-load for your application so that IIS automatically loads your application without an external HTTP request needing to be made. See this blog post for more details.
Host outside of IIS. IIS is primarily designed for hosting websites rather than background services so rather than fighting against it you could simply switch to using a Windows service to host your application. Using Owin you can even host your existing Web API within the service so you should need to make minimal code changes.

Hide all traces of a program that is running [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
So this question is kind of interesting, I think. And before I lose anyones attention or they start shoving their fingers into my face with accusations: no I am not making or attempting to make a virus. I actually had a really good idea for a game. This game will be kind of like a creepy pasta(http://en.wikipedia.org/wiki/Creepypasta), in that it will mimic the ideals of a "haunted game". This game, when launched, will play for only a few seconds before ultimately "crashing" back to the desktop. It is at this time that I would like to hide all traces of its existence(which is still running in the background) so that I may continue with phase 2 of the game. During this phase, I will randomly take control of a console window, or play creepy sound effects at arbitrary/random intervals. The game will open back up at random, as if it has a mind of its own, too, but the game will be different each time this happens.
I would like to hide the game from the task manager completely. So this window:
Will show absolutely nothing of the program, no matter the tab the user selects. I want the game to, quite literally, turn into a ghost. The programming language that I am planning on using is C# and for the graphics library, OpenTK(Which is irrelevant for this question, but I want to make sure I lay down as much information as possible).
Anyone have any ideas? Oh, and I should also mention that I am quite fluent in the .NET framework/api, and I can build any windows forms application by hand(without using the editor).
Update:
I just thought of a fun alternative to hiding it. The answer: make the program smarter. Send the program to desktop, then listen for the opening of a task manager. If it opens, my program immediately shuts down task manager and the game responds with something super creepy like "But i thought you wanted to play with me? Why are you trying to kill me?" in a console. Sounds awesome. Lol.
I am no expert but I think most techniques that deals with process hiding uses CreateRemoteThread.
http://msdn.microsoft.com/en-us/library/windows/desktop/ms682437(v=vs.85).aspx
It is pretty tough to get right, but there are maaany blogs about it, eg:
http://resources.infosecinstitute.com/using-createremotethread-for-dll-injection-on-windows/
This works by picking some victim process that is already running, like say svchost.exe and add your thread into this.
Also while speaking of svchost, you can also very legally register a service and be hosted by this windows process, your clients may see the running game by calling the listing command:
tasklist /svc /fi "imagename eq svchost.exe"
or:
http://www.howtogeek.com/80082/svchost-viewer-shows-exactly-what-each-svchost-exe-instance-is-doing/
This is a tad more hidden than directly appearing as a task, while remainging more gentle to the user than the CreateRemoteThread. Also less crash prone, and also, anti viruses usually hook CreateRemoteThread to block calls to it.

Multithreading: prevent Visual Studio from blocking a particular thread [duplicate]

This question already has answers here:
How to debug a single thread in Visual Studio?
(12 answers)
Closed 8 years ago.
I have a client/server multithreaded application and one thread is used to send a regular ping to the server. The problem appears when i want to debug other threads it also blocks my ping thread. I already tryed options in Visual Studio Debug->Windows->Thread but don't want to use it each time i need to debug a part of my application.
Moreover as I work in a team I can't force my teammates to use this technique. How can I do this without much effort for them (macro, ...)?
Thanks
edit: It's not a duplicate thread! I don't want to use conditional break point every time i need to debug my application! And don't want to complexity the debug procedure for the rest of my team. I searched everywhere and nobody spoke about executing the thread in another Process
Thanks everyone i finally found an answer to my question!
As #hyde suggested i run my Ping loop in another process, but rather than create another program i dynamically create and execute it from my code
as seen in this topic:
Is it possible to dynamically compile and execute C# code fragments?
And this more advanced topic:
http://simeonpilgrim.com/blog/2007/12/04/compiling-and-running-code-at-runtime/
But after all i still had a problem... I needed to share my NetworkStream between my Main Application and his Child (needed to send to the same server Thread), so i gave to the child the NetworkStream by reflection.
Now work correctly, thanks!

WPF program launches super fast on one computer but super slow on another [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 8 years ago.
Improve this question
My wpf program has a strange problem regarding the startup performance on different computer with same specs.
One computer loads my program less than a second.
Another computer with the same spec loads 10+ secs.
With the help of Visualstudio performance profiler, I notice that two computers loads the program differently! which is so strange.
My problem is basically the same as this post:
C# WPF Very slow application launch
The performance profiler on the "fast" computer(I mean start the program fast) shows that the program starts with System.Windows.Application.Run(),
whereas the "slow" one shows that it starts with System.Windows.Application.RunInternal(...)
With the additional "internal" the boots time increased 10 times even though two computers are of the same spec and the source code are the same. (Actually, it is just plain mvvm light WPF start file)
Any ideas?
The other thing to check may be anti-virus. Is one machine running real-time AV and the other not? Are both logged in users members of the same groups? Are both machines in the same OU?
Log not-caught exceptions (in app level).
See if the users on both
computers have same level of authorizations (both are admin i.e.; or rather are not).
That should help.
Check in both the computer what are the services running. Check your both CPU Usage in two scenario by running your application and after closing your application.

Categories

Resources