PC Review


Reply
Thread Tools Rate Thread

problem with threading & nested anonymous method

 
 
Lloyd Dupont
Guest
Posts: n/a
 
      6th Sep 2005
I have a very simple block of code with open a file in a thread
and for the life of me I have no idea, wonder if it's a 2.0 beta bug?

here is my sample (which open a file in a thread)
===
public void Open(string file)
{
Cursor = Cursors.WaitCursor;
new Thread(delegate()
{
RecipeFile rf = null;
try
{
rf = new RecipeFile(file, false, SetStatus);// is ThreadSafe, only independant data
SetStatus(strings.done); // is ThreadSafe
}
catch (ArgumentException e)
{
SetStatus(e.Message);// is ThreadSafe
}
catch (SQLiteException e)
{
SetStatus(e.Message);// is ThreadSafe
}
finally
{
// error happened here in GUI thread: Cursor = Cursors.Default; =>AccessViolationException
this.BeginInvoke(new InvokeDelegate(delegate() { this.Cursor = Cursors.Default; }));
}
if (IsDisposed)
return;
this.BeginInvoke(new InvokeDelegate(delegate() { RecipeFile = rf; }));
}
).Start();
}
====




--
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.
 
Reply With Quote
 
 
 
 
Willy Denoyette [MVP]
Guest
Posts: n/a
 
      6th Sep 2005
What exact version og the framework are you running?
This works for me using 2.0.50727.

Willy.


"Lloyd Dupont" <(E-Mail Removed)> wrote in message news:uJk%(E-Mail Removed)...
I have a very simple block of code with open a file in a thread
and for the life of me I have no idea, wonder if it's a 2.0 beta bug?

here is my sample (which open a file in a thread)
===
public void Open(string file)
{
Cursor = Cursors.WaitCursor;
new Thread(delegate()
{
RecipeFile rf = null;
try
{
rf = new RecipeFile(file, false, SetStatus);// is ThreadSafe, only independant data
SetStatus(strings.done); // is ThreadSafe
}
catch (ArgumentException e)
{
SetStatus(e.Message);// is ThreadSafe
}
catch (SQLiteException e)
{
SetStatus(e.Message);// is ThreadSafe
}
finally
{
// error happened here in GUI thread: Cursor = Cursors.Default; =>AccessViolationException
this.BeginInvoke(new InvokeDelegate(delegate() { this.Cursor = Cursors.Default; }));
}
if (IsDisposed)
return;
this.BeginInvoke(new InvokeDelegate(delegate() { RecipeFile = rf; }));
}
).Start();
}
====




--
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.
 
Reply With Quote
 
Lloyd Dupont
Guest
Posts: n/a
 
      7th Sep 2005
mmhh.. :-/
good to know at least !!!
I will tell you more tonight when I'll be on my home project again ;-)
"Willy Denoyette [MVP]" <(E-Mail Removed)> wrote in message news:%(E-Mail Removed)...
What exact version og the framework are you running?
This works for me using 2.0.50727.

Willy.


"Lloyd Dupont" <(E-Mail Removed)> wrote in message news:uJk%(E-Mail Removed)...
I have a very simple block of code with open a file in a thread
and for the life of me I have no idea, wonder if it's a 2.0 beta bug?

here is my sample (which open a file in a thread)
===
public void Open(string file)
{
Cursor = Cursors.WaitCursor;
new Thread(delegate()
{
RecipeFile rf = null;
try
{
rf = new RecipeFile(file, false, SetStatus);// is ThreadSafe, only independant data
SetStatus(strings.done); // is ThreadSafe
}
catch (ArgumentException e)
{
SetStatus(e.Message);// is ThreadSafe
}
catch (SQLiteException e)
{
SetStatus(e.Message);// is ThreadSafe
}
finally
{
// error happened here in GUI thread: Cursor = Cursors.Default; =>AccessViolationException
this.BeginInvoke(new InvokeDelegate(delegate() { this.Cursor = Cursors.Default; }));
}
if (IsDisposed)
return;
this.BeginInvoke(new InvokeDelegate(delegate() { RecipeFile = rf; }));
}
).Start();
}
====




