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 2 years ago.
Improve this question
How would you go about killing several processes? I'm trying to update a program but I am finding I have to taskkill 8 processes that are related to the main program before I can actually update it. I just want a simple c# program I can run that will kill these so I can do the update.
I know I can do:
foreach (var process in Process.GetProcessesByName("program-1"))
{
process.Kill();
}
But when I try to add "program-1","program-2","program-3",etc I get errors.
Simply use Process.GetProcesses() and filter the processes you want to kill:
await Process.GetProcesses()
.Where(p => p.ProcessName.ToLower()
.StartsWith("program-"))
.ToAsyncEnumerable()
.ForEachAsync(p => p.Kill());
In addition to #Majid Shahabfar answer:
Kill(Boolean) stops the process and its child/descendent processes.
Further more, Kill is an async method and a good practice after calling Kill() is to wait for the process to exit using WaitForExit method, or calling HasExited to ensure the process has exited.
Related
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 7 months ago.
Improve this question
I have a button that executes a command, and it contains a while loop, the application freezes after I start the command, I have also tried to run it recursively but I got the same behavior, and using a timer is not an option, the task can't be called using an interval
You have to run the blocking section of your code in a separate thread. Running it on the main UI thread will freeze the UI.
You can use Tasks to do this.
using System.Threading.Tasks;
Task.Run(() => {
while (condition)
{
// operation
}
});
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 2 years ago.
Improve this question
I am developing a C# apps with callback functions.
When the app gets a message, a callback function is triggered and opens a form to display the contents of the message. I do that with .Show() and .BringToFront().
But the new form window, that is opened is frozen. I can't click buttons or even close the window. It remains in the background.
Can somebody tell me why this is happening?
I am using visual studio 2019.
The callback function:
public void currentSelectedCharacteristic_ValueChanged3(GattCharacteristic sender, GattValueChangedEventArgs args)
{
Allgemein.HinweisFenster.Show();
Allgemein.HinweisFenster.BringToFront();
}
We need more details to solve. But i can say that there is a process that blocks your main thread. Try to use 'Application.DoEvent();' or move your code that blocks your thread to another thread. And consider using 'background worker' for this.
Please refer to this thread for more details.
You should also consider that for Windows application, you have to be aware of threads manipulation, thus you have always one GUI thread that can be impacted by your calls flow.
So a rule of thumb is to know how your threads work inside your application.
Generally speaking, Frozen windows are often a sign of GUI thread block.
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 3 years ago.
Improve this question
I have a problem with a project. After I close my program it won't start anymore because it is still half running in the background.
Sideinfos:
I use threads to asynchronously send refreshing values to other clients via multicast.
How can I stop the threads, so they won't run in the background and I can restart the program?
When you start a thread, you can just set the IsBackground property to true so that it does not survive the death of the other threads.
var thread = new Thread(SendAndReceive);
thread.IsBackground = true;
thread.Start();
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 5 years ago.
Improve this question
On clicking directly my c# application.exe file, i am running my application and its process in task manager appears if that process on certain condition kills my application stops. What i want is to re run the application after delay of 5 seconds by it self.
You cannot. That process is gone. What you can do is start a second process with you first, most commonly called a "watchdog" or "guard", to scan for your first process and if it does not find it, start it. It's not foolproof though, somebody could just as easily kill that process, too. It's just another layer.
Advice on how to implement that is far to broad for this format, I suggest you read some good articles on it, try to implement it and come here when you find yourself stuck at a specific problem.
Although my answer might not be that helpful, and also this question isn't a kind related to coding issues, but I suppose, using Windows Task Scheduler might work out for you, as you can add various triggers to your application and repeat your task after any set amount of time.
Hope it helps. Thanks
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
I'm learning C# and it's basically my first coding experience. I've created a Main "Menu" method that goes to other sub methods (some are voids, others bools and ints) with a switch and usage of :
Program Execute = new Program();
Execute.SubMethodName();
Which is working quite well for me, except that i can't exit the sub-method. I have searched over google, stack overflow and the msdn website and every time the answer was to use "return" but i have tried it and it just returns back to the sub-method. How can i exit the sub-methods back to my Main method ?
(Excuse me if i'm not very clear, i'm still learning and i'm very willing to rephrase myself)
All your Exercice functions wait for the user to press ENTER (you call Console.ReadLine()). So to exit, you need to press ENTER on the console screen.