Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6150289
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T19:35:39+00:00 2026-05-23T19:35:39+00:00

WinForms application, with one background worker, do some stuff and nothing else: private void

  • 0

WinForms application, with one background worker, do some stuff and nothing else:

private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
    {
        try
        {
            progressBar1.Invoke(new MethodInvoker(() => { progressBar1.Value = 0; progressBar1.Maximum = Directory.GetFiles(configuration.xml.path).Count(); }));
            Directory.GetFiles(configuration.xml.path).ToList()
                .ForEach(
                p =>
                {
                    Message = XmlSerializerHelper<message>.Read(p);
                    Message.data.ToList().ForEach(www => 
                        { 
                            if (www.state == 0)www.state = 37; 
                        }
                    );
                    try
                    {
                        dataGridView1.Invoke(new MethodInvoker(() =>
                        {
                            var deb1 = Message.caption.source;
                            var deb2 = configuration.periods.Single(c => c.list_parperiod_id.ToString() == Message.caption.datatype.ToString()).name;
                            var deb3 = dataGridView1.Rows.Cast<DataGridViewRow>().Select(o => o.Cells[0].Value.ToString()).Contains(Message.caption.source);
                            var deb4 = dataGridView1.Rows.Cast<DataGridViewRow>().Select(o => o.Cells[1].Value.ToString()).Contains(
                                configuration.periods.Single(c => c.list_parperiod_id.ToString() == Message.caption.datatype.ToString()).name);
                            if (!dataGridView1.Rows.Cast<DataGridViewRow>().Select(o => new
                            {
                                source = o.Cells[0].Value.ToString(),
                                period = o.Cells[1].Value.ToString()
                            }).Contains(
                                new { 
                                    source = Message.caption.source,
                                    period = configuration.periods.Single(c => c.list_parperiod_id.ToString() == Message.caption.datatype.ToString()).name }))                   
                            {
                                textBox1.Anchor = AnchorStyles.Bottom;
                                dataGridView1.Rows.Add(Message.caption.source, configuration.periods.Single(y => y.list_parperiod_id.ToString() == Message.caption.datatype.ToString()).name, "0", "0", "0", "-");
                                if (dataGridView1.Rows.Cast<DataGridViewRow>().Count() > 7)
                                    this.Size = new System.Drawing.Size(this.Width, this.Height + 22);
                                dataGridView1.Size = new Size(dataGridView1.Width, dataGridView1.Height + 22);
                                textBox1.Anchor = AnchorStyles.Top | AnchorStyles.Left | AnchorStyles.Right | AnchorStyles.Bottom;
                            }
                        }));
                        Message.data.Where(q => q.value.Length > 0).ToList().ForEach(r =>
                            {
                                try
                                {
                                    r.value = r.value.Replace(',', '.');
                                    data.insObjParameterByCodeValueUpd(r.ucode, r.state, double.Parse(r.value, CultureInfo.InvariantCulture), DateTime.Parse(r.timestamp), null);
                                    dataGridView1.Invoke(new MethodInvoker(() =>
                                        dataGridView1.Rows.Cast<DataGridViewRow>().Single(u => 
                                            u.Cells[0].Value.ToString() == Message.caption.source.ToString() && 
                                            u.Cells[1].Value.ToString() == configuration.periods.Single(c => c.list_parperiod_id.ToString() == Message.caption.datatype.ToString())
                                            .name).Cells["lasttime"].Value = DateTime.Now.ToString()));
                                    textBox1.Invoke(new MethodInvoker(() =>
                                            {
                                                textBox1.SelectionStart = textBox1.Text.Length;
                                                textBox1.SelectionColor = Color.Black;
                                                textBox1.AppendText(System.DateTime.Now.ToString() + " загружены " + configuration.periods.Single(m=>
                                                    m.list_parperiod_id == Message.caption.datatype).name.ToLower()+ " данные за "+ Message.caption.timestamp+ " от "+ Message.caption.source + ".\r\n");
                                            })
                                        );
                                    log.Write(System.DateTime.Now.ToString() + " загружены " + configuration.periods.Single(m =>
                                                    m.list_parperiod_id == Message.caption.datatype).name.ToLower() + " данные за " + Message.caption.timestamp + " от " + Message.caption.source + ".\r\n");
                                    if ((bool)e.Argument)
                                        dataGridView1.Invoke(new MethodInvoker(() =>
                                        {
                                            dataGridView1.Rows.Cast<DataGridViewRow>().Where(u => u.Cells[0].Value.ToString() == Message.caption.source.ToString() && u.Cells[1].Value.ToString() == configuration.periods.Where(c => c.list_parperiod_id.ToString() == Message.caption.datatype.ToString()).Select(c => c.name).First().ToString()).Single().Cells["collect"].Value =
                                        (int.Parse(dataGridView1.Rows.Cast<DataGridViewRow>().Where(u => u.Cells[0].Value.ToString() == Message.caption.source.ToString() && u.Cells[1].Value.ToString() == configuration.periods.Where(c => c.list_parperiod_id.ToString() == Message.caption.datatype.ToString()).Select(c => c.name).First().ToString()).Single().Cells["collect"].Value.ToString()) + 1).ToString();
                                        }
                                          ));
                                    else
                                        dataGridView1.Invoke(new MethodInvoker(() =>
                                        {
                                            dataGridView1.Rows.Cast<DataGridViewRow>().Where(u => u.Cells[0].Value.ToString() == Message.caption.source.ToString() && u.Cells[1].Value.ToString() == configuration.periods.Where(c => c.list_parperiod_id.ToString() == Message.caption.datatype.ToString()).Select(c => c.name).First().ToString()).Single().Cells["hand"].Value =
                                        (int.Parse(dataGridView1.Rows.Cast<DataGridViewRow>().Where(u => u.Cells[0].Value.ToString() == Message.caption.source.ToString() && u.Cells[1].Value.ToString() == configuration.periods.Where(c => c.list_parperiod_id.ToString() == Message.caption.datatype.ToString()).Select(c => c.name).First().ToString()).Single().Cells["hand"].Value.ToString()) + 1).ToString();
                                        }
                                        ));

                                }
                                catch (Exception ww)
                                {
                                    if (!InfoMessage.Send(ww.Message, configuration.webservice.address, configuration.source.name))
                                        log.Write(System.DateTime.Now.ToString() + " веб сервис недоступен.\r\n");
                                    dataGridView1.Invoke(new MethodInvoker(() =>
                                            {
                                                dataGridView1.Rows.Cast<DataGridViewRow>().Where(u => u.Cells[0].Value.ToString() == Message.caption.source.ToString() && u.Cells[1].Value.ToString() == configuration.periods.Where(c => c.list_parperiod_id.ToString() == Message.caption.datatype.ToString()).Select(c => c.name).First().ToString()).Single().Cells["unknown"].Value =
                                                (int.Parse(dataGridView1.Rows.Cast<DataGridViewRow>().Where(u => u.Cells[0].Value.ToString() == Message.caption.source.ToString() && u.Cells[1].Value.ToString() == configuration.periods.Where(c => c.list_parperiod_id.ToString() == Message.caption.datatype.ToString()).Select(c => c.name).First().ToString()).Single().Cells["unknown"].Value.ToString()) + 1).ToString();
                                            })
                                        );
                                    textBox1.Invoke(new MethodInvoker(() =>
                                            {
                                                textBox1.SelectionStart = textBox1.Text.Length;
                                                textBox1.SelectionColor = Color.Red;
                                                textBox1.AppendText(System.DateTime.Now.ToString() + " " + ww.Message + "\r\n");
                                                log.Write(System.DateTime.Now.ToString() + " " + ww.Message + "\r\n");
                                            })
                                        );
                                    log.Write(System.DateTime.Now.ToString() + " " + ww.Message + "\r\n");
                                    log.Write(System.DateTime.Now.ToString() + " " + ww.Message + "\r\n");
                                }
                            });

                        if (!checkBox2.Checked)
                            File.Delete(p);
                    }
                    catch (Exception e11)
                    {
                        if (!InfoMessage.Send(e11.Message, configuration.webservice.address, configuration.source.name))
                            log.Write(System.DateTime.Now.ToString() + " веб сервис недоступен.\r\n");
                        textBox1.Invoke(
                            new MethodInvoker(() =>
                                {
                                    textBox1.SelectionStart = textBox1.Text.Length;
                                    textBox1.SelectionColor = Color.Red;
                                    textBox1.AppendText(System.DateTime.Now.ToString() + " " + e11.Message + "\r\n");
                                    log.Write(System.DateTime.Now.ToString() + " " + e11.Message + "\r\n");
                                })
                            );
                        log.Write(System.DateTime.Now.ToString() + " " + e11.Message + "\r\n");
                    }
                    progressBar1.Invoke(new MethodInvoker(() => { progressBar1.Value++; }));
                });
        }
        catch (Exception ee)
        {
            if (!InfoMessage.Send(ee.Message, configuration.webservice.address, configuration.source.name))
                log.Write(System.DateTime.Now.ToString() + " веб сервис недоступен.\r\n");
            textBox1.Invoke(new MethodInvoker(() =>
                    {
                        textBox1.SelectionStart = textBox1.Text.Length;
                        textBox1.SelectionColor = Color.Red;
                        textBox1.AppendText(System.DateTime.Now.ToString() + " " + ee.Message + "\r\n");
                    })
            );
            log.Write(System.DateTime.Now.ToString() + " " + ee.Message + "\r\n"); 
        }
    }