--
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.
 
Reply With Quote
 
Lloyd Dupont
Guest
Posts: n/a
 
      7th Sep 2005
mmhh.....
progress here.... the anonymous inner method are simple bugged!!!
and the proof below!!!!
there is code, only very slightly different, and one is crashing and not the other.
the difference is subtle anonymous inner class stuff.....


========= Working version ===========
public void Open(string file)
{
Cursor = Cursors.WaitCursor;
string doneMsg = strings.done; // initialize for Thread safety
new Thread(delegate()
{
try
{
RecipeFile rf = new RecipeFile(file, false, SetStatus); // variable declared & used in the same block
SetStatus(doneMsg);
if (!IsDisposed)
BeginInvoke(new InvokeDelegate(delegate()
{
RecipeFile = rf; // used in this other anonymous method & thread
}));
}
catch (ArgumentException e) { SetStatus(e.Message); }
catch (SQLiteException e) { SetStatus(e.Message); }
finally
{
if (!IsDisposed)
BeginInvoke(new InvokeDelegate(delegate() { Cursor = Cursors.Default; }));
}
}
).Start();
}
========= Crashing version ===========
public void Open(string file)
{
Cursor = Cursors.WaitCursor;
string doneMsg = strings.done; // initialize for Thread safety
new Thread(delegate()
{
RecipeFile rf; // variable declared here
try
{
rf = new RecipeFile(file, false, SetStatus);
SetStatus(doneMsg);
if (!IsDisposed)
BeginInvoke(new InvokeDelegate(delegate()
{
RecipeFile = rf; // used in this other anonymous method & thread
}));
}
catch (ArgumentException e) { SetStatus(e.Message); }
catch (SQLiteException e) { SetStatus(e.Message); }
finally
{
if (!IsDisposed)
BeginInvoke(new InvokeDelegate(delegate() { Cursor = Cursors.Default; }));
}
}
).Start();
}
=================================



--
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.
"Willy Denoyette [MVP]" <(E-Mail Removed)> wrote in message news:%(E-Mail Removed)...
What exact version og the framework are you running?
This works for me using 2.0.50727.

Willy.


"Lloyd Dupont" <(E-Mail Removed)> wrote in message news:uJk%(E-Mail Removed)...
I have a very simple block of code with open a file in a thread
and for the life of me I have no idea, wonder if it's a 2.0 beta bug?

here is my sample (which open a file in a thread)
===
public void Open(string file)
{
Cursor = Cursors.WaitCursor;
new Thread(delegate()
{
RecipeFile rf = null;
try
{
rf = new RecipeFile(file, false, SetStatus);// is ThreadSafe, only independant data
SetStatus(strings.done); // is ThreadSafe
}
catch (ArgumentException e)
{
SetStatus(e.Message);// is ThreadSafe
}
catch (SQLiteException e)
{
SetStatus(e.Message);// is ThreadSafe
}
finally
{
// error happened here in GUI thread: Cursor = Cursors.Default; =>AccessViolationException
this.BeginInvoke(new InvokeDelegate(delegate() { this.Cursor = Cursors.Default; }));
}
if (IsDisposed)
return;
this.BeginInvoke(new InvokeDelegate(delegate() { RecipeFile = rf; }));
}
).Start();
}
====




--
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.
 
Reply With Quote
 
Willy Denoyette [MVP]
Guest
Posts: n/a
 
      7th Sep 2005
I can't see how you managed to compile this:
RecipeFile = rf; // used in this other anonymous method & thread
RecipeFile denotes a type not a variable and you assign it a value!
Mind to post the whole code or at least a complete sample that illustrates the issue?
Also talking about a "crash" include the exception message and add the callstack .


Willy.

"Lloyd Dupont" <(E-Mail Removed)> wrote in message news:(E-Mail Removed)...
mmhh.....
progress here.... the anonymous inner method are simple bugged!!!
and the proof below!!!!
there is code, only very slightly different, and one is crashing and not the other.
the difference is subtle anonymous inner class stuff.....


