BackgroundThread with infinite loop with Async method ?

Dharamart

Member
Joined
Jun 25, 2022
Messages
10
Programming Experience
10+
Hello Friends,


I need help in windows form. When ever i am trying to implement the Async method in background for infinite time , my windows apps not performing well. so i need solution for this please suggest me the solution for this issue.

i just want to run my windows application does not affect when my background service worker performing task . user from frontend wont affected and front end panel work smoothly.


language C#.
 
If you really need something running independent of yoru WinForms front end, actually implement a Windows Service instead of trying to run as a another thread within the same process space of your WinForms app.

Show us exactly how you are running your async method. Did you remember to use await on your async method call?
 
Also, if you really need that other thread to run infinitely, don't use up a thread pool thread and fool around async. Just start another thread.
 
C#:
private void Form1_Load(object sender, EventArgs e)
        {
Method1();
}

public static async Task Method1()
        {
            await Task.Run(() =>
            {
                while (true) {
                    updateXml("company", "qwer");
                    string myfile = @"file.txt";

                    using (StreamWriter w = File.AppendText(myfile))
                    {
                        w.WriteLine("The next line!" + DateTime.Now.ToShortDateString()+_generic.readXml("company"));
                        w.Close();
                    }
                }

                // Do something
                Task.Delay(1000*5).Wait();
            });
        }


 public string updateXml(string key,string value)
        {
            List<string> retValue = new List<string>();
            XmlDocument doc = new XmlDocument();
            try
            {
                doc.Load("bitDB.xml");
                XmlNode versionNode = doc.SelectSingleNode(@"/bitDB/" + key.ToString());
                versionNode.InnerText= value;
                // save the XmlDocument back to disk
                doc.Save("bitDB.xml");
            }
            catch (System.IO.FileNotFoundException)
            {
                Console.WriteLine("Please enter your company name and web API address.");
            }
        }

bitdb.xml
XML:
<?xml version="1.0" encoding="utf-8"?>
<bitDB>
  <company>qwer</company>
  <url>iouoiuio</url>
  <interval.minute>3</interval.minute>
  <lastCapture>17/06/2022 19:50:54</lastCapture>
  <total_count>14</total_count>
  <last_count>15</last_count>
</bitDB>
 
Last edited by a moderator:
You don't need await on line 8. Just let Task.Run() schedule that work on another thread.
 
A few asides:

You don't need to call Close() on line 17 since using will call Dispose() on the stream which will close the stream.

The code on line 22 is unreachable due to your infinite loop starting at line 10.

You don't need ToString() on line 34.
 
Back
Top Bottom