473,406 Members | 2,371 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

App sporadically hangs while raising event while processing system.threading.timer event

Dan
I've created a pocketpc app which has a startup form containing a listview.
The form creates an object which in turn creates a System.Threading.Timer.
It keeps track of the Timer state using a TimerState object similar to the
example in the System.Threading.Timer documentation. The method which
handles the timer events, among other things, periodically calls a method in
this TimerState object which raises an event to the startup form, which in
turn adds a record to the listview. Sporadically this code in the startup
form hangs when it tries to add the record to the listview:

listView1.Items.Add(lvi).

I first noticed this happening when I happend to dock the iPAQ while it was
adding an item. The ActiveSync messagebox came up and hung, as did my
process. I am now able to reproduce it at will by adding a MessageBox call
prior to raising the event. The messagebox call returns but the messagebox
itself remains displayed on the screen, and my process hangs at the line
mentioned above.

Anyone have any thoughts on what's going on? The details of the
implementation are described below....Dan

1. Form1 creates an UploadLooper object.

2. UploadLooper creates a TimerState object, similar to the example in
the System.Threading.Timer documentation.

3. The UploadLooper module also contains a delegate declaration, along
with a declaration for an UploadEvent object derived from EventArgs:

public delegate void StatusEvent(Object sender, UploadEvent e);

4. The TimerState object declares the event and contains a RaiseStatus
method which enables us to fire StatusEvent:

public class TimerState