========= Working version ===========
public void Open(string file)
{
Cursor = Cursors.WaitCursor;
string doneMsg = strings.done; // initialize for Thread safety
new Thread(delegate()
{
try
{
RecipeFile rf = new RecipeFile(file, false, SetStatus); // variable declared & used in the same block
SetStatus(doneMsg);
if (!IsDisposed)
BeginInvoke(new InvokeDelegate(delegate()
{
RecipeFile = rf; // used in this other anonymous method & thread
}));
}
catch (ArgumentException e) { SetStatus(e.Message); }
catch (SQLiteException e) { SetStatus(e.Message); }
finally
{
if (!IsDisposed)
BeginInvoke(new InvokeDelegate(delegate() { Cursor = Cursors.Default; }));
}
}
).Start();
}
========= Crashing version ===========
public void Open(string file)
{
Cursor = Cursors.WaitCursor;
string doneMsg = strings.done; // initialize for Thread safety
new Thread(delegate()
{
RecipeFile rf; // variable declared here
try
{
rf = new RecipeFile(file, false, SetStatus);
SetStatus(doneMsg);
if (!IsDisposed)
BeginInvoke(new InvokeDelegate(delegate()
{
RecipeFile = rf; // used in this other anonymous method & thread
}));
}
catch (ArgumentException e) { SetStatus(e.Message); }
catch (SQLiteException e) { SetStatus(e.Message); }
finally
{
if (!IsDisposed)
BeginInvoke(new InvokeDelegate(delegate() { Cursor = Cursors.Default; }));
}
}
).Start();
}
=================================



--
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.
"Willy Denoyette [MVP]" <(E-Mail Removed)> wrote in message news:%(E-Mail Removed)...
What exact version og the framework are you running?
This works for me using 2.0.50727.

Willy.


"Lloyd Dupont" <(E-Mail Removed)> wrote in message news:uJk%(E-Mail Removed)...
I have a very simple block of code with open a file in a thread
and for the life of me I have no idea, wonder if it's a 2.0 beta bug?

here is my sample (which open a file in a thread)
===
public void Open(string file)
{
Cursor = Cursors.WaitCursor;
new Thread(delegate()
{
RecipeFile rf = null;
try
{
rf = new RecipeFile(file, false, SetStatus);// is ThreadSafe, only independant data
SetStatus(strings.done); // is ThreadSafe
}
catch (ArgumentException e)
{
SetStatus(e.Message);// is ThreadSafe
}
catch (SQLiteException e)
{
SetStatus(e.Message);// is ThreadSafe
}
finally
{
// error happened here in GUI thread: Cursor = Cursors.Default; =>AccessViolationException
this.BeginInvoke(new InvokeDelegate(delegate() { this.Cursor = Cursors.Default; }));
}
if (IsDisposed)
return;
this.BeginInvoke(new InvokeDelegate(delegate() { RecipeFile = rf; }));
}
).Start();
}
====




--
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.
 
Reply With Quote
 
Lloyd Dupont
Guest
Posts: n/a
 
      8th Sep 2005
here is how I manage to write such code:
class RecipeFile
{
object someData;
}
class MainForm : Form
{
public RecipeFile RecipeFile // common pattern think of: 'Size Control.Size' for exemple
{
get { return rFile; }
set { rFile = value; }
}
RecipeFile rFile;

public void Open(string file)
{
// code below
}
}

If you want a stack trace (fair enough) I will provide it tonight.
but as far as I remember.... uh.... now that you mention it I didn't looked at this particular trace ?!? mhh.. so bad!!!

"Willy Denoyette [MVP]" <(E-Mail Removed)> wrote in message news:u8e8ES$(E-Mail Removed)...
I can't see how you managed to compile this:
RecipeFile = rf; // used in this other anonymous method & thread
RecipeFile denotes a type not a variable and you assign it a value!
Mind to post the whole code or at least a complete sample that illustrates the issue?
Also talking about a "crash" include the exception message and add the callstack .


Willy.

