multithread debuging

  • Thread starter Thread starter Lloyd Dupont
  • Start date Start date
L

Lloyd Dupont

I have a program which use multithread.
Inadvertently dare I say, as they are not my threads but those of a FileSystemWatcher.

Anyway some method get called on my GUI.
I rewrite the code in such a way that it should only be to a funtion like that:
void HTreeChanged(object sender, TreeEventArgs e)
{
if(base.Created)
BeginInvoke(new Action(Invalidate));
}
however there is, apparently, another method call on an other thread.
the problem is I have no idea which one and VS.NET popup a message box saying:
System.InvalidOperationException was unhandled
Message: Cross-thread operation not valid: Control '' accessed from a thread other than the thread it was created on.


but even though I looked at all the active thread's stack I have no idea which method is throwing..

what can I do?
 
oopss... found it.
in the menu debug/exception you've got a little check box to throw and stop at exception!
so handy!

--
If you're in a war, instead of throwing a hand grenade at the enemy, throw one of those small pumpkins. Maybe it'll make everyone think how stupid war is, and while they are thinking, you can throw a real grenade at them.
Jack Handey.
I have a program which use multithread.
Inadvertently dare I say, as they are not my threads but those of a FileSystemWatcher.

Anyway some method get called on my GUI.
I rewrite the code in such a way that it should only be to a funtion like that:
void HTreeChanged(object sender, TreeEventArgs e)
{
if(base.Created)
BeginInvoke(new Action(Invalidate));
}
however there is, apparently, another method call on an other thread.
the problem is I have no idea which one and VS.NET popup a message box saying:
System.InvalidOperationException was unhandled
Message: Cross-thread operation not valid: Control '' accessed from a thread other than the thread it was created on.


but even though I looked at all the active thread's stack I have no idea which method is throwing..

what can I do?
 
If the method Invalidate accesses some controls and it is getting invoked on a thread other then the one the controls were created on (which is what may be happening by using BeginInvoke ), then you need to invoke a method to do the updating on the original thread. You can easily check by checking the control's field InvokeRequired

For example,

if ( this.someControl.InvokeRequired )
this.someControl.Invoke( new MyDelegate( MyDelegatedMethod),args );

I have a program which use multithread.
Inadvertently dare I say, as they are not my threads but those of a FileSystemWatcher.

Anyway some method get called on my GUI.
I rewrite the code in such a way that it should only be to a funtion like that:
void HTreeChanged(object sender, TreeEventArgs e)
{
if(base.Created)
BeginInvoke(new Action(Invalidate));
}
however there is, apparently, another method call on an other thread.
the problem is I have no idea which one and VS.NET popup a message box saying:
System.InvalidOperationException was unhandled
Message: Cross-thread operation not valid: Control '' accessed from a thread other than the thread it was created on.


but even though I looked at all the active thread's stack I have no idea which method is throwing..

what can I do?
 
I found the problem.
Hey... this InvokeRequired variable is interesting....!!
Anyway I manage to single thread the code in my data

--
If you're in a war, instead of throwing a hand grenade at the enemy, throw one of those small pumpkins. Maybe it'll make everyone think how stupid war is, and while they are thinking, you can throw a real grenade at them.
Jack Handey.
If the method Invalidate accesses some controls and it is getting invoked on a thread other then the one the controls were created on (which is what may be happening by using BeginInvoke ), then you need to invoke a method to do the updating on the original thread. You can easily check by checking the control's field InvokeRequired

For example,

if ( this.someControl.InvokeRequired )
this.someControl.Invoke( new MyDelegate( MyDelegatedMethod),args );

I have a program which use multithread.
Inadvertently dare I say, as they are not my threads but those of a FileSystemWatcher.

Anyway some method get called on my GUI.
I rewrite the code in such a way that it should only be to a funtion like that:
void HTreeChanged(object sender, TreeEventArgs e)
{
if(base.Created)
BeginInvoke(new Action(Invalidate));
}
however there is, apparently, another method call on an other thread.
the problem is I have no idea which one and VS.NET popup a message box saying:
System.InvalidOperationException was unhandled
Message: Cross-thread operation not valid: Control '' accessed from a thread other than the thread it was created on.


but even though I looked at all the active thread's stack I have no idea which method is throwing..

what can I do?
 
testing
I found the problem.
Hey... this InvokeRequired variable is interesting....!!
Anyway I manage to single thread the code in my data

--
If you're in a war, instead of throwing a hand grenade at the enemy, throw one of those small pumpkins. Maybe it'll make everyone think how stupid war is, and while they are thinking, you can throw a real grenade at them.
Jack Handey.
If the method Invalidate accesses some controls and it is getting invoked on a thread other then the one the controls were created on (which is what may be happening by using BeginInvoke ), then you need to invoke a method to do the updating on the original thread. You can easily check by checking the control's field InvokeRequired

For example,

if ( this.someControl.InvokeRequired )
this.someControl.Invoke( new MyDelegate( MyDelegatedMethod),args );

I have a program which use multithread.
Inadvertently dare I say, as they are not my threads but those of a FileSystemWatcher.

Anyway some method get called on my GUI.
I rewrite the code in such a way that it should only be to a funtion like that:
void HTreeChanged(object sender, TreeEventArgs e)
{
if(base.Created)
BeginInvoke(new Action(Invalidate));
}
however there is, apparently, another method call on an other thread.
the problem is I have no idea which one and VS.NET popup a message box saying:
System.InvalidOperationException was unhandled
Message: Cross-thread operation not valid: Control '' accessed from a thread other than the thread it was created on.


but even though I looked at all the active thread's stack I have no idea which method is throwing..

what can I do?
 
Back
Top