Best way to initiate long time tasks in background | C#

Sorry in advance if a very simple or confusing question!

I'm writing a program (C# Net.3.5 / WinForms) that sends data to the device. The device receives the data and returns a 0 or -1 response. I'm unsure of the right way to apply it to my situation. No matter the variation that I try my UI still hangs because I don't seem to be calling my desired function asynchronously, additionally, I may in fact need threading for my solution. Now I have large files where data (more then 10k lines) is stored and I need to send them without freezing the uninstalled program. When the task is completed, the massage box is displayed.

I have the following code that works without errors:

//tranfer data to device
    public static int UploadDataToPlu(string DeviceURL)
    {
        try
        {
            //listDevice
            string path = DeviceURL;
            
            // This text is added only once to the file.
            if (File.Exists(path))
            {
                //if file exits and not null
                var arrayList = FileHelper.ReadAllLines(path).Where(x => !FileHelper.IsNullOrWhiteSpace(x)).ToList();
                int len = arrayList.Count() / 4;
                if (arrayList.Count() % 4 != 0) len += 1;
                int d = 0;
                if (arrayList.Count >= 1)
                {
                    if (d < arrayList.Count())
                    {
                        for (int J = 0; J < len; J++)
                        {
                            PLU[] plu = new PLU[4];

                            for (int i = 0; i < 4; i++)
                            {
                                if (d >= arrayList.Count()) d -= 1;

                                string getValues = arrayList[d].ToString();

                                string[] getDvsName;
                                if (getValues.Split('\t').Length >= 4)
                                {
                                    getDvsName = getValues.Split('\t');
                                    plu[i].Name = getDvsName[1].ToString();
                                    //...
                                    d++;
                                }
                                else
                                {
                                    getDvsName = getValues.Split(';');
                                    plu[i].Name = getDvsName[1].ToString();
                                    //...
                                    d++;
                                }
                            }
                            if (rtsdrv.rtscaleTransferPLUCluster(plu) == 0) { }
                        }
                    }
                    MessageBox.Show("Uploaded ok ");
                }
            }
           
        }
        catch (Exception exr)
        {
            MessageBox.Show("Error: \n '"+ exr.Message + "', \n '"+ exr.ToString() + "'");
        }
        return 0;
    }

And I cell to funtion as follows:

private void btnUpload_Click(object sender, EventArgs e)
    {
        string IpAddr = ExtraDeviceData.DeviceIpAddress;
        string DeviceURL = ExtraDeviceData.DownloadPathChekcedStatusUrl;
        if (DeviceConnection.ConnectScale(IpAddr))
            DownloadData.UploadDataToPlu(DeviceURL);
    }

The program is currently running. But when the data is large, the program freezes. I found some solutions such as Async/Await and Threading, but I'm still ensure which is better for my probblem.

I also saw BackgroundWorker, but when I applied the function in DoWorker, no work was done on another thread:

protected void myWorker_DoWork(object sender, DoWorkEventArgs e)
    {
        DownloadData.UploadData);
    }

Q: How can I perform processes by applying a function (UploadDataToPlu) without having a freeze the program? P.S: I refer to UploadDataToPlu in several places. That’s why I made this process a separate function. Thanks!



Read more here: https://stackoverflow.com/questions/64941351/best-way-to-initiate-long-time-tasks-in-background-c-sharp

Content Attribution

This content was originally published by MinorDestroyer at Recent Questions - Stack Overflow, and is syndicated here via their RSS feed. You can read the original post over there.

%d bloggers like this: