scrollbars of datagridview are blackened by backgroundWorker












0















I have c# WinForms application, it has some search parameters (TextBox and ComboBox) and a search Button, below this GroupBox are two DataGridView controls placed side by side. Then I have a PictureBox placed on top of the DataGridView controls whose Visible property I have set to False. Then I also have a BackgroundWorker. On button1_Click event, I am setting PictureBox's Visible property to Trueand calling backgroundWorker1.RunWorkerAsync();. On backgroundWorker1_DoWork(object sender, DoWorkEventArgs e), I am doing fetching records and populating DataGridView controls. On backgroundWorker1_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e), I am calling pictureBox3.Hide(); However, ever since I have added this loader functionality, the form remains unresponsive for some more seconds unlike the previous behavior and also the ScrollBars of the DataGridView controls are blackened. Like, literally blackened. Though, DataGridView controls are still scrollable. The form itself has DoubleBuffered set to True. Also, I have made the DataGridView controls DoubleBuffered via Reflection like this,



public static void DoubleBuffered(this DataGridView dgv, bool setting)
{
Type dgvType = dgv.GetType();
PropertyInfo pi = dgvType.GetProperty("DoubleBuffered",
BindingFlags.Instance | BindingFlags.NonPublic);
pi.SetValue(dgv, setting, null);
}


Please help me with the problematic (bold) part. Thank you.




EDIT - DoWork Code




private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
{
dataGridView1.DataSource = null;
dataGridView2.DataSource = null;
if (btn2d.Checked && btn3d.Checked)
{
searchdata1();
searchdata2();
}
if (!btn2d.Checked && !btn3d.Checked)
{
searchdata1();
searchdata2();
}
if (btn2d.Checked && !btn3d.Checked)
{
searchdata1();
}
if (!btn2d.Checked && btn3d.Checked)
{
searchdata2();
}
}









share|improve this question

























  • The problem is probably generated in the DoWork method. If you Invoke() the UI thread each time you add a row to the DGV, your method becomes, in a way, synchronous. Also, you can't update the UI from the DoWork method. Use the BackgroundWorker.ProgressChanged event, raised in the UI thread. You need to show how the code in the DoWork method operates, not how you set the DoubleBuffered property of a control (which, btw, works only if that control actually uses that property).

    – Jimi
    Nov 22 '18 at 14:02













  • Jimi, please check the updated question. Also, btw, DoubleBuffered does work alright.

    – user10516497
    Nov 26 '18 at 4:02











  • You must avoid referencing UI objects in the DoWork handler. No exceptions. Even the BGW itself. Cast sender to BackGroundWorker if you need to reference it. If you have to pass values to the DoWork method, use the object in RunWorkerAsync(object). This object will become the DoWorkEventArgs parameter. First thing, move out of DoWork the DataGridView.DataSource = null;. These need to be set before calling RunWorkerAsync. Use the ReportProgress method to perform any UI update (it will raise the ProgressChanged event, which is run on the UI thread).

    – Jimi
    Nov 26 '18 at 4:48













  • Can I see an example of what you are saying?

    – user10516497
    Nov 26 '18 at 5:35











  • You can find the same advices I gave you all over SO. I don't have a C# answer about the BackGroundWorker. It's not used that much in this language (Tasks/TPL are the major tools). I posted some in VB.Net. This one: Get File Size on FTP Server has a complete Form sample on PasteBin to test. It uses more or less all the features that I described here.

    – Jimi
    Nov 26 '18 at 16:15


















0















I have c# WinForms application, it has some search parameters (TextBox and ComboBox) and a search Button, below this GroupBox are two DataGridView controls placed side by side. Then I have a PictureBox placed on top of the DataGridView controls whose Visible property I have set to False. Then I also have a BackgroundWorker. On button1_Click event, I am setting PictureBox's Visible property to Trueand calling backgroundWorker1.RunWorkerAsync();. On backgroundWorker1_DoWork(object sender, DoWorkEventArgs e), I am doing fetching records and populating DataGridView controls. On backgroundWorker1_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e), I am calling pictureBox3.Hide(); However, ever since I have added this loader functionality, the form remains unresponsive for some more seconds unlike the previous behavior and also the ScrollBars of the DataGridView controls are blackened. Like, literally blackened. Though, DataGridView controls are still scrollable. The form itself has DoubleBuffered set to True. Also, I have made the DataGridView controls DoubleBuffered via Reflection like this,



