Post: [SOLVED] cross-threading
05-28-2016, 05:14 PM #1
seanhellen
Are you high?
(adsbygoogle = window.adsbygoogle || []).push({}); Hi everyone - I am a hobby coder (ie noob lol) and I have done a "thing" and wanted it to be threaded as it can take a while depending on the size of the table being exported. Anyways, I have this;

    
private void Export_Click(object sender, EventArgs e)
{
Thread add = new Thread(() => ExportDoc());
add.IsBackground = true;
add.Start();
}


    
private void ExportDoc()
{
if (Total)
{
//export to word & save the doc
}
else
{
MessageBox.Show("Press The Populate Info Button First", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}


all it does is export the contents of a few textboxes and a datagridview to word, but it throws a cross-threading error in one of the textboxes (bikedet.text). i was following a simple tut in this section and saw a link at the end to another thread explaingin this problem and how to fix it - but I became very very lost very quickly Happy Can anyone show me how to fix it?

Thanks Smile
Last edited by seanhellen ; 05-29-2016 at 04:14 PM.
05-29-2016, 03:00 AM #2
Originally posted by seanhellen View Post
Hi everyone - I am a hobby coder (ie noob lol) and I have done a "thing" and wanted it to be threaded as it can take a while depending on the size of the table being exported. Anyways, I have this;

    
private void Export_Click(object sender, EventArgs e)
{
Thread add = new Thread(() => ExportDoc());
add.IsBackground = true;
add.Start();
}


    
private void ExportDoc()
{
if (Total)
{
//export to word & save the doc
}
else
{
MessageBox.Show("Press The Populate Info Button First", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}


all it does is export the contents of a few textboxes and a datagridview to word, but it throws a cross-threading error in one of the textboxes (bikedet.text). i was following a simple tut in this section and saw a link at the end to another thread explaingin this problem and how to fix it - but I became very very lost very quickly Happy Can anyone show me how to fix it?

Thanks Smile


I'm not sure if this is what you wanted but this is what I use to export accounts in my account creator.

    
//Add these to the top of your project
using System.IO;
using System.Threading;

//Add this to the button that exports!
saveFileDialog.ShowDialog();
if (saveFileDialog.FileName == null)
return;
Thread write = new Thread(() => writing(saveFileDialog.FileName));
write.IsBackground = true;
write.Start();

//Add this under the button you use to export!
private void writing(string filename)
{
try
{
using (StreamWriter File = new StreamWriter(filename))
{
foreach (var Acc in Accounts)
{
File.WriteLine(Acc.Username + ":" + Acc.OAuth);
}
}
MessageBox.Show("Accounts Exported", "Success", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); return;
}

}


Thanks to kas for this code in his Twitch account creator You must login or register to view this content.
05-29-2016, 12:35 PM #3
Sloth
Banned
Originally posted by seanhellen View Post
Hi everyone - I am a hobby coder (ie noob lol) and I have done a "thing" and wanted it to be threaded as it can take a while depending on the size of the table being exported. Anyways, I have this;

    
private void Export_Click(object sender, EventArgs e)
{
Thread add = new Thread(() => ExportDoc());
add.IsBackground = true;
add.Start();
}


    
private void ExportDoc()
{
if (Total)
{
//export to word & save the doc
}
else
{
MessageBox.Show("Press The Populate Info Button First", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}


all it does is export the contents of a few textboxes and a datagridview to word, but it throws a cross-threading error in one of the textboxes (bikedet.text). i was following a simple tut in this section and saw a link at the end to another thread explaingin this problem and how to fix it - but I became very very lost very quickly Happy Can anyone show me how to fix it?

Thanks Smile


    checkforillegalcrossthreads = false;

Smile

Threads are great but I do believe tasks are better but i'm not entirely sure on that subject
Last edited by Sloth ; 05-29-2016 at 12:37 PM.
05-29-2016, 12:36 PM #4
Sloth
Banned
Originally posted by tyman1294 View Post
I'm not sure if this is what you wanted but this is what I use to export accounts in my account creator.

    
//Add these to the top of your project
using System.IO;
using System.Threading;

//Add this to the button that exports!
saveFileDialog.ShowDialog();
if (saveFileDialog.FileName == null)
return;
Thread write = new Thread(() => writing(saveFileDialog.FileName));
write.IsBackground = true;
write.Start();

//Add this under the button you use to export!
private void writing(string filename)
{
try
{
using (StreamWriter File = new StreamWriter(filename))
{
foreach (var Acc in Accounts)
{
File.WriteLine(Acc.Username + ":" + Acc.OAuth);
}
}
MessageBox.Show("Accounts Exported", "Success", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); return;
}

}


Thanks to kas for this code in his Twitch account creator You must login or register to view this content.


Sloth m80 i'm abused by kas :(

The following user thanked Sloth for this useful post:

tyman1294
05-29-2016, 12:45 PM #5
Mr Smithy x
Former Staff
Originally posted by Kas
    checkforillegalcrossthreads = false;

Smile

Threads are great but I do believe tasks are better but i'm not entirely sure on that subject


Thats the quick and dirty way Sal but i used to do it McCoy

    
private delegate void SetOnFailedCallback(string title, string message);

private void OnFailed(string title, string message){
if(InvokeRequired){
Invoke(new OnFailedCallback(OnFailed), new object[]{string,message});
} else {
doStuff();
}
}
05-29-2016, 12:47 PM #6
Sloth
Banned
Originally posted by Mr
Thats the quick and dirty way Sal but i used to do it McCoy

    
private delegate void SetOnFailedCallback(string title, string message);

private void OnFailed(string title, string message){
if(InvokeRequired){
Invoke(new OnFailedCallback(OnFailed), new object[]{string,message});
} else {
doStuff();
}
}


I'm a dirty boy Sal

I never really looked in to the proper way of handling it I only started that practice recently within the past few months =Z

The following user thanked Sloth for this useful post:

Mr Smithy x
05-29-2016, 01:02 PM #7
jwm614
NextGenUpdate Elite
Originally posted by seanhellen View Post
Hi everyone - I am a hobby coder (ie noob lol) and I have done a "thing" and wanted it to be threaded as it can take a while depending on the size of the table being exported. Anyways, I have this;

    
private void Export_Click(object sender, EventArgs e)
{
Thread add = new Thread(() => ExportDoc());
add.IsBackground = true;
add.Start();
}


    
private void ExportDoc()
{
if (Total)
{
//export to word & save the doc
}
else
{
MessageBox.Show("Press The Populate Info Button First", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}


all it does is export the contents of a few textboxes and a datagridview to word, but it throws a cross-threading error in one of the textboxes (bikedet.text). i was following a simple tut in this section and saw a link at the end to another thread explaingin this problem and how to fix it - but I became very very lost very quickly Happy Can anyone show me how to fix it?

Thanks Smile


     Func<int> del = delegate()
{
textBox1.text = "Text here";
return 0;
};
Invoke(del);

or

delegate void SetTextCallback(string text);
private void SetText(string text)
{
if (textBox1.InvokeRequired)
{
SetTextCallback d = new SetTextCallback(SetText);
textBox1.Invoke(d, new object[] { text });
}
else
textBox1.Text = text;
}
Last edited by jwm614 ; 05-29-2016 at 01:06 PM.
05-29-2016, 02:55 PM #8
seanhellen
Are you high?
Hey guys - thanks for that, but I am now even more confused...

Originally posted by Kas
    checkforillegalcrossthreads = false;


Tried, didn't do anything - I had bool checkforillegalcrossthreads = false; at the top of my project.

Originally posted by Mr
    
private delegate void SetOnFailedCallback(string title, string message);

private void OnFailed(string title, string message){
if(InvokeRequired){
Invoke(new OnFailedCallback(OnFailed), new object[]{string,message});
} else {
doStuff();
}
}


VS didn't like the OnFailedCallback part, nor the string in the new object

Originally posted by jwm614 View Post
     Func<int> del = delegate()
{
textBox1.text = "Text here";
return 0;
};
Invoke(del);

or

delegate void SetTextCallback(string text);
private void SetText(string text)
{
if (textBox1.InvokeRequired)
{
SetTextCallback d = new SetTextCallback(SetText);
textBox1.Invoke(d, new object[] { text });
}
else
textBox1.Text = text;
}


This just confused me completely Happy Didn't know where to start...I replaced textbox1 with bikedet (the one causing the problem) but the error still popped up
05-29-2016, 03:39 PM #9
jwm614
NextGenUpdate Elite
Originally posted by seanhellen View Post
Hey guys - thanks for that, but I am now even more confused...



Tried, didn't do anything - I had bool checkforillegalcrossthreads = false; at the top of my project.



VS didn't like the OnFailedCallback part, nor the string in the new object



This just confused me completely Happy Didn't know where to start...I replaced textbox1 with bikedet (the one causing the problem) but the error still popped up


    private void ExportDoc()
{
if (Total)
{
Func<int> del = delegate()
{
//put you function here
return 0;
};
Invoke(del);
}
else
{
MessageBox.Show("Press The Populate Info Button First", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
05-29-2016, 04:14 PM #10
seanhellen
Are you high?
Originally posted by jwm614 View Post
    private void ExportDoc()
{
if (Total)
{
Func<int> del = delegate()
{
//put you function here
return 0;
};
Invoke(del);
}
else
{
MessageBox.Show("Press The Populate Info Button First", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}


Ahhhh, I see Happy Thanks a lot - that got it :yes:

Copyright © 2024, NextGenUpdate.
All Rights Reserved.

Gray NextGenUpdate Logo