0

I'm having a problem with outputting to textblock. Basicly what I do is this:

private void ReadData()
    {
        double dHeading = 0;
        int iHeading = 0;
        String sString = "";
        while (!stop)
        {
            //Get Heading
            result = fsuipc.FSUIPC_Read(0x0580, 4, ref token, ref dwResult);
            result = fsuipc.FSUIPC_Process(ref dwResult);
            result = fsuipc.FSUIPC_Get(ref token, ref dwResult);
            dHeading = dwResult;

            if (dHeading != 0)
            {
                dHeading = dHeading * 360 / (65536.0 * 65536.0);
                iHeading = Convert.ToInt32(dHeading);
            }
            if (iHeading < 0)
            {
                iHeading = 360 + Convert.ToInt32(dHeading);
            }
            if (iHeading == 0)
            {
                iHeading = 360;
            }
            if (result == true && iHeading < 10)
            {
                sString =  "00" + Convert.ToString(iHeading);
            }
            if (result == true && iHeading >= 10 && iHeading < 100)
            {
                sString = "0" + Convert.ToString(iHeading);
            }
            if (result == true && iHeading >= 100)
            {
                sString = Convert.ToString(iHeading);
            }

            txbHeading.Text = sString;
            // But if I change this line to MessageBox.Show(sString);
            // it works fine.
        }
    }

The program freezes and I can't do anything with it. I have to stop it in VS . If I change the txbHeading.Text = sString to MessageBox.Show(sString), it works fine.

Please note that I just started with C#. Thanks in advance!

3
  • 1
    What is the problem? There is an error? Or the line is executed but nothing happen on the screen? Jan 25, 2013 at 0:46
  • what means, works fine? do you get an exception?
    – esskar
    Jan 25, 2013 at 0:46
  • Comment out the whole body of the function and add txbHeading.Text = "asdf" to check if you can set textblock text at all in that context. If not, then you need to investigate why not, if yes then you need to figure out why sString won't get shown. Jan 25, 2013 at 0:51

2 Answers 2

2

The while loop in your code causes the UI thread to block, so the program should stop responding when the method is called. A background worker allows your code to be executed in a seperate thread without blocking the GUI.

1
  • Yes it stops responden after a few seconds. Then how do I do use a background worker? I've never worked with it and I don't understand everything said on the internet.
    – Anthony
    Jan 25, 2013 at 18:31
1

try

this.Invoke(new Action(() => txbHeading.Text = sString))

instead. i assume you running outside the UI thread.

More on this: The Practical Guide to Multithreading - Part 1

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.