C
C#2w ago
De1337

freeze winform app until loop exits

I have an app running and when a specific button is hit it runs a bunch of tasks. I want to freeze the tasks at one point until a loop is finished. Not sure why this code isn't doing that:
loop = true;

//loop while receiving data to prevent going past this point until the end
while (loop == true)
{
//upon data receipt from tracer, send data to dataReceivedHandler function
port.DataReceived += new SerialDataReceivedEventHandler(dataRecievedHandler);
}
loop = true;

//loop while receiving data to prevent going past this point until the end
while (loop == true)
{
//upon data receipt from tracer, send data to dataReceivedHandler function
port.DataReceived += new SerialDataReceivedEventHandler(dataRecievedHandler);
}
Later in the code I have a loop=false in my dataReceivedHandlerFuntion to break the loop but code after this code is executing prematurely
4 Replies
Jimmacle
Jimmacle2w ago
that is a huge memory leak you're subscribing additional handlers to the event as fast as possible in an infinite loop
De1337
De13372w ago
okay well then I will not do it this way my goal is to loop on receiving data until complete and then move on
Jimmacle
Jimmacle2w ago
if you want code in one thread to wait until code in another thread is done, use a reset event or semaphore or tasks with async/await
De1337
De13372w ago
that should be enough for me to lookup what you mean I think thank you