Calling Invoke a 2nd time hangs up my thread ?!?!? 
Author Message
 Calling Invoke a 2nd time hangs up my thread ?!?!?

I use Invoke to call a function from a thread :

public DelegateGetNextLogID m_DelegateGetNextLogID;

public form1()
{
....
       m_DelegateGetNextLogID = new DelegateGetNextLogID(this.GetNextLogID);
....

Quote:
}

private void WorkerThreadFunction()
{
.......
     int logID = (int) Invoke(this.m_DelegateGetNextLogID);    .........

Quote:
}

the thread is created again each time, the Invoke is only called once in the
thread

The second time the thread is created and the invoke is called, it just
hangs up my thread.

Whats wrong here?



Sat, 28 May 2005 18:58:49 GMT  
 Calling Invoke a 2nd time hangs up my thread ?!?!?
Quorton

Quote:
> I use Invoke to call a function from a thread :

> public DelegateGetNextLogID m_DelegateGetNextLogID;

> public form1()
> {
> ....
>        m_DelegateGetNextLogID = new

DelegateGetNextLogID(this.GetNextLogID);

Quote:
> ....
> }

> private void WorkerThreadFunction()
> {
> .......
>      int logID = (int) Invoke(this.m_DelegateGetNextLogID);    .........
> }

> the thread is created again each time, the Invoke is only called once in
the
> thread

> The second time the thread is created and the invoke is called, it just
> hangs up my thread.

> Whats wrong here?

You might need to trace this into the delegate on the second invocation and
find out what line of code it hangs on.  Does the CPU utilization approach
100% or 0% when the second invocation happens and the thread hangs?  If it
approaches 100%, there may be a tight infinite loop.  If it approaches 0%,
there may be a resource locked on a semaphore that is holding things up.

Hope that helps.

Regards,

Randy



Sun, 29 May 2005 00:27:30 GMT  
 Calling Invoke a 2nd time hangs up my thread ?!?!?
Hi Qourton

this sounds like a standard case of a deadlock, Thread A is waiting for
Thread B which in turn waits for Thread A again..

Invoke is a synchronous call, that means, it wont return untill the the
thread you call it upon has executed the delegate
Now, if that thread is still occupied with executing the first invoke, your
second call wont return untill they both finish

Wheter this is what is happening is not entirely clear from the code you
posted, if you post a bit more, i'm sure i could help you bit better.. the
best solution would be to make the GetNextLogID threadsafe so you wouldn't
need to do invoke at all, and have a bit better performance, but it depends
on the code wheter that is possible

I've had some cases where i coulnd't figure out why a deadlock happened, and
one of my standard approaches then was to see wheter the problem would
dissapear when you would replace the Invoke() with a BeginInvoke(), since
that would return immidiately..

If this happens however, there is something wrong, since that means you dont
understand your own code, so i dont advise it :-)

Willem



Quote:
> I use Invoke to call a function from a thread :

> public DelegateGetNextLogID m_DelegateGetNextLogID;

> public form1()
> {
> ....
>        m_DelegateGetNextLogID = new

DelegateGetNextLogID(this.GetNextLogID);
Quote:
> ....
> }

> private void WorkerThreadFunction()
> {
> .......
>      int logID = (int) Invoke(this.m_DelegateGetNextLogID);    .........
> }

> the thread is created again each time, the Invoke is only called once in
the
> thread

> The second time the thread is created and the invoke is called, it just
> hangs up my thread.

> Whats wrong here?



Sun, 29 May 2005 06:42:06 GMT  
 Calling Invoke a 2nd time hangs up my thread ?!?!?
I'm not sure what else I can tell you, but here is a bit more code

public DelegateGetNextLogID m_DelegateGetNextLogID;

public form1()
{
....
       m_DelegateGetNextLogID = new DelegateGetNextLogID(this.GetNextLogID);
....

Quote:
}

void StartDownload()
{
.......
      m_DownloadThread = new Thread(new
ThreadStart(this.DownloadThreadFunction,));
      m_DownloadThread.Name = "NNTP Socket Thread";
      m_DownloadThread.Start();

Quote:
}

