0

How can I use backgroundworker in while loop where I use SqlDataReader ? I have a code where I compare the records in the remote database with the local database. If there is no match it is adding to the list of new records. If there is a match but the date_modified fields are not the same then it is adding to the list of update. My code is working fine but the since there are way too many records in the table sometimes my program hangs and I can not give any information to the user in UI. To do that I need to use backgroundworker. So how do I do that ?

My code is like this;

remote_query = "SELECT ID, DATE_MODIFIED FROM  REMOTE_TABLE";
remote_connect();
remote_command = new SqlCommand(remote_query, remote_coonnection);
SqlDataReader remote_reader = remote_command.ExecuteReader();
while (remote_reader.Read())
{
    local_query = "SELECT ID, DATE_MODIFIED FROM  LOCAL_TABLE WHERE ID=" + remote_reader[remote_reader.GetOrdinal("ID")].ToString();
    local_connect();
    local_command = new SqlCommand(local_query, poscon);
    SqlDataReader local_reader = local_command.ExecuteReader();
    if (local_reader.HasRows)
    {
        while (local_reader.Read())
        {
            if (remote_reader[remote_reader.GetOrdinal("DATE_MODIFIED")].ToString() != local_reader[local_reader.GetOrdinal("DATE_MODIFIED")].ToString())
            {
                string[] line = { remote_reader[remote_reader.GetOrdinal("ID")].ToString(), remote_reader[remote_reader.GetOrdinal("DEFINITION_")].ToString() };
                var item = new ListViewItem(line);
                list_of_to_be_updated.Items.Add(item);
            }
        }
    }
    else
    {
        string[] line = { remote_reader[remote_reader.GetOrdinal("ID")].ToString(), remote_reader[remote_reader.GetOrdinal("DEFINITION_")].ToString() };
        var item = new ListViewItem(line);
        list_of_to_be_inserted.Items.Add(item);
    }
}
  • There is lots of information available on how to use `BackgroundWorker` and other background-thread ways to implement work. **What have you tried?** See marked duplicates for examples of how to do work in a background thread. Take care to note that interaction with UI objects still has to happen on the UI thread; some of the answers in the duplicates explain how to deal with that as well. – Peter Duniho Mar 11 '20 at 16:28
  • I have tried whatever I could find on internet but they all working on iterations like autoincrement values and that lead me to here. However my ID is not like that. I have found a workaround like getting the count of the records and using the limit in my query to get iteration work. But I was not satisfied that's why I am looking for a different approach. Also I am newbie programmer I still don't know well about where to look. Thanks for leading me. – Mustafa Ozbalci Mar 12 '20 at 05:47

1 Answers1

-1

You can follow this to create a background task. https://learn.microsoft.com/en-us/aspnet/core/fundamentals/host/hosted-services?view=aspnetcore-3.1&tabs=visual-studio

You can use this to setup how many minutes to run.

_timer = new Timer(DoWork, null, TimeSpan.Zero, TimeSpan.FromSeconds(5));

Then do your work inside this

private void DoWork(object state)
{
    // Your task runs here ....
}
Alex - Tin Le
  • 1,982
  • 1
  • 6
  • 11