public static void DoubleBuffered(this DataGridView dgv, bool setting)
{
Type dgvType = dgv.GetType();
PropertyInfo pi = dgvType.GetProperty("DoubleBuffered",
BindingFlags.Instance | BindingFlags.NonPublic);
pi.SetValue(dgv, setting, null);
}


Please help me with the problematic (bold) part. Thank you.




EDIT - DoWork Code




private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
{
dataGridView1.DataSource = null;
dataGridView2.DataSource = null;
if (btn2d.Checked && btn3d.Checked)
{
searchdata1();
searchdata2();
}
if (!btn2d.Checked && !btn3d.Checked)
{
searchdata1();
searchdata2();
}
if (btn2d.Checked && !btn3d.Checked)
{
searchdata1();
}
if (!btn2d.Checked && btn3d.Checked)
{
searchdata2();
}
}









share|improve this question

























  • The problem is probably generated in the DoWork method. If you Invoke() the UI thread each time you add a row to the DGV, your method becomes, in a way, synchronous. Also, you can't update the UI from the DoWork method. Use the BackgroundWorker.ProgressChanged event, raised in the UI thread. You need to show how the code in the DoWork method operates, not how you set the DoubleBuffered property of a control (which, btw, works only if that control actually uses that property).

    – Jimi
    Nov 22 '18 at 14:02













  • Jimi, please check the updated question. Also, btw, DoubleBuffered does work alright.

    – user10516497
    Nov 26 '18 at 4:02











  • You must avoid referencing UI objects in the DoWork handler. No exceptions. Even the BGW itself. Cast sender to BackGroundWorker if you need to reference it. If you have to pass values to the DoWork method, use the object in RunWorkerAsync(object). This object will become the DoWorkEventArgs parameter. First thing, move out of DoWork the DataGridView.DataSource = null;. These need to be set before calling RunWorkerAsync. Use the ReportProgress method to perform any UI update (it will raise the ProgressChanged event, which is run on the UI thread).

    – Jimi
    Nov 26 '18 at 4:48













  • Can I see an example of what you are saying?

    – user10516497
    Nov 26 '18 at 5:35











  • You can find the same advices I gave you all over SO. I don't have a C# answer about the BackGroundWorker. It's not used that much in this language (Tasks/TPL are the major tools). I posted some in VB.Net. This one: Get File Size on FTP Server has a complete Form sample on PasteBin to test. It uses more or less all the features that I described here.

    – Jimi
    Nov 26 '18 at 16:15
















0












0








0








I have c# WinForms application, it has some search parameters (TextBox and ComboBox) and a search Button, below this GroupBox are two DataGridView controls placed side by side. Then I have a PictureBox placed on top of the DataGridView controls whose Visible property I have set to False. Then I also have a BackgroundWorker. On button1_Click event, I am setting PictureBox's Visible property to Trueand calling backgroundWorker1.RunWorkerAsync();. On backgroundWorker1_DoWork(object sender, DoWorkEventArgs e), I am doing fetching records and populating DataGridView controls. On backgroundWorker1_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e), I am calling pictureBox3.Hide(); However, ever since I have added this loader functionality, the form remains unresponsive for some more seconds unlike the previous behavior and also the ScrollBars of the DataGridView controls are blackened. Like, literally blackened. Though, DataGridView controls are still scrollable. The form itself has DoubleBuffered set to True. Also, I have made the DataGridView controls DoubleBuffered via Reflection like this,



public static void DoubleBuffered(this DataGridView dgv, bool setting)
{
Type dgvType = dgv.GetType();
PropertyInfo pi = dgvType.GetProperty("DoubleBuffered",
BindingFlags.Instance | BindingFlags.NonPublic);
pi.SetValue(dgv, setting, null);
}


Please help me with the problematic (bold) part. Thank you.




EDIT - DoWork Code




private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
{
dataGridView1.DataSource = null;
dataGridView2.DataSource = null;
if (btn2d.Checked && btn3d.Checked)
{
searchdata1();
searchdata2();
}
if (!btn2d.Checked && !btn3d.Checked)
{
searchdata1();
searchdata2();
}
if (btn2d.Checked && !btn3d.Checked)
{
searchdata1();
}
if (!btn2d.Checked && btn3d.Checked)
{
searchdata2();
}
}









share|improve this question
















