0

I'm new to C# and I'm trying to use async and await function in C# to work with threading and non blocking GUI.

This is what I have so far:

 public async Task ReadInfo()
        {
            string serial;
            android.UpdateDeviceList();
            if (android.HasConnectedDevices)
            {
                serial = android.ConnectedDevices[0];
                device = android.GetConnectedDevice(serial);
                string model = device.BuildProp.GetProp("ro.product.model");
                string bootloader = device.BuildProp.GetProp("ro.bootloader");
                string pda = device.BuildProp.GetProp("ro.build.PDA");

                addlog("Model : " , Color.White, true, true);
                addlog(model, Color.DodgerBlue, true, false); 
                addlog("Bootloader : ", Color.White, true, true);
                addlog(bootloader, Color.DodgerBlue, true, false); 
                addlog("PDA Version : ", Color.White, true, true);
                addlog(pda, Color.DodgerBlue, true, false); 
            }
            else
            {
                addlog("ADB device not found.", Color.Red, true, true);
            }

        }

This is the AddLog Method:

public void addlog(string s, Color color, bool isBold, bool newline = false)
        {
            if (newline)
            {
                rtbLog.BeginInvoke(new MethodInvoker(() => rtbLog.AppendText("\r\n")));
            }
            Color selectionColor = color;
            rtbLog.BeginInvoke(new MethodInvoker(() => rtbLog.SelectionStart = rtbLog.Text.Length));
            rtbLog.BeginInvoke(new MethodInvoker(() => selectionColor = rtbLog.SelectionColor));
            rtbLog.BeginInvoke(new MethodInvoker(() => rtbLog.SelectionColor = color));
            if (isBold)
            {
                rtbLog.BeginInvoke(new MethodInvoker(() => rtbLog.SelectionFont = new Font(rtbLog.Font, FontStyle.Bold)));
            }
            rtbLog.BeginInvoke(new MethodInvoker(() => rtbLog.AppendText(s)));
            rtbLog.BeginInvoke(new MethodInvoker(() => rtbLog.SelectionColor = selectionColor));
            rtbLog.BeginInvoke(new MethodInvoker(() => rtbLog.SelectionFont = new Font(rtbLog.Font, FontStyle.Regular)));
            rtbLog.BeginInvoke(new MethodInvoker(() => rtbLog.ScrollToCaret()));
        }

On Button1_Click I have this:

 private async void Button1_Click(object sender, EventArgs e)
        {
           await  ReadInfo();
        }

I don't know why its freezing the GUI.

Solution to the problem

Changing

public async Task ReadInfo()

to

 public void ReadInfo()

and calling on button1_click as

Task.Run(() => ReadInfo());
5
  • Using async and await does not magically create new threads for you. Also, since ReadInfo does not have a single await on it, the entire method runs synchronously. I would suggest you to get started with the official documentation: learn.microsoft.com/en-us/dotnet/csharp/programming-guide/… Commented Jun 2, 2018 at 23:23
  • What is rtbLog and why are you doing BeginInvoke with MethodInvoker just to set properties on it? Commented Jun 2, 2018 at 23:27
  • Cross Thread Calls. Commented Jun 2, 2018 at 23:29
  • I would suggest you to make ReadInfo public void ReadInfo and change your button click to await Task.Run(() => ReadInfo());. That should take care of not freezing the GUI and keeping your code clean Commented Jun 2, 2018 at 23:50
  • @CamiloTerevinto Thank you for making it short and easy that's what i expect as an answer Commented Jun 3, 2018 at 0:02

1 Answer 1

4

Nothing in ReadInfo is actually asynchronous - in fact, the compiler should already be warning you about this. If there is no asynchronous incomplete await, everything continues on the current thread - the UI thread.

Adding async does not make code run on a different thread.

Sign up to request clarification or add additional context in comments.

6 Comments

@CamiloTerevinto even await doesn't explicitly add any threading. In cases like this, I would expect sync-context to apply, so await is a call back to the UI thread anyway
How to implement new thread in this scenario ?
@AmanAli if you want to use a thread: use a thread - any of Thread, ThreadPool.QueueUserWorkItem or Task.Run should be good candidates
Yeah, I know. But it is awesome how people do not read the warning that the method will run synchronously.
I don't see any warning.
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.