473,569 Members | 2,772 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

How can i implement a message pump with winforms ?

Hi all,

i'm doing a quite long treatment in a function, and i'm showing the
progress of the treatment with a progressbar. The code is something
like this :

for each(entry in entries) // about 100 entries
{
TreatmentForOne Or2Secs();
this->progressBar1->PerformStep( );
}
during the debug, the debugger tells me that :

Managed Debugging Assistant 'ContextSwitchD eadlock' has detected a
problem in 'd:\test\NetSen dDotNet\debug\N etSendDotNet.ex e'.
Additional Information: The CLR has been unable to transition from COM
context 0x189558 to COM context 0x189408 for 60 seconds. The thread
that owns the destination context/apartment is most likely either doing
a non pumping wait or processing a very long running operation without
pumping Windows messages. This situation generally has a negative
performance impact and may even lead to the application becoming non
responsive or memory usage accumulating continually over time. To avoid
this problem, all single threaded apartment (STA) threads should use
pumping wait primitives (such as CoWaitForMultip leHandles) and
routinely pump messages during long running operations.
Specially :
To avoid this problem, all single threaded apartment (STA) threads
should use pumping wait primitives (such as CoWaitForMultip leHandles)
and routinely pump messages during long running operations.

So, how can i pumping messages ? or, should i use a thread (and how ?)
Thanks for your help
Nicolas H.

Feb 13 '06 #1
17 23677
>Specially :
To avoid this problem, all single threaded apartment (STA) threads
should use pumping wait primitives (such as CoWaitForMultip leHandles)
and routinely pump messages during long running operations. So, how can i pumping messages ? or, should i use a thread (and how ?)


A more elegant solution perhaps is to use a timer.
on each elapsed timer event, process one work item. this will give your
application the chance to perform aditional message processing in between
your own processing.

this also means that your overall processing will take longer, since you are
not continuously processing your own stuff.
using a separate thread will give you a bit better performance,but requires
more coding.

pumping messages inside a message handler is something i've never tried in
..NET, so I don't know if it is possible or not.

kind regards,
Bruno.
br************* *********@hotma il.com
Remove only "_nos_pam"
Feb 13 '06 #2
Call routinely CurrentThread->Join(0), this implicitely calls
CoWaitForMultip leHandles and returns.

Willy.
<ni************ *@motorola.com> wrote in message
news:11******** **************@ o13g2000cwo.goo glegroups.com.. .
| Hi all,
|
| i'm doing a quite long treatment in a function, and i'm showing the
| progress of the treatment with a progressbar. The code is something
| like this :
|
| for each(entry in entries) // about 100 entries
| {
| TreatmentForOne Or2Secs();
| this->progressBar1->PerformStep( );
| }
|
|
| during the debug, the debugger tells me that :
|
| Managed Debugging Assistant 'ContextSwitchD eadlock' has detected a
| problem in 'd:\test\NetSen dDotNet\debug\N etSendDotNet.ex e'.
| Additional Information: The CLR has been unable to transition from COM
| context 0x189558 to COM context 0x189408 for 60 seconds. The thread
| that owns the destination context/apartment is most likely either doing
| a non pumping wait or processing a very long running operation without
| pumping Windows messages. This situation generally has a negative
| performance impact and may even lead to the application becoming non
| responsive or memory usage accumulating continually over time. To avoid
| this problem, all single threaded apartment (STA) threads should use
| pumping wait primitives (such as CoWaitForMultip leHandles) and
| routinely pump messages during long running operations.
|
|
| Specially :
| To avoid this problem, all single threaded apartment (STA) threads
| should use pumping wait primitives (such as CoWaitForMultip leHandles)
| and routinely pump messages during long running operations.
|
| So, how can i pumping messages ? or, should i use a thread (and how ?)
|
|
| Thanks for your help
|
|
| Nicolas H.
|
Feb 13 '06 #3
>Call routinely CurrentThread->Join(0), this implicitely calls
CoWaitForMulti pleHandles and returns. Willy.


Thanks willy, this is working and pumping message.
But on my form, i have a label and a progress bar.
During this time, the progress bar is refreshed, but not the label.
Should i call an explicit update ?

This is my code :
this->Show();
this->Update();
for each(entry in entries)
{
TreatmentForOne Or2Secs();
this->progressBar1->PerformStep( );
System::Threadi ng::Thread::Cur rentThread->Join(0);
}
this->Hide();

("this" is the form)

Do you know why the label isn't refreshed ?

Thanks in advance

Nicolas

Feb 13 '06 #4
maybe it's better to use Invoke or BeginInvoke

what do you think about it ?
Best, Nicolas

Feb 13 '06 #5
Willy Denoyette [MVP] wrote:
Call routinely CurrentThread->Join(0), this implicitely calls
CoWaitForMultip leHandles and returns.


Is this not what Application.DoE vents was made for?

Of course, performance will be better still if the work is done on a second
thread, with UI updates sent through Control.BeginIn voke.

-cd
Feb 13 '06 #6
>Is this not what Application.DoE vents was made for?
Of course, performance will be better still if the work is done on a second
thread, with UI updates sent through Control.BeginIn voke.


I try to use BeginInvoke, but i can't undestand how it works !

I try to do something like this

ArrayList ^ GetList()
{
ArrayList ^ list = gcnew ArrayList();
BuildListInAThr eadThatTakesTim e();
return list;
}