I have c# WinForms application, it has some search parameters (TextBox and ComboBox) and a search Button, below this GroupBox are two DataGridView controls placed side by side. Then I have a PictureBox placed on top of the DataGridView controls whose Visible property I have set to False. Then I also have a BackgroundWorker. On button1_Click event, I am setting PictureBox's Visible property to Trueand calling backgroundWorker1.RunWorkerAsync();. On backgroundWorker1_DoWork(object sender, DoWorkEventArgs e), I am doing fetching records and populating DataGridView controls. On backgroundWorker1_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e), I am calling pictureBox3.Hide(); However, ever since I have added this loader functionality, the form remains unresponsive for some more seconds unlike the previous behavior and also the ScrollBars of the DataGridView controls are blackened. Like, literally blackened. Though, DataGridView controls are still scrollable. The form itself has DoubleBuffered set to True. Also, I have made the DataGridView controls DoubleBuffered via Reflection like this,



public static void DoubleBuffered(this DataGridView dgv, bool setting)
{
Type dgvType = dgv.GetType();
PropertyInfo pi = dgvType.GetProperty("DoubleBuffered",
BindingFlags.Instance | BindingFlags.NonPublic);
pi.SetValue(dgv, setting, null);
}


Please help me with the problematic (bold) part. Thank you.




EDIT - DoWork Code




private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
{
dataGridView1.DataSource = null;
dataGridView2.DataSource = null;
if (btn2d.Checked && btn3d.Checked)
{
searchdata1();
searchdata2();
}
if (!btn2d.Checked && !btn3d.Checked)
{
searchdata1();
searchdata2();
}
if (btn2d.Checked && !btn3d.Checked)
{
searchdata1();
}
if (!btn2d.Checked && btn3d.Checked)
{
searchdata2();
}
}






c# winforms datagridview backgroundworker gdi






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 26 '18 at 6:22

























asked Nov 22 '18 at 6:38







user10516497




















  • The problem is probably generated in the DoWork method. If you Invoke() the UI thread each time you add a row to the DGV, your method becomes, in a way, synchronous. Also, you can't update the UI from the DoWork method. Use the BackgroundWorker.ProgressChanged event, raised in the UI thread. You need to show how the code in the DoWork method operates, not how you set the DoubleBuffered property of a control (which, btw, works only if that control actually uses that property).

    – Jimi
    Nov 22 '18 at 14:02













  • Jimi, please check the updated question. Also, btw, DoubleBuffered does work alright.

    – user10516497
    Nov 26 '18 at 4:02











  • You must avoid referencing UI objects in the DoWork handler. No exceptions. Even the BGW itself. Cast sender to BackGroundWorker if you need to reference it. If you have to pass values to the DoWork method, use the object in RunWorkerAsync(object). This object will become the DoWorkEventArgs parameter. First thing, move out of DoWork the DataGridView.DataSource = null;. These need to be set before calling RunWorkerAsync. Use the ReportProgress method to perform any UI update (it will raise the ProgressChanged event, which is run on the UI thread).

    – Jimi
    Nov 26 '18 at 4:48













  • Can I see an example of what you are saying?

    – user10516497
    Nov 26 '18 at 5:35











  • You can find the same advices I gave you all over SO. I don't have a C# answer about the BackGroundWorker. It's not used that much in this language (Tasks/TPL are the major tools). I posted some in VB.Net. This one: Get File Size on FTP Server has a complete Form sample on PasteBin to test. It uses more or less all the features that I described here.

    – Jimi
    Nov 26 '18 at 16:15





















  • The problem is probably generated in the DoWork method. If you Invoke() the UI thread each time you add a row to the DGV, your method becomes, in a way, synchronous. Also, you can't update the UI from the DoWork method. Use the BackgroundWorker.ProgressChanged event, raised in the UI thread. You need to show how the code in the DoWork method operates, not how you set the DoubleBuffered property of a control (which, btw, works only if that control actually uses that property).

    – Jimi
    Nov 22 '18 at 14:02













  • Jimi, please check the updated question. Also, btw, DoubleBuffered does work alright.

    – user10516497
    Nov 26 '18 at 4:02











  • You must avoid referencing UI objects in the DoWork handler. No exceptions. Even the BGW itself. Cast sender to BackGroundWorker if you need to reference it. If you have to pass values to the DoWork method, use the object in RunWorkerAsync(object). This object will become the DoWorkEventArgs parameter. First thing, move out of DoWork the DataGridView.DataSource = null;. These need to be set before calling RunWorkerAsync. Use the ReportProgress method to perform any UI update (it will raise the ProgressChanged event, which is run on the UI thread).

    – Jimi
    Nov 26 '18 at 4:48













  • Can I see an example of what you are saying?

    – user10516497
    Nov 26 '18 at 5:35











  • You can find the same advices I gave you all over SO. I don't have a C# answer about the BackGroundWorker. It's not used that much in this language (Tasks/TPL are the major tools). I posted some in VB.Net. This one: Get File Size on FTP Server has a complete Form sample on PasteBin to test. It uses more or less all the features that I described here.

    – Jimi
    Nov 26 '18 at 16:15



