{

public event StatusEvent StatusMessage; // Declaration of the event

....

public void RaiseStatus(string message)

{

Tracer.WriteLine("TimerState.RaiseStatus", "Before raising event");

if (StatusMessage != null)

StatusMessage(this, new UploadEvent(message));

Tracer.WriteLine("TimerState.RaiseStatus", "After raising event");

}

5. The TimerState object also contains a Timer property itself, which
references the Timer we are going to create. We start the timer via the
UploadLooper.StartTimer method as follows:

public void StartTimer(uint millisecondInterval)

{

// Create the delegate that handles the timer events

System.Threading.TimerCallback timerDelegate = new
TimerCallback(CheckStatus);

// Create a timer that waits one second, then invokes every
'millisecondInterval' milliseconds

// and give TimerState a reference to the timer so we can dispose it when
necessary

m_TimerState.TimerInterval = millisecondInterval;

m_TimerState.Timer = new System.Threading.Timer(timerDelegate, m_TimerState,
(uint) 5000, millisecondInterval);

}

6. The CheckStatus method gets called when the Timer fires, and casts
the object it receives to a TimerState object:

static void CheckStatus(Object state)

{

TimerState timerState = (TimerState) state;

timerState.Timer.Change(Timeout.Infinite, Timeout.Infinite); // Disable the
timer

7. CheckStatus then goes on to do its processing. Periodically, calls
the TimerState.RaiseStatus method, which raises an event. Form1 catches the
event and adds a ListViewItem to a ListView:

private void AddNewStatus(string message)

{

Tracer.WriteLine("Form1.AddNewStatus", "Entering sub. Message: " + message);

ListViewItem lvi = new ListViewItem(DateTime.Now.ToString("hh:mm:ss"));

Tracer.WriteLine("Form1.AddNewStatus", "After creating lvi");

lvi.SubItems.Add(message);

Tracer.WriteLine("Form1.AddNewStatus", "After adding subitem");

listView1.Items.Add(lvi);

Tracer.WriteLine("Form1.AddNewStatus", "After adding lvi to listView");

listView1.Refresh();

Tracer.WriteLine("Form1.AddNewStatus", "Exiting sub.");

}

8. Sporadically, the AddNewStatus method hangs on
listView1.Items.Add(lvi). I first noticed this happening when I happend to
dock the iPAQ while it was adding an item. The ActiveSync messagebox came
up and hung, as did my process. I am now able to reproduce it at will by
adding a MessageBox call prior to raising an event. The messagebox call
returns but the messagebox itself remains displayed on the screen, and my
process hangs at the line mentioned above.


Nov 16 '05 #1
6 2831
Well... and now for the hundred's time... You're trying to update the UI
thread from a different (Timer's) thread. You MUST use Control.Invoke in
order to do that.

--
Alex Yakhnin, .NET CF MVP
www.intelliprog.com
www.opennetcf.org

"Dan" wrote:
I've created a pocketpc app which has a startup form containing a listview.
The form creates an object which in turn creates a System.Threading.Timer.
It keeps track of the Timer state using a TimerState object similar to the
example in the System.Threading.Timer documentation. The method which
handles the timer events, among other things, periodically calls a method in
this TimerState object which raises an event to the startup form, which in
turn adds a record to the listview. Sporadically this code in the startup
form hangs when it tries to add the record to the listview:

listView1.Items.Add(lvi).

I first noticed this happening when I happend to dock the iPAQ while it was
adding an item. The ActiveSync messagebox came up and hung, as did my
process. I am now able to reproduce it at will by adding a MessageBox call
prior to raising the event. The messagebox call returns but the messagebox
itself remains displayed on the screen, and my process hangs at the line
mentioned above.

Anyone have any thoughts on what's going on? The details of the
implementation are described below....Dan

1. Form1 creates an UploadLooper object.

2. UploadLooper creates a TimerState object, similar to the example in
the System.Threading.Timer documentation.

3. The UploadLooper module also contains a delegate declaration, along
with a declaration for an UploadEvent object derived from EventArgs:

public delegate void StatusEvent(Object sender, UploadEvent e);

4. The TimerState object declares the event and contains a RaiseStatus
method which enables us to fire StatusEvent:

public class TimerState

{

public event StatusEvent StatusMessage; // Declaration of the event

....

public void RaiseStatus(string message)

{

Tracer.WriteLine("TimerState.RaiseStatus", "Before raising event");

if (StatusMessage != null)

StatusMessage(this, new UploadEvent(message));

Tracer.WriteLine("TimerState.RaiseStatus", "After raising event");

}

5. The TimerState object also contains a Timer property itself, which
references the Timer we are going to create. We start the timer via the
UploadLooper.StartTimer method as follows:

public void StartTimer(uint millisecondInterval)

{

// Create the delegate that handles the timer events

System.Threading.TimerCallback timerDelegate = new
TimerCallback(CheckStatus);

// Create a timer that waits one second, then invokes every
'millisecondInterval' milliseconds

// and give TimerState a reference to the timer so we can dispose it when
necessary

m_TimerState.TimerInterval = millisecondInterval;

m_TimerState.Timer = new System.Threading.Timer(timerDelegate, m_TimerState,
(uint) 5000, millisecondInterval);

}

6. The CheckStatus method gets called when the Timer fires, and casts
the object it receives to a TimerState object:

static void CheckStatus(Object state)

{

TimerState timerState = (TimerState) state;

timerState.Timer.Change(Timeout.Infinite, Timeout.Infinite); // Disable the
timer

7. CheckStatus then goes on to do its processing. Periodically, calls
the TimerState.RaiseStatus method, which raises an event. Form1 catches the
event and adds a ListViewItem to a ListView:

private void AddNewStatus(string message)

{

Tracer.WriteLine("Form1.AddNewStatus", "Entering sub. Message: " + message);

ListViewItem lvi = new ListViewItem(DateTime.Now.ToString("hh:mm:ss"));

Tracer.WriteLine("Form1.AddNewStatus", "After creating lvi");

lvi.SubItems.Add(message);

Tracer.WriteLine("Form1.AddNewStatus", "After adding subitem");

listView1.Items.Add(lvi);

Tracer.WriteLine("Form1.AddNewStatus", "After adding lvi to listView");

listView1.Refresh();

Tracer.WriteLine("Form1.AddNewStatus", "Exiting sub.");

}

8. Sporadically, the AddNewStatus method hangs on
listView1.Items.Add(lvi). I first noticed this happening when I happend to
dock the iPAQ while it was adding an item. The ActiveSync messagebox came
up and hung, as did my process. I am now able to reproduce it at will by
adding a MessageBox call prior to raising an event. The messagebox call
returns but the messagebox itself remains displayed on the screen, and my
process hangs at the line mentioned above.


Nov 16 '05 #2
You cannot affect the UI from a worker thread, which the Thread.Timer is.
You *must* use Invoke to marshal calls to the UI.

<ctacke/>
www.opennetcf.org
"Dan" <da*@dontspamme.com> wrote in message
news:%2****************@TK2MSFTNGP14.phx.gbl...
I've created a pocketpc app which has a startup form containing a listview. The form creates an object which in turn creates a System.Threading.Timer.
It keeps track of the Timer state using a TimerState object similar to the
example in the System.Threading.Timer documentation. The method which
handles the timer events, among other things, periodically calls a method in this TimerState object which raises an event to the startup form, which in
turn adds a record to the listview. Sporadically this code in the startup
form hangs when it tries to add the record to the listview:

listView1.Items.Add(lvi).

I first noticed this happening when I happend to dock the iPAQ while it was adding an item. The ActiveSync messagebox came up and hung, as did my
process. I am now able to reproduce it at will by adding a MessageBox call prior to raising the event. The messagebox call returns but the messagebox itself remains displayed on the screen, and my process hangs at the line
mentioned above.

Anyone have any thoughts on what's going on? The details of the
implementation are described below....Dan

1. Form1 creates an UploadLooper object.

2. UploadLooper creates a TimerState object, similar to the example in the System.Threading.Timer documentation.

3. The UploadLooper module also contains a delegate declaration, along with a declaration for an UploadEvent object derived from EventArgs:

public delegate void StatusEvent(Object sender, UploadEvent e);

4. The TimerState object declares the event and contains a RaiseStatus method which enables us to fire StatusEvent:

public class TimerState

{

public event StatusEvent StatusMessage; // Declaration of the event

...

public void RaiseStatus(string message)

{

Tracer.WriteLine("TimerState.RaiseStatus", "Before raising event");

if (StatusMessage != null)

StatusMessage(this, new UploadEvent(message));

Tracer.WriteLine("TimerState.RaiseStatus", "After raising event");

}

5. The TimerState object also contains a Timer property itself, which
references the Timer we are going to create. We start the timer via the
UploadLooper.StartTimer method as follows:

public void StartTimer(uint millisecondInterval)

{

// Create the delegate that handles the timer events

System.Threading.TimerCallback timerDelegate = new
TimerCallback(CheckStatus);

// Create a timer that waits one second, then invokes every
'millisecondInterval' milliseconds

// and give TimerState a reference to the timer so we can dispose it when
necessary

m_TimerState.TimerInterval = millisecondInterval;

m_TimerState.Timer = new System.Threading.Timer(timerDelegate, m_TimerState, (uint) 5000, millisecondInterval);

}

6. The CheckStatus method gets called when the Timer fires, and casts
the object it receives to a TimerState object:

static void CheckStatus(Object state)

{

TimerState timerState = (TimerState) state;

timerState.Timer.Change(Timeout.Infinite, Timeout.Infinite); // Disable the timer

7. CheckStatus then goes on to do its processing. Periodically, calls the TimerState.RaiseStatus method, which raises an event. Form1 catches the event and adds a ListViewItem to a ListView:

private void AddNewStatus(string message)

{

Tracer.WriteLine("Form1.AddNewStatus", "Entering sub. Message: " + message);
ListViewItem lvi = new ListViewItem(DateTime.Now.ToString("hh:mm:ss"));

Tracer.WriteLine("Form1.AddNewStatus", "After creating lvi");

lvi.SubItems.Add(message);

Tracer.WriteLine("Form1.AddNewStatus", "After adding subitem");

listView1.Items.Add(lvi);

Tracer.WriteLine("Form1.AddNewStatus", "After adding lvi to listView");

listView1.Refresh();

Tracer.WriteLine("Form1.AddNewStatus", "Exiting sub.");

}

8. Sporadically, the AddNewStatus method hangs on
listView1.Items.Add(lvi). I first noticed this happening when I happend to dock the iPAQ while it was adding an item. The ActiveSync messagebox came
up and hung, as did my process. I am now able to reproduce it at will by
adding a MessageBox call prior to raising an event. The messagebox call
returns but the messagebox itself remains displayed on the screen, and my
process hangs at the line mentioned above.


Nov 16 '05 #3
Dan
If that is true, then why does this code *ever* work? (It almost always
does).

Also-- can you point me to a good article on Invoke? Thanks very much...Dan

"Chris Tacke, eMVP" <ct****@spamfree-opennetcf.org> wrote in message
news:uH**************@TK2MSFTNGP15.phx.gbl...
You cannot affect the UI from a worker thread, which the Thread.Timer is.
You *must* use Invoke to marshal calls to the UI.

<ctacke/>
www.opennetcf.org
"Dan" <da*@dontspamme.com> wrote in message
news:%2****************@TK2MSFTNGP14.phx.gbl...
I've created a pocketpc app which has a startup form containing a listview.
The form creates an object which in turn creates a System.Threading.Timer. It keeps track of the Timer state using a TimerState object similar to the example in the System.Threading.Timer documentation. The method which
handles the timer events, among other things, periodically calls a method in
this TimerState object which raises an event to the startup form, which

in turn adds a record to the listview. Sporadically this code in the startup form hangs when it tries to add the record to the listview:

listView1.Items.Add(lvi).

I first noticed this happening when I happend to dock the iPAQ while it

was
adding an item. The ActiveSync messagebox came up and hung, as did my
process. I am now able to reproduce it at will by adding a MessageBox

call
prior to raising the event. The messagebox call returns but the

messagebox
itself remains displayed on the screen, and my process hangs at the line
mentioned above.

Anyone have any thoughts on what's going on? The details of the
implementation are described below....Dan

1. Form1 creates an UploadLooper object.

2. UploadLooper creates a TimerState object, similar to the example

in
the System.Threading.Timer documentation.

3. The UploadLooper module also contains a delegate declaration,

along
with a declaration for an UploadEvent object derived from EventArgs:

public delegate void StatusEvent(Object sender, UploadEvent e);

4. The TimerState object declares the event and contains a

RaiseStatus
method which enables us to fire StatusEvent:

public class TimerState

{

public event StatusEvent StatusMessage; // Declaration of the event

...

public void RaiseStatus(string message)

{

Tracer.WriteLine("TimerState.RaiseStatus", "Before raising event");

if (StatusMessage != null)

StatusMessage(this, new UploadEvent(message));

Tracer.WriteLine("TimerState.RaiseStatus", "After raising event");

}

5. The TimerState object also contains a Timer property itself, which references the Timer we are going to create. We start the timer via the
UploadLooper.StartTimer method as follows:

public void StartTimer(uint millisecondInterval)

{

// Create the delegate that handles the timer events

System.Threading.TimerCallback timerDelegate = new
TimerCallback(CheckStatus);

// Create a timer that waits one second, then invokes every
'millisecondInterval' milliseconds

// and give TimerState a reference to the timer so we can dispose it when necessary

m_TimerState.TimerInterval = millisecondInterval;

m_TimerState.Timer = new System.Threading.Timer(timerDelegate,

m_TimerState,
(uint) 5000, millisecondInterval);

}

6. The CheckStatus method gets called when the Timer fires, and casts the object it receives to a TimerState object:

static void CheckStatus(Object state)

{

TimerState timerState = (TimerState) state;

timerState.Timer.Change(Timeout.Infinite, Timeout.Infinite); // Disable

the
timer

7. CheckStatus then goes on to do its processing. Periodically,

calls
the TimerState.RaiseStatus method, which raises an event. Form1 catches

the
event and adds a ListViewItem to a ListView:

private void AddNewStatus(string message)

{

Tracer.WriteLine("Form1.AddNewStatus", "Entering sub. Message: " +

message);

ListViewItem lvi = new ListViewItem(DateTime.Now.ToString("hh:mm:ss"));

Tracer.WriteLine("Form1.AddNewStatus", "After creating lvi");

lvi.SubItems.Add(message);

Tracer.WriteLine("Form1.AddNewStatus", "After adding subitem");

listView1.Items.Add(lvi);

Tracer.WriteLine("Form1.AddNewStatus", "After adding lvi to listView");

listView1.Refresh();

Tracer.WriteLine("Form1.AddNewStatus", "Exiting sub.");

}

8. Sporadically, the AddNewStatus method hangs on
listView1.Items.Add(lvi). I first noticed this happening when I happend

to
dock the iPAQ while it was adding an item. The ActiveSync messagebox came up and hung, as did my process. I am now able to reproduce it at will by adding a MessageBox call prior to raising an event. The messagebox call
returns but the messagebox itself remains displayed on the screen, and my process hangs at the line mentioned above.



Nov 16 '05 #4
You are probably suffering the side effect of touching GUI controls from
non-GUI threads.
In your case a listview from a threading.timer. The answer is to use
Control.Invoke.

There is an explanation with links here:
http://www.zen13120.zen.co.uk/Blog/2...d-full-fx.html

Cheers
Daniel

"Dan" <da*@dontspamme.com> wrote in message
news:%2****************@TK2MSFTNGP14.phx.gbl...
I've created a pocketpc app which has a startup form containing a
listview.
The form creates an object which in turn creates a System.Threading.Timer.
It keeps track of the Timer state using a TimerState object similar to the
example in the System.Threading.Timer documentation. The method which
handles the timer events, among other things, periodically calls a method
in
this TimerState object which raises an event to the startup form, which in
turn adds a record to the listview. Sporadically this code in the startup
form hangs when it tries to add the record to the listview:

listView1.Items.Add(lvi).

I first noticed this happening when I happend to dock the iPAQ while it
was
adding an item. The ActiveSync messagebox came up and hung, as did my
process. I am now able to reproduce it at will by adding a MessageBox
call
prior to raising the event. The messagebox call returns but the
messagebox
itself remains displayed on the screen, and my process hangs at the line
mentioned above.

Anyone have any thoughts on what's going on? The details of the
implementation are described below....Dan

1. Form1 creates an UploadLooper object.

2. UploadLooper creates a TimerState object, similar to the example
in
the System.Threading.Timer documentation.

3. The UploadLooper module also contains a delegate declaration,
along
with a declaration for an UploadEvent object derived from EventArgs:

public delegate void StatusEvent(Object sender, UploadEvent e);

4. The TimerState object declares the event and contains a
RaiseStatus
method which enables us to fire StatusEvent:

public class TimerState

{

public event StatusEvent StatusMessage; // Declaration of the event

...

public void RaiseStatus(string message)

{

Tracer.WriteLine("TimerState.RaiseStatus", "Before raising event");

if (StatusMessage != null)

StatusMessage(this, new UploadEvent(message));

Tracer.WriteLine("TimerState.RaiseStatus", "After raising event");

}

5. The TimerState object also contains a Timer property itself, which
references the Timer we are going to create. We start the timer via the
UploadLooper.StartTimer method as follows:

public void StartTimer(uint millisecondInterval)

{

// Create the delegate that handles the timer events

System.Threading.TimerCallback timerDelegate = new
TimerCallback(CheckStatus);

// Create a timer that waits one second, then invokes every
'millisecondInterval' milliseconds

// and give TimerState a reference to the timer so we can dispose it when
necessary

m_TimerState.TimerInterval = millisecondInterval;

m_TimerState.Timer = new System.Threading.Timer(timerDelegate,
m_TimerState,
(uint) 5000, millisecondInterval);

}

6. The CheckStatus method gets called when the Timer fires, and casts
the object it receives to a TimerState object:

static void CheckStatus(Object state)

{

TimerState timerState = (TimerState) state;

timerState.Timer.Change(Timeout.Infinite, Timeout.Infinite); // Disable
the
timer

7. CheckStatus then goes on to do its processing. Periodically,
calls
the TimerState.RaiseStatus method, which raises an event. Form1 catches
the
event and adds a ListViewItem to a ListView:

private void AddNewStatus(string message)

{

Tracer.WriteLine("Form1.AddNewStatus", "Entering sub. Message: " +
message);

ListViewItem lvi = new ListViewItem(DateTime.Now.ToString("hh:mm:ss"));

Tracer.WriteLine("Form1.AddNewStatus", "After creating lvi");

lvi.SubItems.Add(message);

Tracer.WriteLine("Form1.AddNewStatus", "After adding subitem");

listView1.Items.Add(lvi);

Tracer.WriteLine("Form1.AddNewStatus", "After adding lvi to listView");

listView1.Refresh();

Tracer.WriteLine("Form1.AddNewStatus", "Exiting sub.");

}

8. Sporadically, the AddNewStatus method hangs on
listView1.Items.Add(lvi). I first noticed this happening when I happend
to
dock the iPAQ while it was adding an item. The ActiveSync messagebox came
up and hung, as did my process. I am now able to reproduce it at will by
adding a MessageBox call prior to raising an event. The messagebox call
returns but the messagebox itself remains displayed on the screen, and my
process hangs at the line mentioned above.


Nov 16 '05 #5
It works out of pure luck. The issue is there's a lock condition that may
or may not be in a safe state when you access the UI, so sometimes it will
work, other times it won't.

http://www.demsey.org/blog/PermaLink...8-837c35e7d26c

--
<ctacke/>
www.OpenNETCF.org
Your CF searches start and end here
"Dan" <da*@dontspamme.com> wrote in message
news:Om**************@TK2MSFTNGP15.phx.gbl...
If that is true, then why does this code *ever* work? (It almost always
does).

Also-- can you point me to a good article on Invoke? Thanks very much...Dan
"Chris Tacke, eMVP" <ct****@spamfree-opennetcf.org> wrote in message
news:uH**************@TK2MSFTNGP15.phx.gbl...
You cannot affect the UI from a worker thread, which the Thread.Timer is.
You *must* use Invoke to marshal calls to the UI.

<ctacke/>
www.opennetcf.org
"Dan" <da*@dontspamme.com> wrote in message
news:%2****************@TK2MSFTNGP14.phx.gbl...
I've created a pocketpc app which has a startup form containing a listview.
The form creates an object which in turn creates a System.Threading.Timer. It keeps track of the Timer state using a TimerState object similar to the example in the System.Threading.Timer documentation. The method which
handles the timer events, among other things, periodically calls a method
in
this TimerState object which raises an event to the startup form, which in turn adds a record to the listview. Sporadically this code in the startup form hangs when it tries to add the record to the listview:

listView1.Items.Add(lvi).

I first noticed this happening when I happend to dock the iPAQ while
it was
adding an item. The ActiveSync messagebox came up and hung, as did my
process. I am now able to reproduce it at will by adding a MessageBox

call
prior to raising the event. The messagebox call returns but the

messagebox
itself remains displayed on the screen, and my process hangs at the
line mentioned above.

Anyone have any thoughts on what's going on? The details of the
implementation are described below....Dan

1. Form1 creates an UploadLooper object.

2. UploadLooper creates a TimerState object, similar to the example in
the System.Threading.Timer documentation.

3. The UploadLooper module also contains a delegate declaration,

along
with a declaration for an UploadEvent object derived from EventArgs:

public delegate void StatusEvent(Object sender, UploadEvent e);

4. The TimerState object declares the event and contains a

RaiseStatus
method which enables us to fire StatusEvent:

public class TimerState

{

public event StatusEvent StatusMessage; // Declaration of the
event
...

public void RaiseStatus(string message)

{

Tracer.WriteLine("TimerState.RaiseStatus", "Before raising event");

if (StatusMessage != null)

StatusMessage(this, new UploadEvent(message));

Tracer.WriteLine("TimerState.RaiseStatus", "After raising event");

}

5. The TimerState object also contains a Timer property itself,

which references the Timer we are going to create. We start the timer via the UploadLooper.StartTimer method as follows:

public void StartTimer(uint millisecondInterval)

{

// Create the delegate that handles the timer events

System.Threading.TimerCallback timerDelegate = new
TimerCallback(CheckStatus);

// Create a timer that waits one second, then invokes every
'millisecondInterval' milliseconds

// and give TimerState a reference to the timer so we can dispose it when necessary

m_TimerState.TimerInterval = millisecondInterval;

m_TimerState.Timer = new System.Threading.Timer(timerDelegate,

m_TimerState,
(uint) 5000, millisecondInterval);

}

6. The CheckStatus method gets called when the Timer fires, and casts the object it receives to a TimerState object:

static void CheckStatus(Object state)

{

TimerState timerState = (TimerState) state;

timerState.Timer.Change(Timeout.Infinite, Timeout.Infinite); // Disable the
timer

7. CheckStatus then goes on to do its processing. Periodically,

calls
the TimerState.RaiseStatus method, which raises an event. Form1
catches
the
event and adds a ListViewItem to a ListView:

private void AddNewStatus(string message)

{

Tracer.WriteLine("Form1.AddNewStatus", "Entering sub. Message: " +

message);

ListViewItem lvi = new
ListViewItem(DateTime.Now.ToString("hh:mm:ss"));
Tracer.WriteLine("Form1.AddNewStatus", "After creating lvi");

lvi.SubItems.Add(message);

Tracer.WriteLine("Form1.AddNewStatus", "After adding subitem");

listView1.Items.Add(lvi);

Tracer.WriteLine("Form1.AddNewStatus", "After adding lvi to listView");
listView1.Refresh();

Tracer.WriteLine("Form1.AddNewStatus", "Exiting sub.");

}

8. Sporadically, the AddNewStatus method hangs on
listView1.Items.Add(lvi). I first noticed this happening when I happend to
dock the iPAQ while it was adding an item. The ActiveSync messagebox

came up and hung, as did my process. I am now able to reproduce it at will by adding a MessageBox call prior to raising an event. The messagebox
call returns but the messagebox itself remains displayed on the screen, and

my process hangs at the line mentioned above.




Nov 16 '05 #6
Dan
Daniel, another respondent to this thread, pointed me to the following
article, which points out a way to work around the compact framework
limitation that you are unable pass arguments via Invoke:
http://www.zen13120.zen.co.uk/Blog/2...d-full-fx.html

I am thinking of implementing the workaround as follows:

1. Create a Queue object in the object from which I start the timer thread
2. When I want to pass an argument from the worker thread, SyncLock the
Queue, Enqueue the argument ( a string) to the queue, then End SyncLock the
queue.
3. Invoke an EventHandler in the GUI thread
4. The EventHandler gets a reference to the Queue object from the object
encapsulating the worker thread.
5. In the EventHandler, SyncLock the retrieved Queue object, Dequeue any
enqueued objects, and End Synclock the retrieved Queue.
6. Add the retrieved string to the listview

Does anyone see any problems with this?

Thanks...

Dan

"Chris Tacke, eMVP" <ct****@spamfree-opennetcf.org> wrote in message
news:eR**************@TK2MSFTNGP11.phx.gbl...
It works out of pure luck. The issue is there's a lock condition that may
or may not be in a safe state when you access the UI, so sometimes it will
work, other times it won't.

http://www.demsey.org/blog/PermaLink...8-837c35e7d26c
--
<ctacke/>
www.OpenNETCF.org
Your CF searches start and end here
"Dan" <da*@dontspamme.com> wrote in message
news:Om**************@TK2MSFTNGP15.phx.gbl...
If that is true, then why does this code *ever* work? (It almost always
does).

Also-- can you point me to a good article on Invoke? Thanks very

much...Dan

"Chris Tacke, eMVP" <ct****@spamfree-opennetcf.org> wrote in message
news:uH**************@TK2MSFTNGP15.phx.gbl...
You cannot affect the UI from a worker thread, which the Thread.Timer is. You *must* use Invoke to marshal calls to the UI.

<ctacke/>
www.opennetcf.org
"Dan" <da*@dontspamme.com> wrote in message
news:%2****************@TK2MSFTNGP14.phx.gbl...
> I've created a pocketpc app which has a startup form containing a
listview.
> The form creates an object which in turn creates a

System.Threading.Timer.
> It keeps track of the Timer state using a TimerState object similar to
the
> example in the System.Threading.Timer documentation. The method
which > handles the timer events, among other things, periodically calls a

method
in
> this TimerState object which raises an event to the startup form, which
in
> turn adds a record to the listview. Sporadically this code in the

startup
> form hangs when it tries to add the record to the listview:
>
>
>
> listView1.Items.Add(lvi).
>
>
>
> I first noticed this happening when I happend to dock the iPAQ while

it was
> adding an item. The ActiveSync messagebox came up and hung, as did my > process. I am now able to reproduce it at will by adding a MessageBox call
> prior to raising the event. The messagebox call returns but the
messagebox
> itself remains displayed on the screen, and my process hangs at the line > mentioned above.
>
>
>
> Anyone have any thoughts on what's going on? The details of the
> implementation are described below....Dan
>
>
>
>
>
> 1. Form1 creates an UploadLooper object.
>
> 2. UploadLooper creates a TimerState object, similar to the example in
> the System.Threading.Timer documentation.
>
> 3. The UploadLooper module also contains a delegate declaration, along
> with a declaration for an UploadEvent object derived from EventArgs:
>
>
>
> public delegate void StatusEvent(Object sender, UploadEvent e);
>
>
>
> 4. The TimerState object declares the event and contains a
RaiseStatus
> method which enables us to fire StatusEvent:
>
>
>
> public class TimerState
>
> {
>
> public event StatusEvent StatusMessage; // Declaration of the event >
> ...
>
>
>
> public void RaiseStatus(string message)
>
> {
>
> Tracer.WriteLine("TimerState.RaiseStatus", "Before raising event");
>
> if (StatusMessage != null)
>
> StatusMessage(this, new UploadEvent(message));
>
> Tracer.WriteLine("TimerState.RaiseStatus", "After raising event");
>
> }
>
>
>
> 5. The TimerState object also contains a Timer property itself,

which
> references the Timer we are going to create. We start the timer via the > UploadLooper.StartTimer method as follows:
>
>
>
> public void StartTimer(uint millisecondInterval)
>
> {
>
> // Create the delegate that handles the timer events
>
> System.Threading.TimerCallback timerDelegate = new
> TimerCallback(CheckStatus);
>
> // Create a timer that waits one second, then invokes every
> 'millisecondInterval' milliseconds
>
> // and give TimerState a reference to the timer so we can dispose it

when
> necessary
>
> m_TimerState.TimerInterval = millisecondInterval;
>
> m_TimerState.Timer = new System.Threading.Timer(timerDelegate,
m_TimerState,
> (uint) 5000, millisecondInterval);
>
> }
>
>
>
> 6. The CheckStatus method gets called when the Timer fires, and

casts
> the object it receives to a TimerState object:
>
>
>
> static void CheckStatus(Object state)
>
> {
>
> TimerState timerState = (TimerState) state;
>
> timerState.Timer.Change(Timeout.Infinite, Timeout.Infinite); // Disable the
> timer
>
>
>
>
>
> 7. CheckStatus then goes on to do its processing. Periodically, calls
> the TimerState.RaiseStatus method, which raises an event. Form1 catches the
> event and adds a ListViewItem to a ListView:
>
>
>
> private void AddNewStatus(string message)
>
> {
>
> Tracer.WriteLine("Form1.AddNewStatus", "Entering sub. Message: " +
message);
>
> ListViewItem lvi = new ListViewItem(DateTime.Now.ToString("hh:mm:ss")); >
> Tracer.WriteLine("Form1.AddNewStatus", "After creating lvi");
>
> lvi.SubItems.Add(message);
>
> Tracer.WriteLine("Form1.AddNewStatus", "After adding subitem");
>
> listView1.Items.Add(lvi);
>
> Tracer.WriteLine("Form1.AddNewStatus", "After adding lvi to listView"); >
> listView1.Refresh();
>
> Tracer.WriteLine("Form1.AddNewStatus", "Exiting sub.");
>
> }
>
>
>
> 8. Sporadically, the AddNewStatus method hangs on
> listView1.Items.Add(lvi). I first noticed this happening when I happend to
> dock the iPAQ while it was adding an item. The ActiveSync
messagebox came
> up and hung, as did my process. I am now able to reproduce it at
will by
> adding a MessageBox call prior to raising an event. The messagebox call > returns but the messagebox itself remains displayed on the screen,

and my
> process hangs at the line mentioned above.
>
>
>
>
>
>



Nov 16 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Serge | last post by:
Hi, I am having a thread hang problem in my c# code. The example on the website: http://csharp.web1000.com/ is a simplified version of my problem. You will see in the form that a...
15
by: Jacob Crossley | last post by:
Hello all. We have about 10 Window's services that we wrote in c#. We use them to process row's that we have queued up in various SQL tables. The services seem to hang at least once in any given...
7
by: Pi | last post by:
I have an MDI parent with one or more children forms. Each child form has an engine thread that processes data. The child form's _Closing event is roughly 01 Private Sub...
12
by: Jack Russell | last post by:
My unstanding of all VB up to and including vb6 is that an event could not "interrupt" itself. For instance if you had a timer event containing a msgbox then you would only get one message. ...
3
by: Chris Dunaway | last post by:
Consider the following simple classes/interfaces defined below. When the derived class raises the events, on which thread is the event code run? Do I need to do anything to catch the events in my...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.