Using Delegates and thread but i can't achieve to make it working,
because that's my first thread with the dotnet framework. If you know
how to do, or maybe a link that explains that, it will be great.

Otherwise, i will use
System::Threadi ng::Thread::Cur rentThread->Join(0);
and make a
label->update();

....

Best,

Nicolas

Feb 13 '06 #7


"Carl Daniel [VC++ MVP]" <cp************ *************** **@mvps.org.nos pam>
wrote in message news:Ox******** ******@TK2MSFTN GP14.phx.gbl...
| Willy Denoyette [MVP] wrote:
| > Call routinely CurrentThread->Join(0), this implicitely calls
| > CoWaitForMultip leHandles and returns.
|
| Is this not what Application.DoE vents was made for?
|
| Of course, performance will be better still if the work is done on a
second
| thread, with UI updates sent through Control.BeginIn voke.
|
| -cd
|
|
Not really, DoEvents was invented for VB and do pump the windows queue. The
managed wait services like Join() WaitOne() etc. do pump COM messages only
(CoWaitForMulti pleHandles ), so this solves the OP's initial problem.
However, now I see he's running this long running procedure on the UI
thread, which is of course a NO NO in windows.

Willy.

Feb 13 '06 #8

<ni************ *@motorola.com> wrote in message
news:11******** **************@ g14g2000cwa.goo glegroups.com.. .
| >Call routinely CurrentThread->Join(0), this implicitely calls
| >CoWaitForMulti pleHandles and returns.
|
| >Willy.
|
| Thanks willy, this is working and pumping message.
| But on my form, i have a label and a progress bar.
| During this time, the progress bar is refreshed, but not the label.
| Should i call an explicit update ?
|
| This is my code :
| this->Show();
| this->Update();
| for each(entry in entries)
| {
| TreatmentForOne Or2Secs();
| this->progressBar1->PerformStep( );
| System::Threadi ng::Thread::Cur rentThread->Join(0);
| }
| this->Hide();
|
| ("this" is the form)
|
| Do you know why the label isn't refreshed ?
|
| Thanks in advance
|
| Nicolas
|

That means you are running your long running task on the UI thread, this is
something you should not do, run this on another thread and call Invoke or
BeginInvoke to update the progressBar, that way your UI will remain
responsive.

Willy.


Feb 13 '06 #9
>That means you are running your long running task on the UI thread, this is
something you should not do, run this on another thread and call Invoke or
BeginInvoke to update the progressBar, that way your UI will remain
responsive.


Yes, i'm running the task on the form class.

So, i'm going to use thread ... but, i just need something easy

something like this

Thread ^ t = gcnew Thread(gcnew ThreadStart(Thr eadProcess));
t->Start();
WaitForTheThrea dToTerminate();

Is this possible ? and how ?
Thanks for your help

Feb 13 '06 #10

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

Similar topics

2
6900
by: Cantankerous Old Git | last post by:
I am trying to write a program that I hope to get working as a command-line app to start with, and then eventually use a windows service wrapper to call it as a service. Its purpose is to attach to an already running (not ours) service using an API DLL, where it will do houskeeping and monitoring tasks. This is where it all gets strange....
2
1403
by: Ken Williams | last post by:
Hi I have a DLL written in C/C++ that is passed Hwnd when it is called. When the DLL has data for the calling program it uses Hwnd to post a windows message back to the calling program saying it wants attention. I have looked at Delegates and events, but it's not obvious how I can replicate this feature in C#. Anyone got a sample of...
2
1831
by: qushui_chen | last post by:
Now i was writing a programe,profile as first==>search data from SqlServer====>send mail to User==>write to log How can i do to implement the multi-thread? thank
3
2839
by: Andrew Baker | last post by:
OK this has me perplexed, puzzled and bamboozled! I have a remoting service which I displayed a message box in. I then wondered what would happen if a client made a call to the service while the message box was being displayed. Since the call comes in on a different thread the client call was not blocked. This seemed to make sense to me. ...
6
20052
by: orekin | last post by:
Hi There I have been trying to come to grips with Application.Run(), Application.Exit() and the Message Pump and I would really appreciate some feedback on the following questions .. There are quite a few words in this post but the questions are actually quite similar and should be fairly quick to answer ... (1) What is Happening with...
4
9593
by: yaron | last post by:
Hi, I am developing an infrastructure dll in c#. my dll can be use by all kind of application containers win/web/console. how do i implement a shutdown hook which will be called when the user exit from the application (for all kind of .NET applications)? for example in c# console application i do this with win32 interop...
3
2312
by: alex | last post by:
I want to implement an async-function,just like Oracle OCI function: for example: I call oci "execute(strSQL)" function, and I can call "cancle" function to cancle the process if the SQL execute too long. How to implement the execute and cancle functions? thx!
5
2090
by: DC | last post by:
Hi, I need the functionality to take a workitem from a stack, execute it in a different thread, get the next item, execute it asynchronously too, and so on. But: there should never be more than 5 threads processing workitems. As soon as the number of threads gets below five, more workitems can be queued. This goes on until the stack of...
3
1493
by: Luqman | last post by:
How to implement Role Enabled Security in Visual Basic 2005 Windows Application, like we do in ASP.Net 2.0 ? I want to use Sql Server Membership Security for Adding Roles and Users. Can I use system.web.security in Windows Application for this purpose ? Best Regards, Luqman
0
7609
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7921
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
6278
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
0
5217
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3651
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3636
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2107
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1208
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
936
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.