By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,984 Members | 1,009 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,984 IT Pros & Developers. It's quick & easy.

invoke required keeps true

P: 47
Hello all, i am making a multithreaded program which copies a file.
at the end of the file copy it will call a process to update a certain dropdownlist.
the following code will cause a stack overflow, i copied it from the MSDN library and took out the items i didnt need (parameters) and replaced it with my variables.

any help would be appreciated, it could well be that i overlooked something

Expand|Select|Wrap|Line Numbers
  1. delegate void LoadList();
  2.  
  3. private void loadList()
  4.         {
  5.             if (ddlTools.InvokeRequired)
  6.             {
  7.                 LoadList d = new LoadList(loadList);
  8.                 d.Invoke();
  9.             }
  10.             else
  11.             {
  12.                 //Clear our list
  13.                 ddlTools.Items.Clear();
  14.                 //Etc,,,
  15.             }
  16.         }
Mar 5 '09 #1
Share this Question
Share on Google+
6 Replies


tlhintoq
Expert 2.5K+
P: 3,525
I'm going to bet you are looping endlessly really fast and running the stack out.

In the method loadList(), line 7 calls a new loadList(), which is itself, which then calls a new loadList() which is itself, which calls a new loadList() which is itself... get the idea?

Put a breakpoint at line 7 and step through it. I'll bet you see this getting called over and over.
Mar 5 '09 #2

P: 47
yes, i know that far.
i also found out that d.invoke() will use the same thread
so i must use this.invoke(d)
*edit*
capitals are different
loadList
LoadList
Mar 5 '09 #3

tlhintoq
Expert 2.5K+
P: 3,525
Right. LoadList keeps making a new loadList. Over and over and over because it keeps calling itself in line 7.

But it will only do this if the condition of line 5 is true.
Try this: insert a line between 6 and 7 that makes ddtool.InvokeRequired false. That way it will work the first time through, but be changed after it has done the first invoke.
Mar 5 '09 #4

P: 47
i already fixed it by using this.invoke(d)
if i forced it to true i'd probably get a unsafe threadcall error since its still a different thread then the UI component (hence the eternal loop?)
Mar 5 '09 #5

Expert 100+
P: 190
In order to marshal the thread back to the UI thread, it is the control which needs to call invoke on the delegate. This is different than just creating a new delegate and calling invoke on it, because, as tlhintoq says, that just keeps calling itself, and since the thread has not been marshalled yet, the InvokeRequired condition remains true.

Try the following:

Expand|Select|Wrap|Line Numbers
  1. delegate void LoadList();
  2.  
  3. private void loadList()
  4.         {
  5.             if (ddlTools.InvokeRequired)
  6.             {
  7.                 LoadList d = new LoadList(loadList);
  8.                 ddlTools.Invoke(d);
  9.             }
  10.             else
  11.             {
  12.                 //Clear our list
  13.                 ddlTools.Items.Clear();
  14.                 //Etc,,,
  15.             }
  16.         }
Mar 6 '09 #6

Plater
Expert 5K+
P: 7,872
As long as the control used in the if statement is the same one used to .Invoke you should be fine.
I use "this" for both and it seems to work well enough
Mar 6 '09 #7

Post your reply

Sign in to post your reply or Sign up for a free account.