Author Topic: how can a function be called again in C# before prior call finishes  (Read 1319 times)

0 Members and 1 Guest are viewing this topic.

Offline snarkysparkyTopic starter

  • Frequent Contributor
  • **
  • Posts: 415
  • Country: us
This text is inside a function.



Code: [Select]
            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 ?

 

Offline andersm

  • Super Contributor
  • ***
  • Posts: 1198
  • Country: fi
Re: how can a function be called again in C# before prior call finishes
« Reply #1 on: February 08, 2021, 08:37:51 pm »
It's an asynchronous function, and you've tied the calling code to some message that's sent repeatedly?

Offline snarkysparkyTopic starter

  • Frequent Contributor
  • **
  • Posts: 415
  • Country: us
Re: how can a function be called again in C# before prior call finishes
« Reply #2 on: February 08, 2021, 10:09:12 pm »
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.
 

Offline Syntax Error

  • Frequent Contributor
  • **
  • Posts: 584
  • Country: gb
Re: how can a function be called again in C# before prior call finishes
« Reply #3 on: February 08, 2021, 10:14:03 pm »
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?

 

Offline andersm

  • Super Contributor
  • ***
  • Posts: 1198
  • Country: fi
Re: how can a function be called again in C# before prior call finishes
« Reply #4 on: February 08, 2021, 10:40:54 pm »
Is it somehow automatically spawning threads and multiple instances when it is called.
No, 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.

Offline snarkysparkyTopic starter

  • Frequent Contributor
  • **
  • Posts: 415
  • Country: us
Re: how can a function be called again in C# before prior call finishes
« Reply #5 on: February 09, 2021, 02:47:46 pm »
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: [Select]
        void StringAppendRTB(string ToAdd)
        {

            if (pauseRTB == 0)
            {
                richTextBox1.AppendText(ToAdd + "\r\n");

                richTextBox1.ScrollToCaret();

                Application.DoEvents();

            }
        }

 

Offline dferyance

  • Regular Contributor
  • *
  • Posts: 188
Re: how can a function be called again in C# before prior call finishes
« Reply #6 on: February 09, 2021, 05:38:47 pm »
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.
 

Offline Tony_G

  • Frequent Contributor
  • **
  • Posts: 934
  • Country: us
  • Checkout my old test gear channel (link in sig)
    • TGSoapbox
Re: how can a function be called again in C# before prior call finishes
« Reply #7 on: February 10, 2021, 07:19:43 pm »
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: [Select]
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

Offline snarkysparkyTopic starter

  • Frequent Contributor
  • **
  • Posts: 415
  • Country: us
Re: how can a function be called again in C# before prior call finishes
« Reply #8 on: February 11, 2021, 01:52:13 pm »
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.2



Microsoft Visual Studio Community 2019
Version 16.6.2
VisualStudio.16.Release/16.6.2+30204.135
Microsoft .NET Framework
Version 4.8.03752

Installed Version: Community

Visual C++ 2019   00435-60000-00000-AA422
Microsoft Visual C++ 2019

ASP.NET and Web Tools 2019   16.6.948.25768
ASP.NET and Web Tools 2019

Azure App Service Tools v3.0.0   16.6.948.25768
Azure App Service Tools v3.0.0

C# Tools   3.6.0-4.20251.5+910223b64f108fcf039012e0849befb46ace6e66
C# 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.10
Provides common services for use by Azure Mobile Services and Microsoft Azure Tools.

IntelliCode Extension   1.0
IntelliCode Visual Studio Extension Detailed Info

Microsoft JVM Debugger   1.0
Provides support for connecting the Visual Studio debugger to JDWP compatible Java Virtual Machines

Microsoft MI-Based Debugger   1.0
Provides support for connecting Visual Studio to MI compatible debuggers

Microsoft Visual C++ Wizards   1.0
Microsoft Visual C++ Wizards

Microsoft Visual Studio VC Package   1.0
Microsoft Visual Studio VC Package

NuGet Package Manager   5.6.0
NuGet Package Manager in Visual Studio. For more information about NuGet, visit https://docs.nuget.org/

ProjectServicesPackage Extension   1.0
ProjectServicesPackage Visual Studio Extension Detailed Info

Test Adapter for Boost.Test   1.0
Enables 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.0
Enables 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+910223b64f108fcf039012e0849befb46ace6e66
Visual 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+1c969cac25e2d38d71872efe6c8226029e42bb59
Microsoft Visual F# Tools 10.9.1.0 for F# 4.7

Visual Studio Code Debug Adapter Host Package   1.0
Interop layer for hosting Visual Studio Code debug adapters in Visual Studio

Visual Studio Tools for CMake   1.0
Visual Studio Tools for CMake

 


Share me

Digg  Facebook  SlashDot  Delicious  Technorati  Twitter  Google  Yahoo
Smf