private void WorkerThreadFunction()
{
.......
     int logID = (int) Invoke(this.m_DelegateGetNextLogID);
.........

Quote:
}

public int GetNextLogID()
{
      for (int p = 0; p < nrThreads; p++)
      {
          if (this.TabPage[p].available)
         {
              this.TabPage[p].available = false;
              this.tabControl1.Controls.AddRange(new
System.Windows.Forms.Control[] {TabPage[p].page});
              return p;
         }
     }
     this.Disable_Functions();
     return -1;

Quote:
}

The problem occurs when I call StartDownload the second time, the first time
everything works like a charm, but the 2nd time it hangs in the invoke. I
also tried InvokeMethod but that did the same.
I create the thread in my main form, there are no other threads, so I don't
see how it can be a deadlock. (I know the main form is a thread in itself,
but it isn't doing anything)
Also I cannot step into the Invoke with the de{*filter*} (the 2nd time around),
the second I do that it hangs.

The reason I have to use invoke is because I edit a tabcontrol, and you
can't do that from another thread.



Sun, 29 May 2005 16:54:33 GMT  
 Calling Invoke a 2nd time hangs up my thread ?!?!?

Quote:
> The problem occurs when I call StartDownload the second time, the first
time
> everything works like a charm, but the 2nd time it hangs in the invoke.

Actually, it doesn't hang, but it blocks. you can never step into an invoke,
since it approximately does the following

post a message in message loop of the user interface thread that the control
was created on
suspend the thread till we get a notification that the thread has picked up
the message, and executed it. then return the return value

as you can see, this will never allow you to step into the delegate method
you execute..

Now there are 2 possibities

1) The second time you call invoke, the user interface thread never picks up
the message and never starts executing the delegate, and thus it never
returns

2) there is an endless loop in the delegate you executing, and thus it will
never return either.  from the sourcecode you posted thisis not very likely,
though i dont know what this.DisableFunctions does

In both cases this will lead to an unresponsive userinterface, and which
case happens can easily be verified by placing a breakpoint at the beginning
of GetNextLogID()

then the second time you call invoke, it'll either

1) do nothing and "hang" again, then you have case 1
2) break at the breakpoint in GetNextLogID() and then you have case 2

Now, i suggest that after you have a lockup, you break into it, and call the
threads window (ctrl-alt-h)
This shows you a list of all current threads, and where their execution
point it, and this should be able to tell you why the main userinterface
thread is not responding to you invoke (case 1) or something else i can't
think of right now :-)

HTH,
Willem



Sun, 29 May 2005 17:55:22 GMT  
 Calling Invoke a 2nd time hangs up my thread ?!?!?

Quote:
> Now there are 2 possibities

> 1) The second time you call invoke, the user interface thread never picks
up
> the message and never starts executing the delegate, and thus it never
> returns

> 2) there is an endless loop in the delegate you executing, and thus it
will
> never return either.  from the sourcecode you posted thisis not very
likely,
> though i dont know what this.DisableFunctions does

> In both cases this will lead to an unresponsive userinterface, and which
> case happens can easily be verified by placing a breakpoint at the
beginning
> of GetNextLogID()

> then the second time you call invoke, it'll either

> 1) do nothing and "hang" again, then you have case 1
> 2) break at the breakpoint in GetNextLogID() and then you have case 2

case 1 it is.

Quote:
> Now, i suggest that after you have a lockup, you break into it, and call
the
> threads window (ctrl-alt-h)
> This shows you a list of all current threads, and where their execution
> point it, and this should be able to tell you why the main userinterface
> thread is not responding to you invoke (case 1) or something else i can't
> think of right now :-)

Did that, it blocks on the invoke, but I still don't know why, because there
is no code of this invoke (Welcome to the wonderfull world of C#, I'm
starting to mis MFC) so I have no idea why it blocks.

So it's case 1, but why and how do I solve it ? What did I do wrong ?



Sun, 29 May 2005 18:43:16 GMT  
 Calling Invoke a 2nd time hangs up my thread ?!?!?

Quote:

