Click to See Complete Forum and Search --> : Cross Thread operation not valid


bigjoe11a
June 6th, 2009, 12:18 AM
This is new to me. I been playing a round with some libraries for an FTP server. It's just that the person who made it is from Japan.
Any way I keep getting a,

Cross Thread operation not valid control 'm_ListBoxMessages' accessed from a thread other then the thread it was created on.

Well i check google.com again and found some fixes. It just I don't under stand what to do or how to do it.

Here the code that calls it

Assemblies.Ftp.FtpServerMessageHandler.Message += new Assemblies.Ftp.FtpServerMessageHandler.MessageEventHandler(MessageHandler_Message);


and here the code it calls.


private void MessageHandler_Message(int nId, string sMessage)
{
m_listBoxMessages.BeginUpdate();

int nItem = m_listBoxMessages.Items.Add(string.Format("({0}) <{1}> {2}", nId, System.DateTime.Now, sMessage));

if (m_listBoxMessages.Items.Count > 5000)
{
m_listBoxMessages.Items.RemoveAt(0);
}

if (m_listBoxMessages.SelectedIndex < 0)
{
m_listBoxMessages.TopIndex = nItem;
}
else if (m_listBoxMessages.SelectedIndex == nItem - 1)
{
m_listBoxMessages.SelectedIndex = nItem;
}

m_listBoxMessages.EndUpdate();
}


can some one please tell me how to fix this. This is all new to me. This kind of programing is too complexed for me.

Thanks
Joe

dannystommen
June 6th, 2009, 03:53 AM
You need to Invoke it to the main thread


public delegate void UpdateListbox(int nId, string sMessage);

public void MessageHandler_Message(int nId, string sMessage)
{
if (m_listBoxMessages.InvokeRequired)
{
m_listBoxMessages.Invoke(new UpdateListbox(MessageHandler_Message), new object[] { int nId, string sMessage });
}
else
{
// your original code here
}
}

bigjoe11a
June 6th, 2009, 09:41 AM
Thanks Danny, But I'm all new to this. Any chance I can talk you into setting it up like it should be. I just trying to under stand what your telling me to do and I'm sorry I don't.


public delegate void UpdateListbox(int nId, string sMessage);

public void MessageHandler_Message(int nId, string sMessage)
{
if (m_listBoxMessages.InvokeRequired)
{
m_listBoxMessages.Invoke(new UpdateListbox(MessageHandler_Message), new object[] { int nId, string sMessage });
}
else
{
// your original code here

m_listBoxMessages.BeginUpdate();

int nItem = m_listBoxMessages.Items.Add(string.Format("({0}) <{1}> {2}", nId, System.DateTime.Now, sMessage));

if (m_listBoxMessages.Items.Count > 5000)
{
m_listBoxMessages.Items.RemoveAt(0);
}

if (m_listBoxMessages.SelectedIndex < 0)
{
m_listBoxMessages.TopIndex = nItem;
}
else if (m_listBoxMessages.SelectedIndex == nItem - 1)
{
m_listBoxMessages.SelectedIndex = nItem;
}

m_listBoxMessages.EndUpdate();
}
}


is this what you mean. And if so then what calls it.

