how can a function be called again in C# before prior call finishes - Page 1
Forum section: electronics / general

author: snarkysparky
author_rank: Frequent Contributor
timestamp: February 08, 2021, 08:19:53 pm
content: This text is inside a function.Code:MessageBox.Show("here now ", "status");for (uint j = StartBlock; j < (StartBlock + NumBlocks); j++){// Thread.Sleep(200);// StringAppendRTB(" erasing block -> " + j.ToString());}textBox1.Text = "here now";I am not creating any threads. but this function is being called before it completes.I get a screen full of message boxes and never get anything in textbox1;How ?

author: andersm
author_rank: Super Contributor
timestamp: February 08, 2021, 08:37:51 pm
content: It's an asynchronous function, and you've tied the calling code to some message that's sent repeatedly?

author: snarkysparky
author_rank: Frequent Contributor
timestamp: February 08, 2021, 10:09:12 pm
content: Yes. It is possible it is called again before its finished.How could I make this function stay put until it completes ?Is it somehow automatically spawning threads and multiple instances when it is called.I am just a poor c programmer. I want it be like c.

author: Syntax Error
author_rank: Frequent Contributor
timestamp: February 08, 2021, 10:14:03 pm
content: I am not too sure if this is the answer that you expect, but from your code, the MessageBox will simply block the form unless you click OK on the message box. After which the loop will run and then, textbox should populate. Unless for some reason, the loop is failing with an exception?

author: andersm
author_rank: Super Contributor
timestamp: February 08, 2021, 10:40:54 pm
content: Quote from: snarkysparky on February 08, 2021, 10:09:12 pmNo, it is making the message loop run, so it can receive user input. But the event that caused your code snippet to be called in the first place is still being triggered and dispatched, popping up another message box that starts waiting for input, and so on and so on.

author: snarkysparky
author_rank: Frequent Contributor
timestamp: February 09, 2021, 02:47:46 pm
content: Unhandled exception at 0x769A47B3 (user32.dll) in Program.exe: 0xC000041D: An unhandled exception was encountered during a user callback.This appears to be throwing an exception.Code:void StringAppendRTB(string ToAdd){if (pauseRTB == 0){richTextBox1.AppendText(ToAdd + "\r\n");richTextBox1.ScrollToCaret();Application.DoEvents();}}

author: dferyance
author_rank: Regular Contributor
timestamp: February 09, 2021, 05:38:47 pm
content: The response from andersm is likely correct. Calling MessageBox runs the message loop which can call your function again -- effectively recursion.One alternative is to use a log file instead of a message box for debugging. Or use Debugger.Break. Neither of these will pump for messages. But I think there is likely something wrong in another part of your program given this is unexpected. Rather than removing the message pump, check for what is triggering your event.

author: Tony_G
author_rank: Super Contributor
timestamp: February 10, 2021, 07:19:43 pm
content: There is something else going on in your code that is causing this - I created a minimal program that does what your code said and it works as expected:Code:using System;using System.Threading;using System.Windows.Forms;namespace Test{public partial class Form1 : Form{int pauseRTB = 0;public Form1(){InitializeComponent();}private void button1_Click(object sender, EventArgs e){uint StartBlock = 0;uint NumBlocks = 100;MessageBox.Show("here now ", "status");for (uint j = StartBlock; j < (StartBlock + NumBlocks); j++){Thread.Sleep(200);StringAppendRTB(" erasing block -> " + j.ToString());}textBox1.Text = "here now";}void StringAppendRTB(string ToAdd){if (pauseRTB == 0){richTextBox1.AppendText(ToAdd + "\r\n");richTextBox1.ScrollToCaret();Application.DoEvents();}}}}This was with VS 2019 against Fx 4.7.2 - What are you using?TonyG

author: snarkysparky
author_rank: Frequent Contributor
timestamp: February 11, 2021, 01:52:13 pm
content: That code was acting so screwy i abandoned it.I swear I think the call to update the rich text box was returning an incorrect call site. One prior to the function call itself. I know... Never blame the compiler.... But I had to move on.Thanks to all who replied.This was the VS setup.dot net 4.7.2Microsoft Visual Studio Community 2019Version 16.6.2VisualStudio.16.Release/16.6.2+30204.135Microsoft .NET FrameworkVersion 4.8.03752Installed Version: CommunityVisual C++ 2019 00435-60000-00000-AA422Microsoft Visual C++ 2019ASP.NET and Web Tools 2019 16.6.948.25768ASP.NET and Web Tools 2019Azure App Service Tools v3.0.0 16.6.948.25768Azure App Service Tools v3.0.0C# Tools 3.6.0-4.20251.5+910223b64f108fcf039012e0849befb46ace6e66C# components used in the IDE. Depending on your project type and settings, a different version of the compiler may be used.Common Azure Tools 1.10Provides common services for use by Azure Mobile Services and Microsoft Azure Tools.IntelliCode Extension 1.0IntelliCode Visual Studio Extension Detailed InfoMicrosoft JVM Debugger 1.0Provides support for connecting the Visual Studio debugger to JDWP compatible Java Virtual MachinesMicrosoft MI-Based Debugger 1.0Provides support for connecting Visual Studio to MI compatible debuggersMicrosoft Visual C++ Wizards 1.0Microsoft Visual C++ WizardsMicrosoft Visual Studio VC Package 1.0Microsoft Visual Studio VC PackageNuGet Package Manager 5.6.0NuGet Package Manager in Visual Studio. For more information about NuGet, visithttps://docs.nuget.org/ProjectServicesPackage Extension 1.0ProjectServicesPackage Visual Studio Extension Detailed InfoTest Adapter for Boost.Test 1.0Enables Visual Studio's testing tools with unit tests written for Boost.Test. The use terms and Third Party Notices are available in the extension installation directory.Test Adapter for Google Test 1.0Enables Visual Studio's testing tools with unit tests written for Google Test. The use terms and Third Party Notices are available in the extension installation directory.Visual Basic Tools 3.6.0-4.20251.5+910223b64f108fcf039012e0849befb46ace6e66Visual Basic components used in the IDE. Depending on your project type and settings, a different version of the compiler may be used.Visual F# Tools 10.9.1.0 for F# 4.7 16.6.0-beta.20217.4+1c969cac25e2d38d71872efe6c8226029e42bb59Microsoft Visual F# Tools 10.9.1.0 for F# 4.7Visual Studio Code Debug Adapter Host Package 1.0Interop layer for hosting Visual Studio Code debug adapters in Visual StudioVisual Studio Tools for CMake 1.0Visual Studio Tools for CMake