XmlSerializer:

public static class XmlSerializerHelper<T>
{
    public static Type _type = typeof(T);



    public static MemoryStream Save(object obj) {
        XmlSerializerNamespaces ns = new XmlSerializerNamespaces();
        ns.Add("", "");
        XmlSerializer serializer = new XmlSerializer(_type);
        var memstr = new MemoryStream();
        serializer.Serialize(memstr, obj,ns);
        return memstr;
    }
    public static void Save(string path, object obj)
    {
        XmlSerializerNamespaces ns = new XmlSerializerNamespaces();
        ns.Add("", "");
        using (TextWriter textWriter = new StreamWriter(path))
        {
            XmlSerializer serializer = new XmlSerializer(_type);
            serializer.Serialize(textWriter, obj, ns);
        }

    }

    public static T Read(string path)
    {
        T result;
        using (TextReader textReader = new StreamReader(path))
        {
            XmlSerializer deserializer = new XmlSerializer(_type);
            result = (T)deserializer.Deserialize(textReader);
        }
        return result;
    }
}

this stuff repeats every 10 second, by timer.
after 2 weaks of working this application have 150Mb of memory.
What can leak here?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T19:35:40+00:00Added an answer on May 23, 2026 at 7:35 pm

    after 2 weaks of working this application have 150Mb of memory.

    Nothing; that’s doing just fine. Memory is there to be used, and apps needn’t by overly shy about using it. It doesn’t sound like it is haemorrhaging memory; let it be.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm writing a WinForms application and one of the tabs in my TabControl has
In a winforms application i 'm storing one Wingdings char in a SQL Server
I have a DataGridView with one DataGridViewComboBoxColumn in my WinForms application. I need to
I've got a C# winforms application that runs in the background, listening for hotkeys
I've a legacy application using Asp.Net WebSite (winforms...) and I need run a background
I have a winforms application. I have a textbox on one form (call F1)
Once i launch a win forms application, i create a background worker to do
I have a c# .net 2.0 winforms application which consists basically of one form
I am writing a winforms application and one of the functions is to open
I'm trying to build a Winforms application with two COM components. However, one of

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.