Threads. Do I need a lesson?

  • Thread starter Thread starter fhunter
  • Start date Start date
F

fhunter

Hi. I am having some problems with threads and I was
wondering if someone can enlight me. This is one of those
problems with threads that are not easy to emulate on a
small sample code :( :(...


Essentially I have threads in my code that will call a
method (in an foreground class) to do something.
For example in one case the "event called" cancels a
timer. It sets timer.Enabled to false. This cancels an
animation that's playing in the foreground.
On a different case the "event called" draws an image
(background) to a Bitmap that has been set up as a
backbuffer.

Neither one of these cases (and in the emulator only! not
the actual hardware!!! ) seems to want to work.

The fist one, for example, where the timer.Enabled =
false. It just does not seem to turn the timer off.

The second simply locks up locking the thread with it :(

Now I can set it up to where the "Event called" simply
set a boolean. Then when the application runs through the
next update it checks the boolean and it does exactly the
same work that was being done with the "Event called" and
then everything works fine.

Has any one seen a similar behaviour? Am I missing
something fundamental about threads?

Thanks in advance
 
You must use Control.Invoke() method when calling methods of a control
from a different thread than the control thread (the thread in which
the control has been created).
 
Thanks,
This is definetly useful info.
Unfotunately looks like Control.Invoke locks up !

Are there any obvious reasons why this would happen !

Note it never actually goes inside the Event being
called. The Invoke() call it self locks up.

Thanks
 
Thanks Alex,

Actually I do use EventHandler not custom delegates. I
wish I could use a custom one though, I would make things
alot easier :) :).

Any other ideas?
 
Thanks Alex,

I think I found the problem after creating a small sample
project (There are some references to this in the
newgroup but I don't think any one has posted an answer)

I believe the problem I have is that I am using ShowDialog
() to display my form. On which I call Invoke.

If you use Show() things seem to work a bit better.

How can I resolve this issue?
I don't think I can use Show() I need to use ShowDialog().
Why does it not work with ShowDialog() ? That doesn't
make much sence.

Thanks in advance
 
V1 RTM of the .NET Compact Framework has a known bug where Control.Invoke
waits until a dialogs are closed. This is fixed in SP1 which is available
now at the Microsoft web site.

David Wrighton
.NET Compact Framework

This posting is provided "AS IS" with no warranties, and confers no rights.
--------------------
| Content-Class: urn:content-classes:message
| From: "fhunter" <[email protected]>
| Sender: "fhunter" <[email protected]>
| References: <[email protected]>
<[email protected]>
<[email protected]>
<[email protected]>
<[email protected]>
<#[email protected]>
<[email protected]>
| Subject: Re: Threads. Do I need a lesson?
| Date: Thu, 13 Nov 2003 19:09:46 -0800
| Lines: 197
| Message-ID: <[email protected]>
| MIME-Version: 1.0
| Content-Type: text/plain;
| charset="iso-8859-1"
| Content-Transfer-Encoding: 7bit
| X-Newsreader: Microsoft CDO for Windows 2000
| X-MIMEOLE: Produced By Microsoft MimeOLE V5.50.4910.0300
| Thread-Index: AcOqXMIIwQX3hh1ZTwWtzmARadktsg==
| Newsgroups: microsoft.public.dotnet.framework.compactframework
| Path: cpmsftngxa06.phx.gbl
| Xref: cpmsftngxa06.phx.gbl
microsoft.public.dotnet.framework.compactframework:38563
| NNTP-Posting-Host: TK2MSFTNGXA11 10.40.1.163
| X-Tomcat-NG: microsoft.public.dotnet.framework.compactframework
|
| Alex, I should be more specific on what I did.
|
| I took the following example:
| http://msdn.microsoft.com/library/default.asp?
| url=/library/en-
| us/dnnetcomp/html/netcfmultithreadedapp.asp
|
| And it works fine. However. I took it one step further.
| Instead of doing all the "Thread stuff" in the the main
| form I created a new form and included the code as
| specified in the example.
|
| Then from the main form I called:
|
| new MyForm().ShowDialog();
|
| When you do this the following code (the method executed
| by the thread):
|
| public void UpdateThread()
| {
| for(int j = 0; j < 5; j++)
| {
| this.Invoke(new EventHandler(WorkerUpdate));
| Thread.Sleep(700);
| }
| }
|
| Gets called properly and the Invoke method executes fine
| the first time. Then it sleeps for 700millis and when it
| tries to call the Invoke method the second time it locks
| up and never returns.
|
| Why?
|
| Thanks
|
|
|
|
|
|
|
| Ok I took the example in
| >-----Original Message-----
| >Thanks Alex,
| >
| >I think I found the problem after creating a small
| sample
| >project (There are some references to this in the
| >newgroup but I don't think any one has posted an answer)
| >
| >I believe the problem I have is that I am using
| ShowDialog
| >() to display my form. On which I call Invoke.
| >
| >If you use Show() things seem to work a bit better.
| >
| >How can I resolve this issue?
| >I don't think I can use Show() I need to use ShowDialog
| ().
| >Why does it not work with ShowDialog() ? That doesn't
| >make much sence.
| >
| >Thanks in advance
| >
| >
| >
| >>-----Original Message-----
| >>I suppose at this point code snippet is in order...
| >>
| in
| >message
| >>| >>> Thanks Alex,
| >>>
| >>> Actually I do use EventHandler not custom delegates. I
| >>> wish I could use a custom one though, I would make
| >things
| >>> alot easier :) :).
| >>>
| >>> Any other ideas?
| >>>
| >>> >-----Original Message-----
| >>> >Take a look at
| >>>
| >>http://msdn.microsoft.com/mobility/prodtechinfo/devtools
| /
| >>> netcf/FAQ/default.aspx#7.10
| >>> >
| >>> >"fhunter" <[email protected]>
| wrote
| >in
| >>> message
| >>> >| >>> >> Thanks,
| >>> >> This is definetly useful info.
| >>> >> Unfotunately looks like Control.Invoke locks up !
| >>> >>
| >>> >> Are there any obvious reasons why this would
| >happen !
| >>> >>
| >>> >> Note it never actually goes inside the Event being
| >>> >> called. The Invoke() call it self locks up.
| >>> >>
| >>> >> Thanks
| >>> >>
| >>> >>
| >>> >> >-----Original Message-----
| >>> >> >You must use Control.Invoke() method when calling
| >>> >> methods of a control
| >>> >> >from a different thread than the control thread
| >(the
| >>> >> thread in which
| >>> >> >the control has been created).
| >>> >> >
| >>> >> >"fhunter" <[email protected]>
| >wrote
| >>> in
| >>> >> message | >>> [email protected]>...
| >>> >> >> Hi. I am having some problems with threads and
| I
| >was
| >>> >> >> wondering if someone can enlight me. This is
| one
| >of
| >>> >> those
| >>> >> >> problems with threads that are not easy to
| >emulate
| >>> on
| >>> >> a
| >>> >> >> small sample code :( :(...
| >>> >> >>
| >>> >> >>
| >>> >> >> Essentially I have threads in my code that will
| >>> call a
| >>> >> >> method (in an foreground class) to do something.
| >>> >> >> For example in one case the "event called"
| >cancels a
| >>> >> >> timer. It sets timer.Enabled to false. This
| >cancels
| >>> an
| >>> >> >> animation that's playing in the foreground.
| >>> >> >> On a different case the "event called" draws an
| >>> image
| >>> >> >> (background) to a Bitmap that has been set up
| as
| >a
| >>> >> >> backbuffer.
| >>> >> >>
| >>> >> >> Neither one of these cases (and in the emulator
| >>> only!
| >>> >> not
| >>> >> >> the actual hardware!!! ) seems to want to work.
| >>> >> >>
| >>> >> >> The fist one, for example, where the
| >timer.Enabled =
| >>> >> >> false. It just does not seem to turn the timer
| >off.
| >>> >> >>
| >>> >> >> The second simply locks up locking the thread
| >with
| >>> it :
| >>> >> (
| >>> >> >>
| >>> >> >> Now I can set it up to where the "Event called"
| >>> simply
| >>> >> >> set a boolean. Then when the application runs
| >>> through
| >>> >> the
| >>> >> >> next update it checks the boolean and it does
| >>> exactly
| >>> >> the
| >>> >> >> same work that was being done with the "Event
| >>> called"
| >>> >> and
| >>> >> >> then everything works fine.
| >>> >> >>
| >>> >> >> Has any one seen a similar behaviour? Am I
| >missing
| >>> >> >> something fundamental about threads?
| >>> >> >>
| >>> >> >> Thanks in advance
| >>> >> >.
| >>> >> >
| >>> >
| >>> >
| >>> >.
| >>> >
| >>
| >>
| >>.
| >>
| >.
| >
|
 
Back
Top