-2

I have the the following method to show in a gridview (dgJEARequests) a customized selection of items, as you can see from the images. The user will select the items that will send the parameters to the query whereStatement. But when many checkboxes (items) are marked or selected, it will take some time to load all the data in the dgJEARequests, let's say, 5000 records, or less. So, I decided to add a WaitProcessing (a form with a progress bar). When I click the search button btnSearchSelection, I get this error:

An exception of type 'System.InvalidOperationException' occurred in System.Windows.Forms.dll but was not handled in user code

Additional information: Cross-thread operation not valid: Control 'dgJEARequests' accessed from a thread other than the thread it was created on

        private void ShowOnlyCustomSelection()
    {
        String whereStatement = "";
        List<String> fieldList = new List<String>();
        string getStatusName = "";
        string _getStatusName = "";

        //loop through all the checkboxes
        for (int i = 0; i < count; i++)
        {
            if (_cbStatus[i].Checked)
            {
                //getStatusName should give all selected options like this: 'New', 'Started', 'Accepted', etc., then pass it to whereStatement
                _getStatusName += ("'" + _cbStatus[i].Text + "'" + ",").TrimEnd();
            }
        }

        //trims the last comma (,)
        getStatusName = _getStatusName.TrimEnd(',');
        //textBox1.Text = _getStatusName.TrimEnd(','); //--->>this is for testing

        ////////////
        if (getStatusName == "" || getStatusName == null)
        {
            {
                MessageBox.Show("You have not selected your filter(s)!", "Filter Result", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
            }
        }
        else
        {
            //Build WHERE Statement
            fieldList.Add("RequestStatus.StatusName IN (" + getStatusName + ")");

            if (fieldList.Count > 0)
            {
                for (int x = 0; x < fieldList.Count; x++)
                {
                    if (x == 0)
                    {
                        whereStatement = fieldList[x];
                    }
                    else
                    {
                        whereStatement = whereStatement + " AND " + fieldList[x];
                    }
                }

                //Seach for Requests
                jeaRequests = itServices.getRequestsBySQLStatement(whereStatement);

                //dgJEARequests.DataSource = jeaRequests;
                requestList = new List<ITWebService.Requests>(jeaRequests.ToList());
                dgJEARequests.DataSource = requestList;
                dgJEARequests.ClearSelection();

                lblCountOfRequests.Text = requestList.Count.ToString().ToString() + " requests loaded";

            }
            else
            {
                if (chkMine.Checked)
                {
                    jeaRequests = itServices.getRequestsByAssignedToAndStatusID(JEAUser.UserName, "0");
                    //dgJEARequests.DataSource = jeaRequests;
                    requestList = new List<ITWebService.Requests>(jeaRequests.ToList());
                    dgJEARequests.DataSource = requestList;
                    dgJEARequests.ClearSelection();
                }
                else
                {
                    jeaRequests = itServices.getRequestsBySQLStatement("Requests.ID > 0");
                    //dgJEARequests.DataSource = jeaRequests;
                    requestList = new List<ITWebService.Requests>(jeaRequests.ToList());
                    dgJEARequests.DataSource = requestList;
                    dgJEARequests.ClearSelection();
                }
            }
        }
    }

This is the code for the progressbar:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace JEAProjectManager
{
    public partial class WaitProcessing : Form
    {
        public Action Worker
        {
            get;
            set;
        }
        public WaitProcessing(Action worker)
        {
            InitializeComponent();

            if (worker == null)

                throw new ArgumentNullException();
            Worker = worker;

        }
        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);
            Task.Factory.StartNew(Worker).ContinueWith(t =>
            {
                this.Close();
            },
            TaskScheduler.FromCurrentSynchronizationContext());
        }
    }
}

This other is for when I click the search button:

private void btnSearchSelection_CheckedChanged(object sender, EventArgs e)
    {
        //ShowOnlyCustomSelection();
        //WaitProcessing processing = new WaitProcessing();
        using (WaitProcessing processing = new WaitProcessing(ShowOnlyCustomSelection))
        {
            processing.ShowDialog(this);
        }
    }

The query parameters are sent to a Webservice. What can I do to solve this problem? I am customizing this application. I am not the original developer, but it is my task to make it better. I know there are some similar errors out there, but different scenarios.

Screenshots:

Doing my selection

Selection

Passing parameters for query

Parameters

ProgressBar

ProgressBar

Error Message

Error

Mr. Munoz
  • 69
  • 1
  • 3
  • 13

2 Answers2

0

possible duplicate :-/ How to deal with cross-thread access exceptions?

you need to ensure, that the access thread is initialized

DotNetDev
  • 205
  • 1
  • 10
0

It looks like the answer was easy, but took me a while to figure it out. I am not sure if there are better solutions for this, but I had to enclose those components in lambda expression like:

lblCountOfRequests.Text = requestList.Count.ToString().ToString() + " requests loaded";

Or

dgJEARequests.Invoke(new Action(() => dgJEARequests.DataSource = requestList));
Mr. Munoz
  • 69
  • 1
  • 3
  • 13