|
private void button1_Click(object sender, EventArgs e)
{
Thread th = new Thread(run);
th.Start();
run();
}
public void run()
{
for (int i = 0; i < 2; i++)
{
textBox1.Text = "I is now " + i;
Thread.Sleep(100);
}
} Could you please tell me what is wrong with this code? It's throwing System.InvalidOperationException on the line
textBox1.Text = ...
modified 25-Nov-14 8:28am.
|
|
|
|
|
1) Not in preformatted block;
2) Which line triggers the error?
Anyway the main problem I think is in Thread.Slepp(100).
Thread is the name of the class, not of the instance. The instance is thrown away at the end of button_1.click() beacuse it goes out of scope, so you'd probably want to pass its reference to the thread procedure OR declare it as a static class variable.
|
|
|
|
|
In additional information section its saying that " Control 'textBox1' accessed from a thread other than the thread it was created on".
|
|
|
|
|
The error is precisely what it states: you cannot directly access a control from a different thread. A quick search on Google drove me here[^], where there are links to msdn, codeproject and stockoverflow articles explaining the matter in more detail.
|
|
|
|
|
Accessing controls from another thread than where they where created is prohibited and you need to work around this by using delegates.
Please note that doing something like this is very resource heavy and should be avoided if possible.
Example of how you can do this. If you need to update multiple GUI components make sure you update them all at once as you only need to invoke once.
private void button1_Click(object sender, EventArgs e) {
Thread th = new Thread(run);
th.Start();
run();
}
public void run() {
for (int i = 0; i < 2; i++) {
UpdateTextBox("I is now " + i);
Thread.Sleep(100);
}
}
private delegate void DUpdateTextBox(string text);
private void UpdateTextBox(string text) {
if (textBox1.InvokeRequired) {
var d = new DUpdateTextBox(UpdateTextBox);
textBox1.Invoke(d, text);
} else {
textBox1.Text = text;
}
}
|
|
|
|
|
Thanks above code is get worked..
|
|
|
|
|
den2k88 wrote: Thread is the name of the class, not of the instance. It's a static method, so no problem there.
Bastard Programmer from Hell
If you can't read my code, try converting it here[^]
|
|
|
|
|
You are accessing the UI (textBox1) from a different thread than the one it was created on; that's not allowed. You'd want to invoke that (Google for the invoke pattern on MSDN).
It is also starting the run-method on the main-thread, after starting it on a separate thread. Meaning it would be running twice.
Bastard Programmer from Hell
If you can't read my code, try converting it here[^]
|
|
|
|
|
You are using a thread to access a textbox on a different thread. To solve this you will need to create a delegate to access the textbox.
have a read of this invoke textbox from thread[^]
Every day, thousands of innocent plants are killed by vegetarians.
Help end the violence EAT BACON
|
|
|
|
|
You can't access UI objects (i.e. controls) except from the UI thread.
So when you try to change the Text property of your textbox from the run method, it fails because it is being executed on the th thread instead.
If you need to access UI controls, you will have to Invoke them first in order to move execution back to the thread that created them: MSDN Invoke[^]
Bad command or file name. Bad, bad command! Sit! Stay! Staaaay...
|
|
|
|
|
Beyond the cross-threading issue pointed out in other posts here, let me warn you about one thing: you have the run function running twice - in different threads at the same time, and it is not thread-safe at all.
|
|
|
|
|
public FormType() {
button.Cick += (o, e) => {
Task.Factory.StartNew(() => {
for (var i = 0; i < 2; i++) {
textBox.Invoke((Action)delegate {
textBox.Text = string.Format("Variable i currently set to: {0}", i);
});
Thread.Sleep(100);
}
});
}
}
|
|
|
|
|
the issue is (probably) that you call UI elements off the main thread. This is not allowed.
You´ll need to "invoke" a method from the thread which will set the Text.
hope this helps.
|
|
|
|
|
Hi ,
Need clarity regarding the custom sections in the app.Config file.
If the section type is System.Configuration.ConfigurationSectionCollection, how should this be coded in C#?
I did some browsing and found that
The type attribute, which is the name of the class that reads the information.
A sample of the app.config file :
="1.0"="utf-8"
<configuration>
<configSections>
<Section name="ClientSettings" type="System.Configuration.ConfigurationSectionCollection" />
</configSections>
<appSettings>
<add key="ServiceName" value="XXX Server A" />
<add key="LogfileName" value="XXXServer.log" />
<add key="LogfileFolder" value="C:\Program Files\XXXServer\logfiles\" />
</appSettings>
<ClientSettings>
<add key="Port1" value="12345" />
<add key="Port2" value="67899" />
</ClientSettings>
</configuration>
How should this be coded in C# ? Anyone kindly help me out.
Some links would also be helpful.
Thanks in advance .
|
|
|
|
|
|
Thanks Richard ! What am more keen to understand is the section type="System.Configuration.ConfigurationSectionCollection" . I cannot find any good code samples available on net.
Thanks
Mary
|
|
|
|
|
|
Two other good resources on CP for working with AppConfig and AppSettings: 2013 [^], 2006 [^].
«If you search in Google for 'no-one ever got fired for buying IBM:' the top-hit is the Wikipedia article on 'Fear, uncertainty and doubt'» What does that tell you about sanity in these times?
|
|
|
|
|
No question here - the link to YouTube has been removed. If no one had replied, I would have deleted the message. PO'H.
modified 25-Nov-14 1:53am.
|
|
|
|
|
Was there supposed to be a question in that post of yours or did you feel like bragging about something? 'cause your post feels a bit like spam.
|
|
|
|
|
Spam is more useful then his post. Its clear they are trying to sell you Viagra. This, I'm not quite sure what he's showing. Not like you can read his screen or anything.
|
|
|
|
|
Yeah, I know. I watched a part of the video and found it extremely boring and utterly useless. Watching someone type code without any explanation at all is pointless.
|
|
|
|
|
If you look closely, I think he's trying to sell Viagra .
|
|
|
|
|
Tengo un reproductor de video con mcisendstring y quisiera reproducir 2 videos y que no se vean cuadros negros entre uno y el otro, es decir que se vea como un solo video. gracias
|
|
|
|
|
Nessuno ti capisce se non parli in Inglese.
Please write in English, it is the official language of CP community!
|
|
|
|