Using while loop in a command freezes window [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 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
}
});

Related

Killing Multiple Processes [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 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.

In C# Form freezes when opened from callback [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 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.

Not able to use per BackgroundWorker filled model data with MVVM pattern [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 3 years ago.
Improve this question
I have WPF application with MVVM implementation and do successful fill model data (ObservableCollection) with BackgroundWorker.
By trying to show Dialog with this model data, I get error "XamlParseException: Must create DependencySource on same Thread as the DependencyObject".
How can I fix thread save implementation of MVVM-Pattern?
In the DoWork method of BackgroundWorker before:
Facade.Instance.FillCache()
and after:
App.Current.Dispatcher.Invoke(new Action(() => Facade.Instance.FillCache()));
Thanks!

Program won't close fully after closing [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 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();

Program Freezes When Running Lengthy Task [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
Maybe it’s a novice question, but I a am a novice. I have a program which does a lengthy task, while it does it, the form stays frozen, if its minimized it can't be maximized Etc. How can I fix that.
Also, the program runs in a loop, and in each iteration prints info to a list box, line by line. Problem is, the list box doesn't get written, untill after the whole loop finishes. How can I correct that.
You can use either a Thread or a BackgroundWorker to do your work.
Calling lengthy code from the UI will block the UI thread and the app 'freezes'.

Categories

Resources