"Lloyd Dupont" <(E-Mail Removed)> wrote in message news:(E-Mail Removed)...
mmhh.....
progress here.... the anonymous inner method are simple bugged!!!
and the proof below!!!!
there is code, only very slightly different, and one is crashing and not the other.
the difference is subtle anonymous inner class stuff.....


========= Working version ===========
public void Open(string file)
{
Cursor = Cursors.WaitCursor;
string doneMsg = strings.done; // initialize for Thread safety
new Thread(delegate()
{
try
{
RecipeFile rf = new RecipeFile(file, false, SetStatus); // variable declared & used in the same block
SetStatus(doneMsg);
if (!IsDisposed)
BeginInvoke(new InvokeDelegate(delegate()
{
RecipeFile = rf; // used in this other anonymous method & thread
}));
}
catch (ArgumentException e) { SetStatus(e.Message); }
catch (SQLiteException e) { SetStatus(e.Message); }
finally
{
if (!IsDisposed)
BeginInvoke(new InvokeDelegate(delegate() { Cursor = Cursors.Default; }));
}
}
).Start();
}
========= Crashing version ===========
public void Open(string file)
{
Cursor = Cursors.WaitCursor;
string doneMsg = strings.done; // initialize for Thread safety
new Thread(delegate()
{
RecipeFile rf; // variable declared here
try
{
rf = new RecipeFile(file, false, SetStatus);
SetStatus(doneMsg);
if (!IsDisposed)
BeginInvoke(new InvokeDelegate(delegate()
{
RecipeFile = rf; // used in this other anonymous method & thread
}));
}
catch (ArgumentException e) { SetStatus(e.Message); }
catch (SQLiteException e) { SetStatus(e.Message); }
finally
{
if (!IsDisposed)
BeginInvoke(new InvokeDelegate(delegate() { Cursor = Cursors.Default; }));
}
}
).Start();
}
=================================



--
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.
"Willy Denoyette [MVP]" <(E-Mail Removed)> wrote in message news:%(E-Mail Removed)...
What exact version og the framework are you running?
This works for me using 2.0.50727.

Willy.


"Lloyd Dupont" <(E-Mail Removed)> wrote in message news:uJk%(E-Mail Removed)...
I have a very simple block of code with open a file in a thread
and for the life of me I have no idea, wonder if it's a 2.0 beta bug?

here is my sample (which open a file in a thread)
===
public void Open(string file)
{
Cursor = Cursors.WaitCursor;
new Thread(delegate()
{
RecipeFile rf = null;
try
{
rf = new RecipeFile(file, false, SetStatus);// is ThreadSafe, only independant data
SetStatus(strings.done); // is ThreadSafe
}
catch (ArgumentException e)
{
SetStatus(e.Message);// is ThreadSafe
}
catch (SQLiteException e)
{
SetStatus(e.Message);// is ThreadSafe
}
finally
{
// error happened here in GUI thread: Cursor = Cursors.Default; =>AccessViolationException
this.BeginInvoke(new InvokeDelegate(delegate() { this.Cursor = Cursors.Default; }));
}
if (IsDisposed)
return;
this.BeginInvoke(new InvokeDelegate(delegate() { RecipeFile = rf; }));
}
).Start();
}
====




--
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.
 
Reply With Quote
 
Lloyd Dupont
Guest
Posts: n/a
 
      8th Sep 2005
Mmhh.. I know why I didn't investigate the StackTrace......
It's simple a 'serious exception' and VS.NET kept freeze on it, therefore I was not able to get any info.
Now I handle things a bit differently and I have a nice exception panel which *should* display the StackTrace, but all I get is:
--
Exception: System.AccessViolationException
Thread :
Message : Attempted to read or write protected memory. This is often an indication that other memory is corrupt.
Source :
Help :
Stack :



Loaded Assemblies:
- mscorlib v.2.0.0.0
- TheCooksCompanion v.0.0.1.0
etc.........
--
not very helpful, hey?!
anyway, I worked around it!

