Wait until Task is Finished (Completed) in C#

Last Reply one month ago By dharmendr

Posted one month ago

Hello,

I am new to Task logic, I want to wait Task until a flag is true,

however I fail, I sent a message to a port and before it returns, my code does serialize which end up with wrong value. 

So "serialize" Task should wait until I get the port answer and make the flag true. Then perform the serialization. 

        private async void chk_Test1_CheckedChanged(object sender, EventArgs e)
        {            
            if (chk_Test1.Checked == true)
                SendMsg("close light");

            await Task.Run(() => Serialize());

            while (!obj.SetLaser2Status0)
            {
                Console.WriteLine("light close busy wait");
                await Task.Delay(25);
            } 
        }       
	   
	void Serialize()
        {
            string json = JsonConvert.SerializeObject(obj, Formatting.Indented );
            File.WriteAllText(path_combined, json);
        }