scrollbars of datagridview are blackened by backgroundWorker
up vote
1
down vote
favorite
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 -
DoWorkCode
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
add a comment |
up vote
1
down vote
favorite
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 -
DoWorkCode
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
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 theDoubleBufferedproperty of a control (which, btw, works only if that control actually uses that property).
– Jimi
Nov 22 at 14:02
Jimi, please check the updated question. Also, btw, DoubleBuffered does work alright.
– fasih ullah khan
Nov 26 at 4:02
You must avoid referencing UI objects in theDoWorkhandler. No exceptions. Even theBGWitself. CastsendertoBackGroundWorkerif you need to reference it. If you have to pass values to theDoWorkmethod, use the object inRunWorkerAsync(object). This object will become theDoWorkEventArgsparameter. First thing, move out ofDoWorktheDataGridView.DataSource = null;. These need to be set before callingRunWorkerAsync. Use theReportProgressmethod to perform any UI update (it will raise theProgressChangedevent, which is run on the UI thread).
– Jimi
Nov 26 at 4:48
Can I see an example of what you are saying?
– fasih ullah khan
Nov 26 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 at 16:15
add a comment |
up vote
1
down vote
favorite
up vote
1
down vote
favorite
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 -
DoWorkCode
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
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 -
DoWorkCode
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
c# winforms datagridview backgroundworker gdi
edited Nov 26 at 6:22
asked Nov 22 at 6:38
fasih ullah khan
426
426
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 theDoubleBufferedproperty of a control (which, btw, works only if that control actually uses that property).
– Jimi
Nov 22 at 14:02
Jimi, please check the updated question. Also, btw, DoubleBuffered does work alright.
– fasih ullah khan
Nov 26 at 4:02
You must avoid referencing UI objects in theDoWorkhandler. No exceptions. Even theBGWitself. CastsendertoBackGroundWorkerif you need to reference it. If you have to pass values to theDoWorkmethod, use the object inRunWorkerAsync(object). This object will become theDoWorkEventArgsparameter. First thing, move out ofDoWorktheDataGridView.DataSource = null;. These need to be set before callingRunWorkerAsync. Use theReportProgressmethod to perform any UI update (it will raise theProgressChangedevent, which is run on the UI thread).
– Jimi
Nov 26 at 4:48
Can I see an example of what you are saying?
– fasih ullah khan
Nov 26 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 at 16:15
add a comment |
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 theDoubleBufferedproperty of a control (which, btw, works only if that control actually uses that property).
– Jimi
Nov 22 at 14:02
Jimi, please check the updated question. Also, btw, DoubleBuffered does work alright.
– fasih ullah khan
Nov 26 at 4:02
You must avoid referencing UI objects in theDoWorkhandler. No exceptions. Even theBGWitself. CastsendertoBackGroundWorkerif you need to reference it. If you have to pass values to theDoWorkmethod, use the object inRunWorkerAsync(object). This object will become theDoWorkEventArgsparameter. First thing, move out ofDoWorktheDataGridView.DataSource = null;. These need to be set before callingRunWorkerAsync. Use theReportProgressmethod to perform any UI update (it will raise theProgressChangedevent, which is run on the UI thread).
– Jimi
Nov 26 at 4:48
Can I see an example of what you are saying?
– fasih ullah khan
Nov 26 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 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 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 at 14:02
Jimi, please check the updated question. Also, btw, DoubleBuffered does work alright.
– fasih ullah khan
Nov 26 at 4:02
Jimi, please check the updated question. Also, btw, DoubleBuffered does work alright.
– fasih ullah khan
Nov 26 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 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 at 4:48
Can I see an example of what you are saying?
– fasih ullah khan
Nov 26 at 5:35
Can I see an example of what you are saying?
– fasih ullah khan
Nov 26 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 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 at 16:15
add a comment |
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',
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
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
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.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- 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.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
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
DoubleBufferedproperty of a control (which, btw, works only if that control actually uses that property).– Jimi
Nov 22 at 14:02
Jimi, please check the updated question. Also, btw, DoubleBuffered does work alright.
– fasih ullah khan
Nov 26 at 4:02
You must avoid referencing UI objects in the
DoWorkhandler. No exceptions. Even theBGWitself. CastsendertoBackGroundWorkerif you need to reference it. If you have to pass values to theDoWorkmethod, use the object inRunWorkerAsync(object). This object will become theDoWorkEventArgsparameter. First thing, move out ofDoWorktheDataGridView.DataSource = null;. These need to be set before callingRunWorkerAsync. Use theReportProgressmethod to perform any UI update (it will raise theProgressChangedevent, which is run on the UI thread).– Jimi
Nov 26 at 4:48
Can I see an example of what you are saying?
– fasih ullah khan
Nov 26 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 at 16:15