--
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.
"Willy Denoyette [MVP]" <(E-Mail Removed)> wrote in message news:u8e8ES$(E-Mail Removed)...
I can't see how you managed to compile this:
RecipeFile = rf; // used in this other anonymous method & thread
RecipeFile denotes a type not a variable and you assign it a value!
Mind to post the whole code or at least a complete sample that illustrates the issue?
Also talking about a "crash" include the exception message and add the callstack .


Willy.

"Lloyd Dupont" <(E-Mail Removed)> wrote in message news:(E-Mail Removed)...
mmhh.....
progress here.... the anonymous inner method are simple bugged!!!
and the proof below!!!!
there is code, only very slightly different, and one is crashing and not the other.
the difference is subtle anonymous inner class stuff.....


========= Working version ===========
public void Open(string file)
{
Cursor = Cursors.WaitCursor;
string doneMsg = strings.done; // initialize for Thread safety
new Thread(delegate()
{
try
{
RecipeFile rf = new RecipeFile(file, false, SetStatus); // variable declared & used in the same block
SetStatus(doneMsg);
if (!IsDisposed)
BeginInvoke(new InvokeDelegate(delegate()
{
RecipeFile = rf; // used in this other anonymous method & thread
}));
}
catch (ArgumentException e) { SetStatus(e.Message); }
catch (SQLiteException e) { SetStatus(e.Message); }
finally
{
if (!IsDisposed)
BeginInvoke(new InvokeDelegate(delegate() { Cursor = Cursors.Default; }));
}
}
).Start();
}
========= Crashing version ===========
public void Open(string file)
{
Cursor = Cursors.WaitCursor;
string doneMsg = strings.done; // initialize for Thread safety
new Thread(delegate()
{
RecipeFile rf; // variable declared here
try
{
rf = new RecipeFile(file, false, SetStatus);
SetStatus(doneMsg);
if (!IsDisposed)
BeginInvoke(new InvokeDelegate(delegate()
{
RecipeFile = rf; // used in this other anonymous method & thread
}));
}
catch (ArgumentException e) { SetStatus(e.Message); }
catch (SQLiteException e) { SetStatus(e.Message); }
finally
{
if (!IsDisposed)
BeginInvoke(new InvokeDelegate(delegate() { Cursor = Cursors.Default; }));
}
}
).Start();
}
=================================



--
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.
"Willy Denoyette [MVP]" <(E-Mail Removed)> wrote in message news:%(E-Mail Removed)...
What exact version og the framework are you running?
This works for me using 2.0.50727.

Willy.


"Lloyd Dupont" <(E-Mail Removed)> wrote in message news:uJk%(E-Mail Removed)...
I have a very simple block of code with open a file in a thread
and for the life of me I have no idea, wonder if it's a 2.0 beta bug?

here is my sample (which open a file in a thread)
===
public void Open(string file)
{
Cursor = Cursors.WaitCursor;
new Thread(delegate()
{
RecipeFile rf = null;
try
{
rf = new RecipeFile(file, false, SetStatus);// is ThreadSafe, only independant data
SetStatus(strings.done); // is ThreadSafe
}
catch (ArgumentException e)
{
SetStatus(e.Message);// is ThreadSafe
}
catch (SQLiteException e)
{
SetStatus(e.Message);// is ThreadSafe
}
finally
{
// error happened here in GUI thread: Cursor = Cursors.Default; =>AccessViolationException
this.BeginInvoke(new InvokeDelegate(delegate() { this.Cursor = Cursors.Default; }));
}
if (IsDisposed)
return;
this.BeginInvoke(new InvokeDelegate(delegate() { RecipeFile = rf; }));
}
).Start();
}
====




--
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.
 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
anonymous method Tony Johansson Microsoft C# .NET 2 16th Mar 2010 11:07 PM
Re: anonymous method help Martin Honnen Microsoft C# .NET 2 6th Mar 2009 07:55 PM
Re: anonymous method help dunawayc@gmail.com Microsoft C# .NET 0 6th Mar 2009 03:23 PM
Anonymous Method vs. Anonymous Delegate Robert Howells Microsoft C# .NET 3 21st Oct 2007 09:08 AM
Anonymous Method Bob Microsoft C# .NET 2 8th Oct 2007 02:54 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 04:57 PM.