Re: problem with threading & nested anonymous method by Lloyd
Lloyd
Wed Sep 07 10:18:44 CDT 2005
This is a multi-part message in MIME format.
------=_NextPart_000_004E_01C5B413.41368EC0
Content-Type: text/plain;
charset="iso-8859-15"
Content-Transfer-Encoding: quoted-printable
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.....
=3D=3D=3D=3D=3D=3D=3D=3D=3D Working version =
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D
public void Open(string file)
{
Cursor =3D Cursors.WaitCursor;
string doneMsg =3D strings.done; // initialize for Thread safety
new Thread(delegate()
{
try
{
RecipeFile rf =3D new RecipeFile(file, false, SetStatus); // =
variable declared & used in the same block
SetStatus(doneMsg);
if (!IsDisposed)
BeginInvoke(new InvokeDelegate(delegate()
{
RecipeFile =3D 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 =3D =
Cursors.Default; }));
}
}
).Start();
}
=3D=3D=3D=3D=3D=3D=3D=3D=3D Crashing version =
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D
public void Open(string file)
{
Cursor =3D Cursors.WaitCursor;
string doneMsg =3D strings.done; // initialize for Thread safety
new Thread(delegate()
{
RecipeFile rf; // variable declared here
try
{
rf =3D new RecipeFile(file, false, SetStatus);
SetStatus(doneMsg);
if (!IsDisposed)
BeginInvoke(new InvokeDelegate(delegate()
{
RecipeFile =3D 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 =3D =
Cursors.Default; }));
}
}
).Start();
}
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D
--=20
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]" <willy.denoyette@telenet.be> wrote in message =
news:%23ioFdtwsFHA.3040@TK2MSFTNGP14.phx.gbl...
What exact version og the framework are you running?
This works for me using 2.0.50727.
Willy.
"Lloyd Dupont" <ld@NewsAccount.galador.net> wrote in message =
news:uJk%23govsFHA.3216@TK2MSFTNGP12.phx.gbl...
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)
=3D=3D=3D
public void Open(string file)
{
Cursor =3D Cursors.WaitCursor;
new Thread(delegate()=20
{
RecipeFile rf =3D null;
try
{
rf =3D 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 =3D =
Cursors.Default; =3D>AccessViolationException
this.BeginInvoke(new InvokeDelegate(delegate() { =
this.Cursor =3D Cursors.Default; }));
}
if (IsDisposed)
return;
this.BeginInvoke(new InvokeDelegate(delegate() { RecipeFile =
=3D rf; }));
}
).Start();
}
=3D=3D=3D=3D
--=20
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.
------=_NextPart_000_004E_01C5B413.41368EC0
Content-Type: text/html;
charset="iso-8859-15"
Content-Transfer-Encoding: quoted-printable
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML><HEAD>
<META http-equiv=3DContent-Type content=3D"text/html; =
charset=3Diso-8859-15">
<META content=3D"MSHTML 6.00.2900.2722" name=3DGENERATOR>
<STYLE></STYLE>
</HEAD>
<BODY bgColor=3D#ffffff>
<DIV><FONT face=3DArial size=3D2>mmhh.....</FONT></DIV>
<DIV><FONT face=3DArial size=3D2>progress here.... the anonymous inner =
method are=20
simple bugged!!!</FONT></DIV>
<DIV><FONT face=3DArial size=3D2>and the proof below!!!!</FONT></DIV>
<DIV><FONT face=3DArial size=3D2>there is code, only very slightly =
different, and=20
one is crashing and not the other.</FONT></DIV>
<DIV><FONT face=3DArial size=3D2>the difference is subtle anonymous =
inner class=20
stuff.....</FONT></DIV>
<DIV><FONT face=3DArial size=3D2></FONT> </DIV>
<DIV><FONT face=3DArial size=3D2></FONT> </DIV>
<DIV><FONT face=3DArial size=3D2>=3D=3D=3D=3D=3D=3D=3D=3D=3D <FONT =
color=3D#800000>Working=20
version</FONT> =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D<BR></FONT><FONT =
color=3D#0000ff=20
size=3D2>public</FONT><FONT size=3D2> </FONT><FONT color=3D#0000ff=20
size=3D2>void</FONT><FONT size=3D2> Open(</FONT><FONT color=3D#0000ff=20
size=3D2>string</FONT><FONT size=3D2> file)<BR>{<BR></FONT><FONT =
color=3D#008080=20
size=3D2> Cursor</FONT><FONT size=3D2> =3D =
</FONT><FONT=20
color=3D#008080 size=3D2>Cursors</FONT><FONT =
size=3D2>.WaitCursor;<BR></FONT><FONT=20
color=3D#0000ff size=3D2> string</FONT><FONT size=3D2> =
doneMsg =3D=20
</FONT><FONT color=3D#008080 size=3D2>strings</FONT><FONT =
size=3D2>.done; </FONT><FONT=20
color=3D#808080 size=3D2>// initialize for Thread safety<BR></FONT><FONT =
color=3D#0000ff size=3D2> new</FONT><FONT size=3D2> =
</FONT><FONT=20
color=3D#008080 size=3D2>Thread</FONT><FONT size=3D2>(</FONT><FONT =
color=3D#0000ff=20
size=3D2>delegate</FONT><FONT size=3D2>()<BR> =
{<BR></FONT><FONT=20
color=3D#0000ff size=3D2> =
try<BR></FONT><FONT=20
size=3D2> {<BR></FONT><FONT =
color=3D#008080=20
size=3D2> =20
RecipeFile</FONT><FONT size=3D2> rf =3D </FONT><FONT color=3D#0000ff=20
size=3D2>new</FONT><FONT size=3D2> </FONT><FONT color=3D#008080=20
size=3D2>RecipeFile</FONT><FONT size=3D2>(file, </FONT><FONT =
color=3D#0000ff=20
size=3D2>false</FONT><FONT size=3D2>, SetStatus);<FONT color=3D#ff0000> =
// variable=20
declared & used in the same=20
block</FONT><BR> &nb=
sp; =20
SetStatus(doneMsg);<BR></FONT><FONT color=3D#0000ff =
size=3D2> =20
if</FONT><FONT size=3D2>=20
(!IsDisposed)<BR> =
=20
BeginInvoke(</FONT><FONT color=3D#0000ff =
size=3D2>new</FONT><FONT=20
size=3D2> </FONT><FONT color=3D#008080 =
size=3D2>InvokeDelegate</FONT><FONT=20
size=3D2>(</FONT><FONT color=3D#0000ff size=3D2>delegate</FONT><FONT=20
size=3D2>()<BR> =20
{<BR></FONT><FONT color=3D#008080 =
size=3D2> =20
=
=20
RecipeFile</FONT><FONT size=3D2> =3D rf;<FONT color=3D#ff0000> // used =
in this other=20
anonymous method &=20
thread<BR></FONT> &n=
bsp; =20
}));<BR> }<BR></FONT><FONT =
color=3D#0000ff=20
size=3D2> catch</FONT><FONT =
size=3D2>=20
(</FONT><FONT color=3D#008080 size=3D2>ArgumentException</FONT><FONT =
size=3D2>=20
e) { SetStatus(e.Message); }<BR></FONT><FONT =
color=3D#0000ff=20
size=3D2> catch</FONT><FONT =
size=3D2>=20
(</FONT><FONT color=3D#008080 size=3D2>SQLiteException</FONT><FONT =
size=3D2>=20
e) { SetStatus(e.Message); }<BR> =
=20
</FONT><FONT color=3D#0000ff size=3D2>finally<BR></FONT><FONT=20
size=3D2> =20
{<BR> =20
</FONT><FONT color=3D#0000ff size=3D2>if</FONT><FONT size=3D2>=20
(!IsDisposed)<BR> =
=20
BeginInvoke(</FONT><FONT color=3D#0000ff =
size=3D2>new</FONT><FONT=20
size=3D2> </FONT><FONT color=3D#008080 =
size=3D2>InvokeDelegate</FONT><FONT=20
size=3D2>(</FONT><FONT color=3D#0000ff size=3D2>delegate</FONT><FONT =
size=3D2>() {=20
</FONT><FONT color=3D#008080 size=3D2>Cursor</FONT><FONT size=3D2> =3D =
</FONT><FONT=20
color=3D#008080 size=3D2>Cursors</FONT><FONT size=3D2>.Default;=20
}));<BR> }<BR> =20
}<BR> ).Start();<BR>}</DIV></FONT>
<DIV><FONT face=3DArial size=3D2>=3D=3D=3D=3D=3D=3D=3D=3D=3D <FONT =
color=3D#800000>Crashing=20
version</FONT> =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D<BR><FONT face=3D"Times =
New Roman"><FONT=20
color=3D#0000ff>public</FONT><FONT size=3D2> </FONT><FONT =
color=3D#0000ff=20
size=3D2>void</FONT><FONT size=3D2> Open(</FONT><FONT color=3D#0000ff=20
size=3D2>string</FONT></FONT><FONT face=3D"Times New Roman"><FONT =
size=3D2>=20
file)<BR>{<BR></FONT><FONT color=3D#008080 size=3D2> =20
Cursor</FONT><FONT size=3D2> =3D </FONT><FONT color=3D#008080=20
size=3D2>Cursors</FONT></FONT><FONT face=3D"Times New Roman"><FONT=20
size=3D2>.WaitCursor;<BR></FONT><FONT color=3D#0000ff =
size=3D2> =20
string</FONT><FONT size=3D2> doneMsg =3D </FONT><FONT color=3D#008080=20
size=3D2>strings</FONT><FONT size=3D2>.done; </FONT></FONT><FONT=20
face=3D"Times New Roman"><FONT color=3D#808080 size=3D2>// initialize =
for Thread=20
safety<BR></FONT><FONT color=3D#0000ff size=3D2> =
new</FONT><FONT=20
size=3D2> </FONT><FONT color=3D#008080 size=3D2>Thread</FONT><FONT=20
size=3D2>(</FONT><FONT color=3D#0000ff =
size=3D2>delegate</FONT></FONT><FONT=20
face=3D"Times New Roman" size=3D2>()<BR> =
{<BR></FONT><FONT=20
face=3D"Times New Roman" color=3D#0000ff size=3D2><FONT=20
color=3D#008080> RecipeFil=
e</FONT><FONT=20
color=3D#000000 size=3D2> rf; <FONT color=3D#ff0000>// variable declared =
here</FONT></FONT><FONT face=3D"Times New Roman"><FONT=20
size=3D2><BR></FONT></FONT> =20
try<BR></FONT><FONT face=3D"Times New Roman"><FONT =
size=3D2> =20
{<BR></FONT><FONT color=3D#008080=20
size=3D2> &nbs=
p;=20
</FONT><FONT size=3D2>rf =3D </FONT><FONT color=3D#0000ff =
size=3D2>new</FONT><FONT=20
size=3D2> </FONT><FONT color=3D#008080 size=3D2>RecipeFile</FONT><FONT =
size=3D2>(file,=20
</FONT><FONT color=3D#0000ff size=3D2>false</FONT></FONT><FONT=20
face=3D"Times New Roman"><FONT size=3D2>, =
SetStatus);</FONT></FONT></FONT></DIV>
<DIV><FONT face=3DArial size=3D2><FONT face=3D"Times New Roman"><FONT=20
size=3D2> &nbs=
p;=20
SetStatus(doneMsg);<BR></FONT><FONT color=3D#0000ff =
size=3D2> =20
if</FONT></FONT><FONT=20
face=3D"Times New Roman"><FONT size=3D2> =
(!IsDisposed)<BR> =20
=20
BeginInvoke(</FONT><FONT color=3D#0000ff size=3D2>new</FONT><FONT =
size=3D2>=20
</FONT><FONT color=3D#008080 size=3D2>InvokeDelegate</FONT><FONT=20
size=3D2>(</FONT><FONT color=3D#0000ff =
size=3D2>delegate</FONT></FONT><FONT=20
face=3D"Times New Roman"><FONT size=3D2>()<BR> =
=20
{<BR></FONT><FONT color=3D#008080=20
size=3D2> =20
RecipeFile</FONT></FONT><FONT=20
face=3D"Times New Roman"><FONT size=3D2> =3D rf;<FONT color=3D#ff0000> =
// used in this=20
other anonymous method &=20
thread</FONT><BR> &n=
bsp; =20
}));<BR> }<BR></FONT><FONT =
color=3D#0000ff=20
size=3D2> catch</FONT><FONT =
size=3D2>=20
(</FONT><FONT color=3D#008080 =
size=3D2>ArgumentException</FONT></FONT><FONT=20
face=3D"Times New Roman"><FONT size=3D2>=20
e) { SetStatus(e.Message); }<BR></FONT><FONT =
color=3D#0000ff=20
size=3D2> catch</FONT><FONT =
size=3D2>=20
(</FONT><FONT color=3D#008080 =
size=3D2>SQLiteException</FONT></FONT><FONT=20
face=3D"Times New Roman" size=3D2> e) { SetStatus(e.Message);=20
}<BR> </FONT><FONT face=3D"Times =
New Roman"=20
color=3D#0000ff size=3D2>finally<BR></FONT><FONT face=3D"Times New =
Roman"><FONT=20
size=3D2> =20
{<BR> =20
</FONT><FONT color=3D#0000ff size=3D2>if</FONT></FONT><FONT=20
face=3D"Times New Roman"><FONT size=3D2> =
(!IsDisposed)<BR> =20
=20
BeginInvoke(</FONT><FONT color=3D#0000ff size=3D2>new</FONT><FONT =
size=3D2>=20
</FONT><FONT color=3D#008080 size=3D2>InvokeDelegate</FONT><FONT=20
size=3D2>(</FONT><FONT color=3D#0000ff size=3D2>delegate</FONT><FONT =
size=3D2>() {=20
</FONT><FONT color=3D#008080 size=3D2>Cursor</FONT><FONT size=3D2> =3D =
</FONT><FONT=20
color=3D#008080 size=3D2>Cursors</FONT></FONT><FONT face=3D"Times New =
Roman"=20
size=3D2>.Default; }));<BR> =20
}<BR> }<BR> =20
).Start();<BR>}</FONT></FONT></DIV>
<DIV><FONT face=3DArial =
size=3D2>=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D</FONT></DIV>
<DIV><FONT face=3DArial size=3D2></FONT> </DIV>
<DIV><FONT face=3DArial size=3D2></FONT> </DIV>
<DIV><BR>-- <BR>If you're in a war, instead of throwing a hand grenade =
at the=20
enemy, throw one of those small pumpkins. Maybe it'll make everyone =
think how=20
stupid war is, and while they are thinking, you can throw a real grenade =
at=20
them.<BR>Jack Handey.</DIV>
<BLOCKQUOTE dir=3Dltr=20
style=3D"PADDING-RIGHT: 0px; PADDING-LEFT: 5px; MARGIN-LEFT: 5px; =
BORDER-LEFT: #000000 2px solid; MARGIN-RIGHT: 0px">
<DIV>"Willy Denoyette [MVP]" <<A=20
=
href=3D"mailto:willy.denoyette@telenet.be">willy.denoyette@telenet.be</A>=
>=20
wrote in message <A=20
=
href=3D"news:%23ioFdtwsFHA.3040@TK2MSFTNGP14.phx.gbl">news:%23ioFdtwsFHA.=
3040@TK2MSFTNGP14.phx.gbl</A>...</DIV>
<DIV><FONT face=3DTahoma size=3D2>What exact version og the framework =
are you=20
running?</FONT></DIV>
<DIV><FONT face=3DTahoma size=3D2>This works for me using =
2.0.50727.</FONT></DIV>
<DIV><FONT face=3DTahoma size=3D2></FONT> </DIV>
<DIV><FONT face=3DTahoma size=3D2>Willy.</FONT></DIV>
<DIV> </DIV>
<DIV> </DIV>
<DIV>"Lloyd Dupont" <<A=20
=
href=3D"mailto:ld@NewsAccount.galador.net">ld@NewsAccount.galador.net</A>=
>=20
wrote in message <A=20
=
href=3D"news:uJk%23govsFHA.3216@TK2MSFTNGP12.phx.gbl">news:uJk%23govsFHA.=
3216@TK2MSFTNGP12.phx.gbl</A>...</DIV>
<BLOCKQUOTE dir=3Dltr=20
style=3D"PADDING-RIGHT: 0px; PADDING-LEFT: 5px; MARGIN-LEFT: 5px; =
BORDER-LEFT: #000000 2px solid; MARGIN-RIGHT: 0px">
<DIV><FONT face=3DArial size=3D2>I have a very simple block of code =
with open a=20
file in a thread</FONT></DIV>
<DIV><FONT face=3DArial size=3D2>and for the life of me I have no =
idea, wonder=20
if it's a