> Did that, it blocks on the invoke, but I still don't know why, because there
> is no code of this invoke (Welcome to the wonderfull world of C#, I'm
> starting to mis MFC) so I have no idea why it blocks.

> So it's case 1, but why and how do I solve it ? What did I do wrong ?

Can you come up with a small but complete program that demonstrates the
problem without going to any extra resources, i.e. a program that we can
all try? That's likely to be the easiest way to get moving on this
problem.

See http://www.pobox.com/~skeet/csharp/complete.html

--

http://www.pobox.com/~skeet/
If replying to the group, please do not mail me too



Sun, 29 May 2005 19:05:51 GMT  
 Calling Invoke a 2nd time hangs up my thread ?!?!?
Hi, could you just clarify what happens when you do this:

Set a break point on the following line in your WorkerThreadFunction (I
assume the line is in a loop, right?);

int logID = (int) Invoke(this.m_DelegateGetNextLogID); (breakpoint 1)

Set a break point at the top of GetNextLogID. (breakpoint 2)

When you run the code, is this what happens;

1) You hit breakpoint 1
2) Step into it (such that this is)
3) You hit breakpoint 2
4) Step through GetNextLogID until it returns
5) Back in WorkerThreadFunction, Invoke returns (is it the correct value?)
6) step around WorkerThreadFunction until next attempt to Invoke
7) Step into Invoke
8) Breakpoint 2 is not hit, but UI is unresponsive?

--
Nick Holmes
Coyote Software, GmbH.


Quote:
> > Now there are 2 possibities

> > 1) The second time you call invoke, the user interface thread never
picks
> up
> > the message and never starts executing the delegate, and thus it never
> > returns

> > 2) there is an endless loop in the delegate you executing, and thus it
> will
> > never return either.  from the sourcecode you posted thisis not very
> likely,
> > though i dont know what this.DisableFunctions does

> > In both cases this will lead to an unresponsive userinterface, and which
> > case happens can easily be verified by placing a breakpoint at the
> beginning
> > of GetNextLogID()

> > then the second time you call invoke, it'll either

> > 1) do nothing and "hang" again, then you have case 1
> > 2) break at the breakpoint in GetNextLogID() and then you have case 2

> case 1 it is.

> > Now, i suggest that after you have a lockup, you break into it, and call
> the
> > threads window (ctrl-alt-h)
> > This shows you a list of all current threads, and where their execution
> > point it, and this should be able to tell you why the main userinterface
> > thread is not responding to you invoke (case 1) or something else i
can't
> > think of right now :-)

> Did that, it blocks on the invoke, but I still don't know why, because
there
> is no code of this invoke (Welcome to the wonderfull world of C#, I'm
> starting to mis MFC) so I have no idea why it blocks.

> So it's case 1, but why and how do I solve it ? What did I do wrong ?



Sun, 29 May 2005 19:06:14 GMT  
 Calling Invoke a 2nd time hangs up my thread ?!?!?

Quote:

> 1) You hit breakpoint 1
> 2) Step into it (such that this is)
> 3) You hit breakpoint 2
> 4) Step through GetNextLogID until it returns
> 5) Back in WorkerThreadFunction, Invoke returns (is it the correct value?)

Yes I get the correct value the first time, after I leave the thread and
start the thread again (By calling StartDownload again) it blocks in the
invoke call, breakpoints set in GetNextLogID have no effect, so it doesn't
even get there.

Quote:
> 6) step around WorkerThreadFunction until next attempt to Invoke
> 7) Step into Invoke
> 8) Breakpoint 2 is not hit, but UI is unresponsive?

The UI however is still responsive, so the main thread is still active and
running, but thats because the Invoke call is within the download thread, so
that's not really a surprise.


Sun, 29 May 2005 19:53:49 GMT  
 Calling Invoke a 2nd time hangs up my thread ?!?!?
I don't know whets going on either, but something caught my eye;

I assumed that GetNextLogId was being called in a loop in
WorkerThreadFunction, but you seem to imply its only called once, and before
its called again, the thread is left, and restarted. I wonder if this is
having some effect.

Could you try making 2 consecutive calls to GetNextLogId (just cut an paste
the line below itself), and step through it again.

