How To Run A BackGroundWorker And Also Append A Text To A RichTextBox Control From A Delegate That Get Values From Parent Form In C#Return multiple values to a method callerGet int value from enum in C#How to get the IP address of the server on which my C# application is running on?Get property value from string using reflection in C#How to get C# Enum description from value?How to return a value from a Form in C#?WPF selected value , displaymember path inconsistency with listbox and combo boxHow do I get the last four characters from a string in C#?Binary Serialization of an Observable Collection — Saving, LoadingOleDbCommand select does not return expected rows
Book where the stars go black due to aliens stopping human observation collapsing quantum possibilities
Is there any word for "disobedience to God"?
Who are "them" and "the brothers" in Acts 22:5?
What's the minimum number of sensors for a hobby GPS waypoint-following UAV?
Novel where a group of scientists in a spaceship encounter various aliens
Machine learning and operations research projects
Should disabled buttons give feedback when clicked?
Single word for "refusing to move to next activity unless present one is completed."
Robbers: The Hidden OEIS Substring
What species of wasp is this? And how to get rid of them?
Why was hardware diversification an asset for the IBM PC ecosystem?
How can one write good dialogue in a story without sounding wooden?
Reducing using/foreach/using nesting with a helper extension
CentOS 7 -> find: missing Argument for "-exec"
How can I effectively communicate to recruiters that a phone call is not possible?
Why didn't Thanos kill all the Dwarves on Nidavellir?
Why are they 'nude photos'?
How did the hit man miss?
Keep milk (or milk alternative) for a day without a fridge
Matchmaker, Matchmaker, make me a match
What was the definition of "set" that resulted in Russell's Paradox
How to (graphically) present computational results?
ESTA: "Is your travel to the US occurring in transit to another country?" when going on a cruise
As the Dungeon Master, how do I handle a player that insists on a specific class when I already know that choice will cause issues?
How To Run A BackGroundWorker And Also Append A Text To A RichTextBox Control From A Delegate That Get Values From Parent Form In C#
Return multiple values to a method callerGet int value from enum in C#How to get the IP address of the server on which my C# application is running on?Get property value from string using reflection in C#How to get C# Enum description from value?How to return a value from a Form in C#?WPF selected value , displaymember path inconsistency with listbox and combo boxHow do I get the last four characters from a string in C#?Binary Serialization of an Observable Collection — Saving, LoadingOleDbCommand select does not return expected rows
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;
I have a parent form that send a data table to a child form by Delegate.
the Delegate is executed and i have also gotten the the table on the child side.
i want to append a text into my richtextbox control to announce the user what is going on and then run a backgroundworker. but i get the STAThread Exception. i know some thing about Invoke(Delegate) and about single-Thread but i do not know how can i overcome to this cross-threading. Any help is appreciated.
The codes from Appent To RichTextBox are not execute with debugging (i know it is possible with run of the *.exe file).
//What i am doing and trying:(SetDaTableAndFileNameFn is my received Delegate)
public void SetDaTableAndFileNameFn(System.Data.DataTable DataTable)
//Test The Parent Has Sent And Child Has Received.
MessageBox.Show("Ruger Parent...");
dt.Clear();
dt = DataTable;
//Check whether My dt Filled Correctly.
MessageBox.Show(dt.Rows[2][2].ToString());
richTxtBxExprtr.AppendText(">>> Creating And Transferring Data To The File...");
//BGWorker.
bGWExprtrLod.WorkerReportsProgress = true;
bGWExprtrLod.RunWorkerAsync();
private void ExportToTxtIrrigularly(System.Data.DataTable DataTable)
// Using Microsoft.Office.Interop.Word.Application to export datatable.
private void xBtnExprt_Click(object sender, EventArgs e)
SaveFileDialog svFDialXls = new SaveFileDialog();
svFDialXls.Filter = "Plain text(*.txt)
private void bGWExprtrLod_DoWork(object sender, DoWorkEventArgs e)
this.Invoke(new Action(() =>
richTxtBxExprtr.AppendText(">>> Start Processing...n>>> Copying Data Take A Little Time.n>>> Be Patient...n>>> Loadind Data...n-----------------------------------------------n");
ExportToTxtIrrigularly(dt);
));
private void bGWExprtrLod_ProgressChanged(object sender, ProgressChangedEventArgs e)
this.Invoke(new Action(() =>richTxtBxExprtr.AppendText(">>> Line NO. [" + e.ProgressPercentage.ToString() + "] Is In Progress...n");
richTxtBxExprtr.ScrollToCaret();
));
private void bGWExprtrLod_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
this.Invoke(new Action(() => richTxtBxExprtr.AppendText(">>> The Process Is Completed Successfuly.n"); ));
the executed mthod in the BGWorker will open a savedialogbox() and then export my datatable to a text format using
Microsoft.Office.Interop.Word.Application
c# windows
add a comment |
I have a parent form that send a data table to a child form by Delegate.
the Delegate is executed and i have also gotten the the table on the child side.
i want to append a text into my richtextbox control to announce the user what is going on and then run a backgroundworker. but i get the STAThread Exception. i know some thing about Invoke(Delegate) and about single-Thread but i do not know how can i overcome to this cross-threading. Any help is appreciated.
The codes from Appent To RichTextBox are not execute with debugging (i know it is possible with run of the *.exe file).
//What i am doing and trying:(SetDaTableAndFileNameFn is my received Delegate)
public void SetDaTableAndFileNameFn(System.Data.DataTable DataTable)
//Test The Parent Has Sent And Child Has Received.
MessageBox.Show("Ruger Parent...");
dt.Clear();
dt = DataTable;
//Check whether My dt Filled Correctly.
MessageBox.Show(dt.Rows[2][2].ToString());
richTxtBxExprtr.AppendText(">>> Creating And Transferring Data To The File...");
//BGWorker.
bGWExprtrLod.WorkerReportsProgress = true;
bGWExprtrLod.RunWorkerAsync();
private void ExportToTxtIrrigularly(System.Data.DataTable DataTable)
// Using Microsoft.Office.Interop.Word.Application to export datatable.
private void xBtnExprt_Click(object sender, EventArgs e)
SaveFileDialog svFDialXls = new SaveFileDialog();
svFDialXls.Filter = "Plain text(*.txt)
private void bGWExprtrLod_DoWork(object sender, DoWorkEventArgs e)
this.Invoke(new Action(() =>
richTxtBxExprtr.AppendText(">>> Start Processing...n>>> Copying Data Take A Little Time.n>>> Be Patient...n>>> Loadind Data...n-----------------------------------------------n");
ExportToTxtIrrigularly(dt);
));
private void bGWExprtrLod_ProgressChanged(object sender, ProgressChangedEventArgs e)
this.Invoke(new Action(() =>richTxtBxExprtr.AppendText(">>> Line NO. [" + e.ProgressPercentage.ToString() + "] Is In Progress...n");
richTxtBxExprtr.ScrollToCaret();
));
private void bGWExprtrLod_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
this.Invoke(new Action(() => richTxtBxExprtr.AppendText(">>> The Process Is Completed Successfuly.n"); ));
the executed mthod in the BGWorker will open a savedialogbox() and then export my datatable to a text format using
Microsoft.Office.Interop.Word.Application
c# windows
Since you have a BackgroundWorker(), use that progress event to update the RichTextBox instead of trying to do it directly...
– Idle_Mind
Mar 26 at 4:38
Thanks for your comment. If I do this way, i will got a cross_threading again!
– Ehsan
Mar 26 at 7:11
How can i invoke RichTextBox.Appen() from BGWorker_DoWork,BGWorker_ProgressChanged and BGWorker_DoWork and BGWorker_RunWorkerCompleted without cross-Threading?
– Ehsan
Mar 26 at 7:21
i have solved BGWorker_DoWorker with this.Invoke(new Action(() =>, but it does not work for in BGWorker_ProgressChanged, BGWorker_RunWorkerCompleted, is there any way to overcome?
– Ehsan
Mar 26 at 13:39
That DataTable parameter is pretty fishy, good odds that this code already runs on a worker thread. BackgroundWorker can only properly marshal to the UI thread when you call its RunWorkerAsync() method from the UI thread. Verify this with the debugger, set a breakpoint on the method and when it hits use Debug > Windows > Threads. With the expectation that you now discover that you don't need a BGW at all. Or need to make it smarter by also generating the DataTable.
– Hans Passant
Mar 26 at 14:40
add a comment |
I have a parent form that send a data table to a child form by Delegate.
the Delegate is executed and i have also gotten the the table on the child side.
i want to append a text into my richtextbox control to announce the user what is going on and then run a backgroundworker. but i get the STAThread Exception. i know some thing about Invoke(Delegate) and about single-Thread but i do not know how can i overcome to this cross-threading. Any help is appreciated.
The codes from Appent To RichTextBox are not execute with debugging (i know it is possible with run of the *.exe file).
//What i am doing and trying:(SetDaTableAndFileNameFn is my received Delegate)
public void SetDaTableAndFileNameFn(System.Data.DataTable DataTable)
//Test The Parent Has Sent And Child Has Received.
MessageBox.Show("Ruger Parent...");
dt.Clear();
dt = DataTable;
//Check whether My dt Filled Correctly.
MessageBox.Show(dt.Rows[2][2].ToString());
richTxtBxExprtr.AppendText(">>> Creating And Transferring Data To The File...");
//BGWorker.
bGWExprtrLod.WorkerReportsProgress = true;
bGWExprtrLod.RunWorkerAsync();
private void ExportToTxtIrrigularly(System.Data.DataTable DataTable)
// Using Microsoft.Office.Interop.Word.Application to export datatable.
private void xBtnExprt_Click(object sender, EventArgs e)
SaveFileDialog svFDialXls = new SaveFileDialog();
svFDialXls.Filter = "Plain text(*.txt)
private void bGWExprtrLod_DoWork(object sender, DoWorkEventArgs e)
this.Invoke(new Action(() =>
richTxtBxExprtr.AppendText(">>> Start Processing...n>>> Copying Data Take A Little Time.n>>> Be Patient...n>>> Loadind Data...n-----------------------------------------------n");
ExportToTxtIrrigularly(dt);
));
private void bGWExprtrLod_ProgressChanged(object sender, ProgressChangedEventArgs e)
this.Invoke(new Action(() =>richTxtBxExprtr.AppendText(">>> Line NO. [" + e.ProgressPercentage.ToString() + "] Is In Progress...n");
richTxtBxExprtr.ScrollToCaret();
));
private void bGWExprtrLod_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
this.Invoke(new Action(() => richTxtBxExprtr.AppendText(">>> The Process Is Completed Successfuly.n"); ));
the executed mthod in the BGWorker will open a savedialogbox() and then export my datatable to a text format using
Microsoft.Office.Interop.Word.Application
c# windows
I have a parent form that send a data table to a child form by Delegate.
the Delegate is executed and i have also gotten the the table on the child side.
i want to append a text into my richtextbox control to announce the user what is going on and then run a backgroundworker. but i get the STAThread Exception. i know some thing about Invoke(Delegate) and about single-Thread but i do not know how can i overcome to this cross-threading. Any help is appreciated.
The codes from Appent To RichTextBox are not execute with debugging (i know it is possible with run of the *.exe file).
//What i am doing and trying:(SetDaTableAndFileNameFn is my received Delegate)
public void SetDaTableAndFileNameFn(System.Data.DataTable DataTable)
//Test The Parent Has Sent And Child Has Received.
MessageBox.Show("Ruger Parent...");
dt.Clear();
dt = DataTable;
//Check whether My dt Filled Correctly.
MessageBox.Show(dt.Rows[2][2].ToString());
richTxtBxExprtr.AppendText(">>> Creating And Transferring Data To The File...");
//BGWorker.
bGWExprtrLod.WorkerReportsProgress = true;
bGWExprtrLod.RunWorkerAsync();
private void ExportToTxtIrrigularly(System.Data.DataTable DataTable)
// Using Microsoft.Office.Interop.Word.Application to export datatable.
private void xBtnExprt_Click(object sender, EventArgs e)
SaveFileDialog svFDialXls = new SaveFileDialog();
svFDialXls.Filter = "Plain text(*.txt)
private void bGWExprtrLod_DoWork(object sender, DoWorkEventArgs e)
this.Invoke(new Action(() =>
richTxtBxExprtr.AppendText(">>> Start Processing...n>>> Copying Data Take A Little Time.n>>> Be Patient...n>>> Loadind Data...n-----------------------------------------------n");
ExportToTxtIrrigularly(dt);
));
private void bGWExprtrLod_ProgressChanged(object sender, ProgressChangedEventArgs e)
this.Invoke(new Action(() =>richTxtBxExprtr.AppendText(">>> Line NO. [" + e.ProgressPercentage.ToString() + "] Is In Progress...n");
richTxtBxExprtr.ScrollToCaret();
));
private void bGWExprtrLod_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
this.Invoke(new Action(() => richTxtBxExprtr.AppendText(">>> The Process Is Completed Successfuly.n"); ));
the executed mthod in the BGWorker will open a savedialogbox() and then export my datatable to a text format using
Microsoft.Office.Interop.Word.Application
c# windows
c# windows
edited Mar 28 at 5:09
Ehsan
asked Mar 26 at 3:18
EhsanEhsan
35 bronze badges
35 bronze badges
Since you have a BackgroundWorker(), use that progress event to update the RichTextBox instead of trying to do it directly...
– Idle_Mind
Mar 26 at 4:38
Thanks for your comment. If I do this way, i will got a cross_threading again!
– Ehsan
Mar 26 at 7:11
How can i invoke RichTextBox.Appen() from BGWorker_DoWork,BGWorker_ProgressChanged and BGWorker_DoWork and BGWorker_RunWorkerCompleted without cross-Threading?
– Ehsan
Mar 26 at 7:21
i have solved BGWorker_DoWorker with this.Invoke(new Action(() =>, but it does not work for in BGWorker_ProgressChanged, BGWorker_RunWorkerCompleted, is there any way to overcome?
– Ehsan
Mar 26 at 13:39
That DataTable parameter is pretty fishy, good odds that this code already runs on a worker thread. BackgroundWorker can only properly marshal to the UI thread when you call its RunWorkerAsync() method from the UI thread. Verify this with the debugger, set a breakpoint on the method and when it hits use Debug > Windows > Threads. With the expectation that you now discover that you don't need a BGW at all. Or need to make it smarter by also generating the DataTable.
– Hans Passant
Mar 26 at 14:40
add a comment |
Since you have a BackgroundWorker(), use that progress event to update the RichTextBox instead of trying to do it directly...
– Idle_Mind
Mar 26 at 4:38
Thanks for your comment. If I do this way, i will got a cross_threading again!
– Ehsan
Mar 26 at 7:11
How can i invoke RichTextBox.Appen() from BGWorker_DoWork,BGWorker_ProgressChanged and BGWorker_DoWork and BGWorker_RunWorkerCompleted without cross-Threading?
– Ehsan
Mar 26 at 7:21
i have solved BGWorker_DoWorker with this.Invoke(new Action(() =>, but it does not work for in BGWorker_ProgressChanged, BGWorker_RunWorkerCompleted, is there any way to overcome?
– Ehsan
Mar 26 at 13:39
That DataTable parameter is pretty fishy, good odds that this code already runs on a worker thread. BackgroundWorker can only properly marshal to the UI thread when you call its RunWorkerAsync() method from the UI thread. Verify this with the debugger, set a breakpoint on the method and when it hits use Debug > Windows > Threads. With the expectation that you now discover that you don't need a BGW at all. Or need to make it smarter by also generating the DataTable.
– Hans Passant
Mar 26 at 14:40
Since you have a BackgroundWorker(), use that progress event to update the RichTextBox instead of trying to do it directly...
– Idle_Mind
Mar 26 at 4:38
Since you have a BackgroundWorker(), use that progress event to update the RichTextBox instead of trying to do it directly...
– Idle_Mind
Mar 26 at 4:38
Thanks for your comment. If I do this way, i will got a cross_threading again!
– Ehsan
Mar 26 at 7:11
Thanks for your comment. If I do this way, i will got a cross_threading again!
– Ehsan
Mar 26 at 7:11
How can i invoke RichTextBox.Appen() from BGWorker_DoWork,BGWorker_ProgressChanged and BGWorker_DoWork and BGWorker_RunWorkerCompleted without cross-Threading?
– Ehsan
Mar 26 at 7:21
How can i invoke RichTextBox.Appen() from BGWorker_DoWork,BGWorker_ProgressChanged and BGWorker_DoWork and BGWorker_RunWorkerCompleted without cross-Threading?
– Ehsan
Mar 26 at 7:21
i have solved BGWorker_DoWorker with this.Invoke(new Action(() =>, but it does not work for in BGWorker_ProgressChanged, BGWorker_RunWorkerCompleted, is there any way to overcome?
– Ehsan
Mar 26 at 13:39
i have solved BGWorker_DoWorker with this.Invoke(new Action(() =>, but it does not work for in BGWorker_ProgressChanged, BGWorker_RunWorkerCompleted, is there any way to overcome?
– Ehsan
Mar 26 at 13:39
That DataTable parameter is pretty fishy, good odds that this code already runs on a worker thread. BackgroundWorker can only properly marshal to the UI thread when you call its RunWorkerAsync() method from the UI thread. Verify this with the debugger, set a breakpoint on the method and when it hits use Debug > Windows > Threads. With the expectation that you now discover that you don't need a BGW at all. Or need to make it smarter by also generating the DataTable.
– Hans Passant
Mar 26 at 14:40
That DataTable parameter is pretty fishy, good odds that this code already runs on a worker thread. BackgroundWorker can only properly marshal to the UI thread when you call its RunWorkerAsync() method from the UI thread. Verify this with the debugger, set a breakpoint on the method and when it hits use Debug > Windows > Threads. With the expectation that you now discover that you don't need a BGW at all. Or need to make it smarter by also generating the DataTable.
– Hans Passant
Mar 26 at 14:40
add a comment |
1 Answer
1
active
oldest
votes
Not really sure what kind of setup you've got, but it will look something like this:
private BackgroundWorker bGWExprtrLod;
private void Form1_Load(object sender, EventArgs e)
bGWExprtrLod = new BackgroundWorker();
bGWExprtrLod.WorkerReportsProgress = true;
bGWExprtrLod.ProgressChanged += BGWExprtrLod_ProgressChanged;
bGWExprtrLod.RunWorkerCompleted += BGWExprtrLod_RunWorkerCompleted;
bGWExprtrLod.DoWork += BGWExprtrLod_DoWork;
public void SetDaTableAndFileNameFn(System.Data.DataTable DataTable)
// ... other code ...
bGWExprtrLod.RunWorkerAsync();
private void BGWExprtrLod_DoWork(object sender, DoWorkEventArgs e)
bGWExprtrLod.ReportProgress(0, ">>> Creating And Transferring Data To The File...");
// ... do some work ...
private void BGWExprtrLod_ProgressChanged(object sender, ProgressChangedEventArgs e)
string msg = e.UserState.ToString();
richTxtBxExprtr.AppendText(msg);
private void BGWExprtrLod_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
richTxtBxExprtr.AppendText("Transfer Complete!");
** EDIT **
Start by moving the call to you export method outside the Invoke() call:
private void bGWExprtrLod_DoWork(object sender, DoWorkEventArgs e)
this.Invoke(new Action(() =>
richTxtBxExprtr.AppendText(">>> Start Processing...n>>> Copying Data Take A Little Time.n>>> Be Patient...n>>> Loadind Data...n-----------------------------------------------n");
));
ExportToTxtIrrigularly(dt);
I have added BGW component in my form and i did the same thing (codes), but the error has raise again in the BGWExprtrLod_ProgressChanged eventhandler.
– Ehsan
Mar 27 at 6:18
I have done it with the use of this.Invoke(new Action(() =>, but my application freezes until the end of the export method. i have used from BGWorker to prevent from freezing!!!! Is there any point??? Any Guide???
– Ehsan
Mar 27 at 13:06
It sounds like you're using invoke in DoWork, which is pointless. Post more complete code (edit your post) so we can advise how to fix it.
– Idle_Mind
Mar 27 at 13:24
Thanks for your help, i have edited my codes.
– Ehsan
Mar 28 at 4:15
Idle Mind thanks for your perusing. Unfortunately, outside of this.Invoke()=> , ExportToTxtIrrigularly(dt); does not execute!!!! probably this is because of BGW_progressChanged(). if i do this way, i will get the response of the BGW_Completed(). Is Any Other Way?????
– Ehsan
Mar 28 at 12:58
|
show 4 more comments
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
);
);
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%2f55349350%2fhow-to-run-a-backgroundworker-and-also-append-a-text-to-a-richtextbox-control-fr%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
Not really sure what kind of setup you've got, but it will look something like this:
private BackgroundWorker bGWExprtrLod;
private void Form1_Load(object sender, EventArgs e)
bGWExprtrLod = new BackgroundWorker();
bGWExprtrLod.WorkerReportsProgress = true;
bGWExprtrLod.ProgressChanged += BGWExprtrLod_ProgressChanged;
bGWExprtrLod.RunWorkerCompleted += BGWExprtrLod_RunWorkerCompleted;
bGWExprtrLod.DoWork += BGWExprtrLod_DoWork;
public void SetDaTableAndFileNameFn(System.Data.DataTable DataTable)
// ... other code ...
bGWExprtrLod.RunWorkerAsync();
private void BGWExprtrLod_DoWork(object sender, DoWorkEventArgs e)
bGWExprtrLod.ReportProgress(0, ">>> Creating And Transferring Data To The File...");
// ... do some work ...
private void BGWExprtrLod_ProgressChanged(object sender, ProgressChangedEventArgs e)
string msg = e.UserState.ToString();
richTxtBxExprtr.AppendText(msg);
private void BGWExprtrLod_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
richTxtBxExprtr.AppendText("Transfer Complete!");
** EDIT **
Start by moving the call to you export method outside the Invoke() call:
private void bGWExprtrLod_DoWork(object sender, DoWorkEventArgs e)
this.Invoke(new Action(() =>
richTxtBxExprtr.AppendText(">>> Start Processing...n>>> Copying Data Take A Little Time.n>>> Be Patient...n>>> Loadind Data...n-----------------------------------------------n");
));
ExportToTxtIrrigularly(dt);
I have added BGW component in my form and i did the same thing (codes), but the error has raise again in the BGWExprtrLod_ProgressChanged eventhandler.
– Ehsan
Mar 27 at 6:18
I have done it with the use of this.Invoke(new Action(() =>, but my application freezes until the end of the export method. i have used from BGWorker to prevent from freezing!!!! Is there any point??? Any Guide???
– Ehsan
Mar 27 at 13:06
It sounds like you're using invoke in DoWork, which is pointless. Post more complete code (edit your post) so we can advise how to fix it.
– Idle_Mind
Mar 27 at 13:24
Thanks for your help, i have edited my codes.
– Ehsan
Mar 28 at 4:15
Idle Mind thanks for your perusing. Unfortunately, outside of this.Invoke()=> , ExportToTxtIrrigularly(dt); does not execute!!!! probably this is because of BGW_progressChanged(). if i do this way, i will get the response of the BGW_Completed(). Is Any Other Way?????
– Ehsan
Mar 28 at 12:58
|
show 4 more comments
Not really sure what kind of setup you've got, but it will look something like this:
private BackgroundWorker bGWExprtrLod;
private void Form1_Load(object sender, EventArgs e)
bGWExprtrLod = new BackgroundWorker();
bGWExprtrLod.WorkerReportsProgress = true;
bGWExprtrLod.ProgressChanged += BGWExprtrLod_ProgressChanged;
bGWExprtrLod.RunWorkerCompleted += BGWExprtrLod_RunWorkerCompleted;
bGWExprtrLod.DoWork += BGWExprtrLod_DoWork;
public void SetDaTableAndFileNameFn(System.Data.DataTable DataTable)
// ... other code ...
bGWExprtrLod.RunWorkerAsync();
private void BGWExprtrLod_DoWork(object sender, DoWorkEventArgs e)
bGWExprtrLod.ReportProgress(0, ">>> Creating And Transferring Data To The File...");
// ... do some work ...
private void BGWExprtrLod_ProgressChanged(object sender, ProgressChangedEventArgs e)
string msg = e.UserState.ToString();
richTxtBxExprtr.AppendText(msg);
private void BGWExprtrLod_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
richTxtBxExprtr.AppendText("Transfer Complete!");
** EDIT **
Start by moving the call to you export method outside the Invoke() call:
private void bGWExprtrLod_DoWork(object sender, DoWorkEventArgs e)
this.Invoke(new Action(() =>
richTxtBxExprtr.AppendText(">>> Start Processing...n>>> Copying Data Take A Little Time.n>>> Be Patient...n>>> Loadind Data...n-----------------------------------------------n");
));
ExportToTxtIrrigularly(dt);
I have added BGW component in my form and i did the same thing (codes), but the error has raise again in the BGWExprtrLod_ProgressChanged eventhandler.
– Ehsan
Mar 27 at 6:18
I have done it with the use of this.Invoke(new Action(() =>, but my application freezes until the end of the export method. i have used from BGWorker to prevent from freezing!!!! Is there any point??? Any Guide???
– Ehsan
Mar 27 at 13:06
It sounds like you're using invoke in DoWork, which is pointless. Post more complete code (edit your post) so we can advise how to fix it.
– Idle_Mind
Mar 27 at 13:24
Thanks for your help, i have edited my codes.
– Ehsan
Mar 28 at 4:15
Idle Mind thanks for your perusing. Unfortunately, outside of this.Invoke()=> , ExportToTxtIrrigularly(dt); does not execute!!!! probably this is because of BGW_progressChanged(). if i do this way, i will get the response of the BGW_Completed(). Is Any Other Way?????
– Ehsan
Mar 28 at 12:58
|
show 4 more comments
Not really sure what kind of setup you've got, but it will look something like this:
private BackgroundWorker bGWExprtrLod;
private void Form1_Load(object sender, EventArgs e)
bGWExprtrLod = new BackgroundWorker();
bGWExprtrLod.WorkerReportsProgress = true;
bGWExprtrLod.ProgressChanged += BGWExprtrLod_ProgressChanged;
bGWExprtrLod.RunWorkerCompleted += BGWExprtrLod_RunWorkerCompleted;
bGWExprtrLod.DoWork += BGWExprtrLod_DoWork;
public void SetDaTableAndFileNameFn(System.Data.DataTable DataTable)
// ... other code ...
bGWExprtrLod.RunWorkerAsync();
private void BGWExprtrLod_DoWork(object sender, DoWorkEventArgs e)
bGWExprtrLod.ReportProgress(0, ">>> Creating And Transferring Data To The File...");
// ... do some work ...
private void BGWExprtrLod_ProgressChanged(object sender, ProgressChangedEventArgs e)
string msg = e.UserState.ToString();
richTxtBxExprtr.AppendText(msg);
private void BGWExprtrLod_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
richTxtBxExprtr.AppendText("Transfer Complete!");
** EDIT **
Start by moving the call to you export method outside the Invoke() call:
private void bGWExprtrLod_DoWork(object sender, DoWorkEventArgs e)
this.Invoke(new Action(() =>
richTxtBxExprtr.AppendText(">>> Start Processing...n>>> Copying Data Take A Little Time.n>>> Be Patient...n>>> Loadind Data...n-----------------------------------------------n");
));
ExportToTxtIrrigularly(dt);
Not really sure what kind of setup you've got, but it will look something like this:
private BackgroundWorker bGWExprtrLod;
private void Form1_Load(object sender, EventArgs e)
bGWExprtrLod = new BackgroundWorker();
bGWExprtrLod.WorkerReportsProgress = true;
bGWExprtrLod.ProgressChanged += BGWExprtrLod_ProgressChanged;
bGWExprtrLod.RunWorkerCompleted += BGWExprtrLod_RunWorkerCompleted;
bGWExprtrLod.DoWork += BGWExprtrLod_DoWork;
public void SetDaTableAndFileNameFn(System.Data.DataTable DataTable)
// ... other code ...
bGWExprtrLod.RunWorkerAsync();
private void BGWExprtrLod_DoWork(object sender, DoWorkEventArgs e)
bGWExprtrLod.ReportProgress(0, ">>> Creating And Transferring Data To The File...");
// ... do some work ...
private void BGWExprtrLod_ProgressChanged(object sender, ProgressChangedEventArgs e)
string msg = e.UserState.ToString();
richTxtBxExprtr.AppendText(msg);
private void BGWExprtrLod_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
richTxtBxExprtr.AppendText("Transfer Complete!");
** EDIT **
Start by moving the call to you export method outside the Invoke() call:
private void bGWExprtrLod_DoWork(object sender, DoWorkEventArgs e)
this.Invoke(new Action(() =>
richTxtBxExprtr.AppendText(">>> Start Processing...n>>> Copying Data Take A Little Time.n>>> Be Patient...n>>> Loadind Data...n-----------------------------------------------n");
));
ExportToTxtIrrigularly(dt);
edited Mar 28 at 5:21
answered Mar 26 at 14:01
Idle_MindIdle_Mind
25.5k2 gold badges17 silver badges25 bronze badges
25.5k2 gold badges17 silver badges25 bronze badges
I have added BGW component in my form and i did the same thing (codes), but the error has raise again in the BGWExprtrLod_ProgressChanged eventhandler.
– Ehsan
Mar 27 at 6:18
I have done it with the use of this.Invoke(new Action(() =>, but my application freezes until the end of the export method. i have used from BGWorker to prevent from freezing!!!! Is there any point??? Any Guide???
– Ehsan
Mar 27 at 13:06
It sounds like you're using invoke in DoWork, which is pointless. Post more complete code (edit your post) so we can advise how to fix it.
– Idle_Mind
Mar 27 at 13:24
Thanks for your help, i have edited my codes.
– Ehsan
Mar 28 at 4:15
Idle Mind thanks for your perusing. Unfortunately, outside of this.Invoke()=> , ExportToTxtIrrigularly(dt); does not execute!!!! probably this is because of BGW_progressChanged(). if i do this way, i will get the response of the BGW_Completed(). Is Any Other Way?????
– Ehsan
Mar 28 at 12:58
|
show 4 more comments
I have added BGW component in my form and i did the same thing (codes), but the error has raise again in the BGWExprtrLod_ProgressChanged eventhandler.
– Ehsan
Mar 27 at 6:18
I have done it with the use of this.Invoke(new Action(() =>, but my application freezes until the end of the export method. i have used from BGWorker to prevent from freezing!!!! Is there any point??? Any Guide???
– Ehsan
Mar 27 at 13:06
It sounds like you're using invoke in DoWork, which is pointless. Post more complete code (edit your post) so we can advise how to fix it.
– Idle_Mind
Mar 27 at 13:24
Thanks for your help, i have edited my codes.
– Ehsan
Mar 28 at 4:15
Idle Mind thanks for your perusing. Unfortunately, outside of this.Invoke()=> , ExportToTxtIrrigularly(dt); does not execute!!!! probably this is because of BGW_progressChanged(). if i do this way, i will get the response of the BGW_Completed(). Is Any Other Way?????
– Ehsan
Mar 28 at 12:58
I have added BGW component in my form and i did the same thing (codes), but the error has raise again in the BGWExprtrLod_ProgressChanged eventhandler.
– Ehsan
Mar 27 at 6:18
I have added BGW component in my form and i did the same thing (codes), but the error has raise again in the BGWExprtrLod_ProgressChanged eventhandler.
– Ehsan
Mar 27 at 6:18
I have done it with the use of this.Invoke(new Action(() =>, but my application freezes until the end of the export method. i have used from BGWorker to prevent from freezing!!!! Is there any point??? Any Guide???
– Ehsan
Mar 27 at 13:06
I have done it with the use of this.Invoke(new Action(() =>, but my application freezes until the end of the export method. i have used from BGWorker to prevent from freezing!!!! Is there any point??? Any Guide???
– Ehsan
Mar 27 at 13:06
It sounds like you're using invoke in DoWork, which is pointless. Post more complete code (edit your post) so we can advise how to fix it.
– Idle_Mind
Mar 27 at 13:24
It sounds like you're using invoke in DoWork, which is pointless. Post more complete code (edit your post) so we can advise how to fix it.
– Idle_Mind
Mar 27 at 13:24
Thanks for your help, i have edited my codes.
– Ehsan
Mar 28 at 4:15
Thanks for your help, i have edited my codes.
– Ehsan
Mar 28 at 4:15
Idle Mind thanks for your perusing. Unfortunately, outside of this.Invoke()=> , ExportToTxtIrrigularly(dt); does not execute!!!! probably this is because of BGW_progressChanged(). if i do this way, i will get the response of the BGW_Completed(). Is Any Other Way?????
– Ehsan
Mar 28 at 12:58
Idle Mind thanks for your perusing. Unfortunately, outside of this.Invoke()=> , ExportToTxtIrrigularly(dt); does not execute!!!! probably this is because of BGW_progressChanged(). if i do this way, i will get the response of the BGW_Completed(). Is Any Other Way?????
– Ehsan
Mar 28 at 12:58
|
show 4 more comments
Got a question that you can’t ask on public Stack Overflow? Learn more about sharing private information with Stack Overflow for Teams.
Got a question that you can’t ask on public Stack Overflow? Learn more about sharing private information with Stack Overflow for Teams.
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.
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%2f55349350%2fhow-to-run-a-backgroundworker-and-also-append-a-text-to-a-richtextbox-control-fr%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
Since you have a BackgroundWorker(), use that progress event to update the RichTextBox instead of trying to do it directly...
– Idle_Mind
Mar 26 at 4:38
Thanks for your comment. If I do this way, i will got a cross_threading again!
– Ehsan
Mar 26 at 7:11
How can i invoke RichTextBox.Appen() from BGWorker_DoWork,BGWorker_ProgressChanged and BGWorker_DoWork and BGWorker_RunWorkerCompleted without cross-Threading?
– Ehsan
Mar 26 at 7:21
i have solved BGWorker_DoWorker with this.Invoke(new Action(() =>, but it does not work for in BGWorker_ProgressChanged, BGWorker_RunWorkerCompleted, is there any way to overcome?
– Ehsan
Mar 26 at 13:39
That DataTable parameter is pretty fishy, good odds that this code already runs on a worker thread. BackgroundWorker can only properly marshal to the UI thread when you call its RunWorkerAsync() method from the UI thread. Verify this with the debugger, set a breakpoint on the method and when it hits use Debug > Windows > Threads. With the expectation that you now discover that you don't need a BGW at all. Or need to make it smarter by also generating the DataTable.
– Hans Passant
Mar 26 at 14:40