Threading and WndProc - basic question

C

Chris Ashley

Hi,

I'm overriding WndProc to process some custom messages like so:

protected override void WndProc(ref Message m)
{

if (m.Msg == ImageFileMsg.MSG_IF_NEW_DATA)
{
ProcessNewDataMessage(m);
}
base.WndProc(ref m);
}

I receive one of these messages every 100ms or so for a few seconds.
This locks up my user interface completely until this is complete which
isn't ideal as each time I get a message I want to update a textbox on
my form.

I'm not sure if my solution is to use threading? I've never used
threads in .NET before, but my understanding is that if I use threading
I'll have to use delegates for my methods which update my form
controls, and these are all translated into messages which go through
WndProc. Is it possible for me to get my ProcessNewDataMessage to work
in a separate thread bearing in mind it's being called from WndProc? Or
will I encounter problems?
 
J

Jon Skeet [C# MVP]

Chris Ashley said:
I'm overriding WndProc to process some custom messages like so:

protected override void WndProc(ref Message m)
{

if (m.Msg == ImageFileMsg.MSG_IF_NEW_DATA)
{
ProcessNewDataMessage(m);
}
base.WndProc(ref m);
}

I receive one of these messages every 100ms or so for a few seconds.
This locks up my user interface completely until this is complete which
isn't ideal as each time I get a message I want to update a textbox on
my form.

I'm not sure if my solution is to use threading? I've never used
threads in .NET before, but my understanding is that if I use threading
I'll have to use delegates for my methods which update my form
controls, and these are all translated into messages which go through
WndProc. Is it possible for me to get my ProcessNewDataMessage to work
in a separate thread bearing in mind it's being called from WndProc? Or
will I encounter problems?

What does ProcessNewDataMessage need to do? If it's doing some
relatively heavy lifting, I suspect I'd extract all the relevant
information from the message, work on it in another thread (and there
are various options for doing that) and then update the UI using a
delegate. If ProcessNewDataMessage needs to do a lot of UI updating all
in one go, you might want to have a single delegate that takes all the
parameters it needs so you can do it all in one call. (That needn't
mean multiple parameters - it could be a single parameter of a new type
called something like UpdateInformation which has all the relevant info
in it.)
 
W

Willy Denoyette [MVP]

| Hi,
|
| I'm overriding WndProc to process some custom messages like so:
|
| protected override void WndProc(ref Message m)
| {
|
| if (m.Msg == ImageFileMsg.MSG_IF_NEW_DATA)
| {
| ProcessNewDataMessage(m);
| }
| base.WndProc(ref m);
| }
|
| I receive one of these messages every 100ms or so for a few seconds.
| This locks up my user interface completely until this is complete which
| isn't ideal as each time I get a message I want to update a textbox on
| my form.
|
| I'm not sure if my solution is to use threading? I've never used
| threads in .NET before, but my understanding is that if I use threading
| I'll have to use delegates for my methods which update my form
| controls, and these are all translated into messages which go through
| WndProc. Is it possible for me to get my ProcessNewDataMessage to work
| in a separate thread bearing in mind it's being called from WndProc? Or
| will I encounter problems?
|

What do you mean exactly with blocking your UI? If you really receive a
ImageFileMsg.MSG_IF_NEW_DATA message every 100 msec. and you update the
Textbox in your ProcessNewDataMessage(), which means that you update the UI
every 100 msec, this is not what I would call blocking the UI.
What's the service time of the method, and what exactly are you doing in
this method? Is the method CPU bound? Does it block etc... ?

Willy.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top