Question

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!

Was it helpful?

Solution

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.

OTHER TIPS

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

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top