problem with threading & nested anonymous method

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

Lloyd Dupont

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();
}
====
 
What exact version og the framework are you running?
This works for me using 2.0.50727.

Willy.


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();
}
====
 
mmhh.. :-/
good to know at least !!!
I will tell you more tonight when I'll be on my home project again ;-)
What exact version og the framework are you running?
This works for me using 2.0.50727.

Willy.


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();
}
====
 
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.
What exact version og the framework are you running?
This works for me using 2.0.50727.

Willy.


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();
}
====
 
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.

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.
What exact version og the framework are you running?
This works for me using 2.0.50727.

Willy.


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();
}
====
 
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!!!

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.

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.
What exact version og the framework are you running?
This works for me using 2.0.50727.

Willy.


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();
}
====
 
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.
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.

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.
What exact version og the framework are you running?
This works for me using 2.0.50727.

Willy.


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();
}
====
 
Back
Top