2

I have a problem, with a DataGridView which get data from another thread. I'm not the best in english, so here is the code:

private void TaskLoad()
{
    List<taskafterdb> tasklist = new List<taskafterdb>();
    this.tasklistTableAdapter.FillTaskBoard(sHADOWv5_testDataSet.tasklist)
    var s = from x in sHADOWv5_testDataSet.tasklist
            orderby x.DATE ascending
            select x;
    dgv_tasks.DataSource = s.AsDataView();
    foreach (var task in sHADOWv5_testDataSet.tasklist)
    {
                tasklist.Add(new taskafterdb { DATE = task.DATE, COLOR = task.COLOR }); 
    }  
    tasklist = tasklist.OrderBy(t => t.DATE).ToList();
    dgv_tasks.DataSource = tasklist;
    foreach (DataGridViewColumn column in dgv_tasks.Columns)
    {
        column.Frozen = false;
    }
    dgv_tasks.Columns["TASK"].AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill;
    dgv_tasks.AutoSizeRowsMode = DataGridViewAutoSizeRowsMode.DisplayedCells;
    dgv_tasks.DefaultCellStyle.WrapMode = DataGridViewTriState.True;
    dgv_tasks.ScrollBars = ScrollBars.Both;
    dgv_tasks.Controls[0].Enabled = true;
    dgv_tasks.Controls[1].Enabled = true;
}

This is the TaskLoad() method, which fills the DGV with rows.

If I put this method to the main thread eg.: Form1_Load, the scrollbars are working perfectly. The big problem is that I should refresh the data from another thread:

    static public Thread dohardwork;
    public void Form1_Load(object sender, EventArgs e)
    {
        dohardwork = new Thread(hardwork);
        dohardwork.Start();
    }

    private void hardwork()
    {
        TaskLolad();
        Form1_Resize(this, null);
        IsHardworkDone = true;
        fadeintimer.Enabled = true;
        dohardwork.Abort();
    }

And then, the scrollbars are gone... There is a little space for it, but nothing more. I can scroll with mouse, or with arrow keys, but nothing more...

Thank you for any kind of help! :)

RAPTOR
  • 124
  • 1
  • 11
  • 1
    In general, if you have a background thread, the code in that thread should never interface with the UI thread. Setting the DataSource and calling Form1_Resize are obvious UI calls, so that would need to be refactored. That `Abort()` on your thread is a code smell: see [What's wrong with using Thread.Abort()](http://stackoverflow.com/q/1559255/719186) – LarsTech Dec 22 '14 at 14:39
  • Thank you for warn me! In this case it's doesn't matter, because of the IsHardworkDone boolean, but you forced me to rethink my code in other cases... Thanks again! :) – RAPTOR Dec 24 '14 at 19:50

1 Answers1

1

Here is what you need to do in order to improve performace and fix your issue

  1. Refactor your TaskLoad method into 2 seperate methods. The first called GetTasks, which only fetch data from your database and nothing more. The second one called ShowTasks responsible for loading your tasks into the datagrid.

  2. Your thread should call GetTasks only, upon receiving the result, use the BeginInvoke method of your DataGrid to run ShowTasks in the main thread passing it the result.

I'm writing this on the phone so can't show you a sample code. Let me know if you're still stuck and i'll show you some sample code later.

Tien Dinh
  • 1,037
  • 7
  • 12
  • Thank you, it's better, but still not the best... I will improve the code based on your instructions, and if it works, i'm going to mark your's as an answer! Thank you, and happy Xmas-coding :) – RAPTOR Dec 24 '14 at 19:46
  • Okay, it's not so important now... I solved the GUI glitch using `.visible` during the fade-in time. I'm sure that your solution is perfect just my code is too complicated, so i'll accept this as answer. Thank you! – RAPTOR Dec 25 '14 at 09:21