Or, and Jon suggested, post some code here, so that we can play with it too!

--
Nick Holmes
Coyote Software, GmbH.


Quote:

> > 1) You hit breakpoint 1
> > 2) Step into it (such that this is)
> > 3) You hit breakpoint 2
> > 4) Step through GetNextLogID until it returns
> > 5) Back in WorkerThreadFunction, Invoke returns (is it the correct
value?)

> Yes I get the correct value the first time, after I leave the thread and
> start the thread again (By calling StartDownload again) it blocks in the
> invoke call, breakpoints set in GetNextLogID have no effect, so it doesn't
> even get there.

> > 6) step around WorkerThreadFunction until next attempt to Invoke
> > 7) Step into Invoke
> > 8) Breakpoint 2 is not hit, but UI is unresponsive?

> The UI however is still responsive, so the main thread is still active and
> running, but thats because the Invoke call is within the download thread,
so
> that's not really a surprise.



Sun, 29 May 2005 20:00:33 GMT  
 Calling Invoke a 2nd time hangs up my thread ?!?!?

Quote:
> The UI however is still responsive, so the main thread is still active and
> running

interesting, if the main thread is still running the Invoke should be
serviced

Quote:
> but thats because the Invoke call is within the download thread, so
> that's not really a surprise.

The invoke is waiting for the Main thread to execute it, the only question
is why..

I can't really say more about it while dry swimming, so i suggest you do as
Jon posted, create a minimal program that replicates the behaviour

Willem,



Sun, 29 May 2005 20:16:18 GMT  
 Calling Invoke a 2nd time hangs up my thread ?!?!?


Wed, 18 Jun 1902 08:00:00 GMT  
 Calling Invoke a 2nd time hangs up my thread ?!?!?
Quote:
> I assumed that GetNextLogId was being called in a loop in
> WorkerThreadFunction, but you seem to imply its only called once, and
before
> its called again, the thread is left, and restarted. I wonder if this is
> having some effect.

I was thinking the same

Quote:
> Could you try making 2 consecutive calls to GetNextLogId (just cut an
paste
> the line below itself), and step through it again.

Tried it and it worked, so that didn't help

Quote:
> Or, and Jon suggested, post some code here, so that we can play with it
too!

Tried that too, but couldn't reproduce it with a minium of code

BUT...then I stripped some code until it worked and added it bit by bit, and
whatayaknow, At the end of the thread I call RemoveLogID(logid) (yes without
the invoke, since I didn't get an error on it)

the function looks like this

public void RemoveLog(int logID)
{
       tabControl1.Controls.Remove(TabPage[logID].page);
      TabPage[logID].available = true;
      TabPage[logID].progressBar.Value = 0;
      Enable_Functions();

Quote:
}

I didn't notice it before (since I didn't get a compile or runtime error on
it), but because I edit the tabcontrol on the main form from within a thread
I'm supposed to use Invoke here too, so I did, AND NOW IT WORKS.

I know, it's a stupid mistake and I've should have seen it earlier.
But this still doesn't explain to me why I didn't get a compile or runtime
error (with GetNextLogID I got a runtime error (*) and it basically does the
same) and why the invoke on GetNextLogID blocked ?

A well, another MS Mistery......

Thanx for the help anyway.

(*) - Controls created on one thread cannot be parented to a control on a
different thread.



Sun, 29 May 2005 20:37:44 GMT  
 
 [ 13 post ] 

 Relevant Pages 

1. function doesn't work when main calls the 2nd time

2. COM Server hang ups

3. HELP - NT SysTray Clock Hangs when changing time and background threads running in my app

4. Microsoft Word hangs up if I call OleInitialize in my background thread

5. 2nd Instance of VC++ Hangs???

6. IcmpSendEcho works first time, but hangs every time after

7. CDatabase::OpenEx() hangs when process invoked by IIS?

8. Multithreading, User Interface Threads, SetWindowText/SendMessage, Thread Hangs

9. calling CoInitialize() multiple times on the same thread?

10. MSDN Integration, 2nd time

11. 2nd time around ...

12. Exception Handling - 2nd Time around

 

 
Powered by phpBB® Forum Software