Click here to Skip to main content
15,887,746 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi All,
I have cured one problem with a bit of software but created a side issue, deep breath the code is below:

C#
}
void Result()
{
   float Value_Extract = 0;
   int Lenght = 0;
    string ValueToBeExtracted;

    //Value_Extract = Convert.ToSingle(rtbIncoming.Text); errors due to the N
    //Length - 1 to get rid of N                     need to change
   // MessageBox.Show(rtbIncoming.Text);
    Thread.Sleep(1500);
    MessageBox.Show(rtbIncoming.Text);
    Lenght = ((rtbIncoming.TextLength)-3);
    ValueToBeExtracted = rtbIncoming.Text.Substring(1,Lenght-2);

   // MessageBox.Show(ValueToBeExtracted);

    float.TryParse(ValueToBeExtracted,out  Value_Extract);

  //  MessageBox.Show(Convert.ToString(Value_Extract));
    if (Value_Extract < 0.45)
    {

        MessageBox.Show("Too Low", "", MessageBoxButtons.OK, MessageBoxIcon.Error);
    }
    else if (Value_Extract > 0.55)
    {

        MessageBox.Show("Too High", "", MessageBoxButtons.OK, MessageBoxIcon.Error);
    }
    else
    {
       //txtResult.Text = Value_Extract.ToString();
        MessageBox.Show("Pass", "", MessageBoxButtons.OK, MessageBoxIcon.Information);
    }

Now it works well enough but the message box after the Thread<dot>Sleep. It show the rich text box contents which are blank to act as a time waster for the thing to return a value. With that box it works, without it BANG!

What I have tried:

A timer to waste time along the lines of
C#
var sw = new StopWatch();
var maxTime = TimeSpan.FromMinutes(5);
sw.Start();
for (int i=0; i < 100; i++)
{  
    RunfunctionA();

    if(sw.Elapsed <= maxTime)
    {
        RunfunctionB();
        break;
    }
}
I'm not too sure if this will cause a stop in the example 5 mins I need 2 - 3 seconds at most, altering this from minutes to seconds appears to break some thing I'm not sure what or even if this will work!
Am I going about this the right way?
Posted
Comments
0x01AA 3-Jan-24 13:06pm    
To be honest, the code looks ....
Now I suggest the next horror: After Thread.Sleep put a Application.DoEvents()
Good luck
glennPattonWork3 3-Jan-24 14:34pm    
DoEvents... I so remember that from VB6 when I started my first approach to debugging. The code looks that way well let us just say "use the code we have written for xxx" was used, not guilty!..
Thanks I will give that ago on Friday!
Richard Deeming 4-Jan-24 4:04am    
There seems to be a lot of information missing here. For example, what does "BANG!" mean? If you're getting an exception, provide the full exception details.
glennPattonWork3 4-Jan-24 5:06am    
Sorry more detail should have been added, question written in post software failure panic. 'BANG' meant it threw an exeception which I now know meant the Threads have done something awful and the device you are talking to is locked up. Although managment says it's quicker to use already existing code you can spent time trying to make it work an if() instead of a switch() etc. I think-hope Application.DoEvents() will solve the issue but...
Richard MacCutchan 4-Jan-24 5:17am    
If you get an exception, or any error, then it is important to provide the complete text, and also the line of code where it occurs. As it stands it is difficult to figure out what the above code is supposed to be doing. For example, what is the data that appears in the rtbIncoming box and where does it come from?

1 solution

Glen, don't use Thread.Sleep in your GUI code (which it has to be or the RichTextBox accesses would throw a cross-threading exception). Instead, use a Timer (on the GUI thread) and handle the Tick event, or move all possible "delayed" code to a different thread (BackgroundWorker makes this pretty simple) and use progress reporting to update the GUI.

When you call Thread.Sleep it suspends the current thread for that time - which means that no GUI element gets updated and your app "freezes" as far as the user is concerned if you sleep the GUI thread. Add in that MessageBox is an application modal call (it doesn't return until the dialog is dismissed by the user) and you start getting pretty ugly apps ... :D
 
Share this answer
 
Comments
glennPattonWork3 4-Jan-24 8:01am    
As I said this was a bit of a panic use X code to get Y to work, and the darn thing I didn't realise was going into a headless machine (used to pour Silicon). It became my job when the director whos job it was (a software engineering proffesional, with a string of certifcates) couldn't do it. My question is why was everything controlled via a nested if() and not a switch (with a nice default), don't post in panic!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900