Arjay
June 6th, 2009, 12:33 PM
is this what you mean. And if so then what calls it.Joe, this may sound flip (which I don't intend), but have you discovered how to set breakpoints and use the debugger? If not, please say so and we will explain it.

bigjoe11a
June 6th, 2009, 03:15 PM
Thanks ArJay and Yes I do. That's how I fount out where the code is crashing at. Did I also say that the code I'm working with now is not mine. Its a sample code for an FTP server that I been playing with. When I set the breakpoint on this line


m_listBoxMessages.BeginUpdate();


That's where the error is at and it tells me what the error is. It just doesn't tell me how to fix it. It doesn't say what lines I need to add or change.

Does that give you a better idea about whats going on. If I can't fix it. or can't find a answer to my question. Then I post and hope and pray that some one can help. I have posted before and never got one reply.

Joe

Arjay
June 6th, 2009, 04:39 PM
That's where the error is at and it tells me what the error is. It just doesn't tell me how to fix it. It doesn't say what lines I need to add or change.What is the error? In general, when you get an error, post the error here.

bigjoe11a
June 6th, 2009, 06:11 PM
What is the error? In general, when you get an error, post the error here.

here the error that the breakpoints shows me

Cross Thread operation not valid control 'm_ListBoxMessage' accessed from a thread other then the thread it was created on.

and then it show me an help option below. I just didn't under stand what it was talking about

How to Make Cross Thread Calls to windows form controls

Arjay
June 6th, 2009, 09:15 PM
here the error that the breakpoints shows me

Cross Thread operation not valid control 'm_ListBoxMessage' accessed from a thread other then the thread it was created on.

and then it show me an help option below. I just didn't under stand what it was talking about

How to Make Cross Thread Calls to windows form controlsClick on the help, download the sample (or copy the sample into a new project), compile the sample and use the debugger to step through the sample. This is how you can understand the sample enough that you can apply the sample in your code. Once you've done that, post back with specific questions on anything you don't understand.

To get you started, here some threading background:

When threading there is a basic concept that threads must be synchronized in order to access any resources shared between two or more threads. Actually it's a touch more complicated than this - any number of threads can be allowed read access, but only one thread can be allowed to write at a time. Typically a shared resource is protected against multiple thread access by a critical section or a mutex.

What these do is lock out threads so only one thread has access to the resource at a time. Another approach to synchronization is to use a queue and have one thread service the queue and perform the 'access requests' for the other threads.

.Net controls such as listboxes can be considered shared resources (across threads). As such, only thread can access the resource at a time. By default this thread is the thread that created the listbox. .Net uses the queue approach for synchronization and 'queues' up any change requests for a control via the Invoke mechanism.

bigjoe11a
June 6th, 2009, 09:50 PM
Arjay. If I knew how to do that I would have done so. and all I want is this sample to work so I can read threw the code and get a better under standing of how the program works.

What your asking me to do is some thing a long way off. Since it was only parts of code and not an sample part of a program.

The help section only gives very small samples and it doesn't relate to a program. That was I hate about there help section in VC#.

All i want to do is get this sample code working so I can learn from it and mapbe apply it to a bigger program that I'm setting up.

Joe

Arjay
June 6th, 2009, 11:33 PM
Since I don't see the actual help error, I can't see what was provided.

At any rate, the text you included, "How to Make Cross Thread Calls to windows form controls" looks promising.

Throwing that into google, yields the msdn topic:

http://msdn.microsoft.com/en-us/library/ms171728.aspx

Change the filter of the topic to only include C# code and you'll notice that there is enough code to build a sample.

You create a forms project and put the code from the link into it.

This is part of the learning process understanding how to interpret and use code samples.

Try this, and if you run into trouble, ask specific questions.

bigjoe11a
June 6th, 2009, 11:49 PM
here the error that the breakpoints shows me

Cross Thread operation not valid control 'm_ListBoxMessage' accessed from a thread other then the thread it was created on.

and then it show me an help option below. I just didn't under stand what it was talking about

How to Make Cross Thread Calls to windows form controls

Arjay. You missed the Error help code. I'm reading it now. It the line right above these lines. Look here

This is the answer I got from the help. and I'm reading it again.
>>How to Make Cross Thread Calls to windows form controls
and it still makes no since to me at all.

Arjay
June 7th, 2009, 08:13 PM
Arjay. You missed the Error help code. I'm reading it now. It the line right above these lines. Look here

This is the answer I got from the help. and I'm reading it again.
>>How to Make Cross Thread Calls to windows form controls
and it still makes no since to me at all.Did you try what I suggested? I.e., did you filter so only C# code shows up in the link I provided and use the code to create a sample project?

There's only so much we can do on this forum, if you don't try what we are pointing you to. So put the code into a sample, set some break points and try to understand what is going on. Don't just read the code, compile the code and step through it.