The problem is probably generated in the DoWork method. If you Invoke() the UI thread each time you add a row to the DGV, your method becomes, in a way, synchronous. Also, you can't update the UI from the DoWork method. Use the BackgroundWorker.ProgressChanged event, raised in the UI thread. You need to show how the code in the DoWork method operates, not how you set the DoubleBuffered property of a control (which, btw, works only if that control actually uses that property).

– Jimi
Nov 22 '18 at 14:02







The problem is probably generated in the DoWork method. If you Invoke() the UI thread each time you add a row to the DGV, your method becomes, in a way, synchronous. Also, you can't update the UI from the DoWork method. Use the BackgroundWorker.ProgressChanged event, raised in the UI thread. You need to show how the code in the DoWork method operates, not how you set the DoubleBuffered property of a control (which, btw, works only if that control actually uses that property).

– Jimi
Nov 22 '18 at 14:02















Jimi, please check the updated question. Also, btw, DoubleBuffered does work alright.

– user10516497
Nov 26 '18 at 4:02





Jimi, please check the updated question. Also, btw, DoubleBuffered does work alright.

– user10516497
Nov 26 '18 at 4:02













You must avoid referencing UI objects in the DoWork handler. No exceptions. Even the BGW itself. Cast sender to BackGroundWorker if you need to reference it. If you have to pass values to the DoWork method, use the object in RunWorkerAsync(object). This object will become the DoWorkEventArgs parameter. First thing, move out of DoWork the DataGridView.DataSource = null;. These need to be set before calling RunWorkerAsync. Use the ReportProgress method to perform any UI update (it will raise the ProgressChanged event, which is run on the UI thread).

– Jimi
Nov 26 '18 at 4:48







You must avoid referencing UI objects in the DoWork handler. No exceptions. Even the BGW itself. Cast sender to BackGroundWorker if you need to reference it. If you have to pass values to the DoWork method, use the object in RunWorkerAsync(object). This object will become the DoWorkEventArgs parameter. First thing, move out of DoWork the DataGridView.DataSource = null;. These need to be set before calling RunWorkerAsync. Use the ReportProgress method to perform any UI update (it will raise the ProgressChanged event, which is run on the UI thread).

– Jimi
Nov 26 '18 at 4:48















Can I see an example of what you are saying?

– user10516497
Nov 26 '18 at 5:35





Can I see an example of what you are saying?

– user10516497
Nov 26 '18 at 5:35













You can find the same advices I gave you all over SO. I don't have a C# answer about the BackGroundWorker. It's not used that much in this language (Tasks/TPL are the major tools). I posted some in VB.Net. This one: Get File Size on FTP Server has a complete Form sample on PasteBin to test. It uses more or less all the features that I described here.

– Jimi
Nov 26 '18 at 16:15







You can find the same advices I gave you all over SO. I don't have a C# answer about the BackGroundWorker. It's not used that much in this language (Tasks/TPL are the major tools). I posted some in VB.Net. This one: Get File Size on FTP Server has a complete Form sample on PasteBin to test. It uses more or less all the features that I described here.

– Jimi
Nov 26 '18 at 16:15














0






active

oldest

votes











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53425165%2fscrollbars-of-datagridview-are-blackened-by-backgroundworker%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown
























0






active

oldest

votes








0






active

oldest

votes









active

oldest

votes






active

oldest

votes
















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53425165%2fscrollbars-of-datagridview-are-blackened-by-backgroundworker%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Create new schema in PostgreSQL using DBeaver

Deepest pit of an array with Javascript: test on Codility

Costa Masnaga