473,890 Members | 1,988 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Threadlock on a Modeless Dialog

I'm currently writing a small program that churns on a repetitive task
while displaying a progress & cancel modeless dialog. I've been having
problems due to threadlocking, and I was wondering if anyone could
give me some advice.

Basically when I create the modeless dialog, I'm assuming that C#
under the covers will be implicitly creating a new thread for the
modeless dialog's message pump. Thus I protect the state determiners
with a mutex and a semaphore. But when the main application waits for
the user to push the "Close" button on the dialog the modeless dialog
stops refreshing, as if causing the main app thread to wait stopped
the message pump for the modeless dialog. So it seems like my first
assumption was wrong - .NET doesn't create a new thread to pump the
modeless dialog.

So here are the questions I have:

1) If there is a second thread for the modeless dialog, why is it
freezing?

2) Can you direct me towards a successful modeless progress dialog?

Thanks in advance. :) Here's an overview of the application, in
pseudo-code:

public AppMainClass
{
public bool m_bCancelled;

public void DoWork()
{
// Show a modeless progress dialog.
Progress_Form oProgressDialog = new Progress_Form( this );

oProgressDialog .Show();

bool bDone = false;

while ( bDone == false )
{
// Do work...

// Check if they hit the cancel button.
if ( m_bCancelled == true )
{
bDone = true;
}
}

// Tell the modeless dialog to convert to a "Finished" dialog,
// regardless of whether we've been cancelled or not.

// Wait to close.
oProgressDialog .WaitForClose() ;

// Close the dialog.
oProgressDialog .Close();
}
}
ProgressDialog
{
private Mutex m_oCloseLock;
private AutoResetEvent m_oCloseSignal;
private void FinishButtonCli cked(object sender, System.EventArg s e)
{
// Enter the mutex.
m_oCloseLock.Wa itOne();

// If we're set to close,
if ( FinishButton.Te xt == "Close" )
{
// Release the semaphore to WaitForClose();
m_oCloseSignal. Set();
}
else
{
// Set us as closing required.
m_AppMainClass. InterruptProces sing();
}

// Release the mutex.
m_oCloseLock.Re leaseMutex();
}

public void WaitForClose()
{
// We've been instructed to close.
m_oCloseLock.Wa itOne();

// Change the cancel button text.
FinishButton.Te xt = "Close";

// Release the lock.
m_oCloseLock.Re leaseMutex();

// Block on the semaphore.
// NOTE - THIS IS WHERE THE MODELESS DIALOG FREEZES.
m_oCloseSignal. WaitOne();
}
}
Nov 16 '05 #1
1 4252
You're correct that a message pump isn't set up for you automatically when
you create your modeless dialog. The approach I would recommend would be
to do all of your intensive work on a backround thread that periodically
communicates to the UI thread with progress update. Here's some
pseudo-code on how this would work.

public class MainApp
{
public void FunctionThatInv okesIntensiveWo rk()
{
Thread worker = new Thread(new ThreadStart(Wor kerFunction));
DialogThatDispl aysUpdate update = new DialogThatDispl aysUpdate();
update.Show();
worker.Start();
}

public void WorkerFunction( )
{
while (intensiveWorkN otFinished)
{
// do more work
// communicate back to the UI thread the status of the operation
}
}
}

Here's a great article that discusses this very topic:

http://msdn.microsoft.com/msdnmag/is...g/default.aspx

hth

-Joel
--------------------
From: ca***********@y ahoo.com (Carmine)
Newsgroups: microsoft.publi c.dotnet.langua ges.csharp
Subject: Threadlock on a Modeless Dialog
Date: 28 Jun 2004 13:35:26 -0700
Organization : http://groups.google.com
Lines: 104
Message-ID: <5b************ **************@ posting.google. com>
NNTP-Posting-Host: 65.78.25.41
Content-Type: text/plain; charset=ISO-8859-1
Content-Transfer-Encoding: 8bit
X-Trace: posting.google. com 1088454927 28894 127.0.0.1 (28 Jun 2004 20:35:27 GMT)X-Complaints-To: gr**********@go ogle.com
NNTP-Posting-Date: Mon, 28 Jun 2004 20:35:27 +0000 (UTC)
Path: cpmsftngxa10.ph x.gbl!TK2MSFTNG XA01.phx.gbl!cp msftngxa06.phx. gbl!TK2MSFTNGP0 8
.phx.gbl!newsfe ed00.sul.t-online.de!t-online.de!borde r2.nntp.dca.gig anews.co
m!border1.nntp. dca.giganews.co m!nntp.giganews .com!news.glorb .com!postnews2. g
oogle.com!not-for-mailXref: cpmsftngxa10.ph x.gbl microsoft.publi c.dotnet.langua ges.csharp:2547 26
X-Tomcat-NG: microsoft.publi c.dotnet.langua ges.csharp

I'm currently writing a small program that churns on a repetitive task
while displaying a progress & cancel modeless dialog. I've been having
problems due to threadlocking, and I was wondering if anyone could
give me some advice.

Basically when I create the modeless dialog, I'm assuming that C#
under the covers will be implicitly creating a new thread for the
modeless dialog's message pump. Thus I protect the state determiners
with a mutex and a semaphore. But when the main application waits for
the user to push the "Close" button on the dialog the modeless dialog
stops refreshing, as if causing the main app thread to wait stopped
the message pump for the modeless dialog. So it seems like my first
assumption was wrong - .NET doesn't create a new thread to pump the
modeless dialog.

So here are the questions I have:

1) If there is a second thread for the modeless dialog, why is it
freezing?

2) Can you direct me towards a successful modeless progress dialog?

Thanks in advance. :) Here's an overview of the application, in
pseudo-code:

public AppMainClass
{
public bool m_bCancelled;

public void DoWork()
{
// Show a modeless progress dialog.
Progress_Form oProgressDialog = new Progress_Form( this );

oProgressDialog .Show();

bool bDone = false;

while ( bDone == false )
{
// Do work...

// Check if they hit the cancel button.
if ( m_bCancelled == true )
{
bDone = true;
}
}

// Tell the modeless dialog to convert to a "Finished" dialog,
// regardless of whether we've been cancelled or not.

// Wait to close.
oProgressDialog .WaitForClose() ;

// Close the dialog.
oProgressDialog .Close();
}
}
ProgressDial og
{
private Mutex m_oCloseLock;
private AutoResetEvent m_oCloseSignal;
private void FinishButtonCli cked(object sender, System.EventArg s e)
{
// Enter the mutex.
m_oCloseLock.Wa itOne();

// If we're set to close,
if ( FinishButton.Te xt == "Close" )
{
// Release the semaphore to WaitForClose();
m_oCloseSignal. Set();
}
else
{
// Set us as closing required.
m_AppMainClass. InterruptProces sing();
}

// Release the mutex.
m_oCloseLock.Re leaseMutex();
}

public void WaitForClose()
{
// We've been instructed to close.
m_oCloseLock.Wa itOne();

// Change the cancel button text.
FinishButton.Te xt = "Close";

// Release the lock.
m_oCloseLock.Re leaseMutex();

// Block on the semaphore.
// NOTE - THIS IS WHERE THE MODELESS DIALOG FREEZES.
m_oCloseSignal. WaitOne();
}
}


This reply is provided AS IS, without warranty (express or implied).

Nov 16 '05 #2

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

Similar topics

0
3195
by: Andrew | last post by:
I get a Null Reference Exception if I close a modeless form (that is, a form displayed using Show()) when a selection is made from a ComboxBox. If the form is modal (displayed using ShowDialog()) or the selection is made from, say, a ListBox, no exception is thrown. I have included a simple example below. The error message refers to Unsafe Native Methods, but the code is 100% managed. What is going on ? I am using C#.NET 2003, Standard...
1
2940
by: Bruno van Dooren | last post by:
Hi, i was finally able to get my modeless dialog box to work, but a new problem did arise. i have implemented the dialog box in a dll that is called by a command line application that is native win32 without a message pump. if i show the dialog modal, it works fine.
1
2441
by: andrew | last post by:
Hi there, I'm having a problem with a modeless form in my app. I have a main form in my app and a socket that waits on data from a server (I use BeginReceive/EndReceive for that) and when I receive certain data from the server I want to create a new modeless form to show some of that data. But after receiving the data from the socket the modeless dialog does not appear (it seems to be receiving no windows message to update itself,...
2
3061
by: proit_123 | last post by:
I am working on a windows forms application and have the following requirement. ∑ I need to display a modeless dialog from the main form. o This allows user to continue to work with the application. o For the sake of example, the user could launch the modeless dialog from Form A and navigate to a Form B in the main window. ∑ When the modeless dialog is closed, I need to perform certain logic in the main form based on the form that is...
8
5467
by: proit_123 | last post by:
I am working on a windows forms application and have the following requirement. I have two projects in my application Project A and Project B. And Project A has the reference of Project B. I need to display a modeless dialog from the main form which is in Project A and the modeless dialog to be raised is in Project B. After closing the modeless dialog i need to pass a value from modeless dialog to the main form of Project A and also i...
0
3904
by: Ralstoj | last post by:
Hi I am programing in Autocad with VB Autodesk have not given users access to new note function in Autocad CIVIL3d API. I am trying to work round this by creating notes using the sendkey command in Vb. I can get the add note dialog box to come up ok but it does not have focus as I believe it is a modeless dialog box as it gets focus when you move the mouse cursor over it. What I am after is a VB eample on how to send key strokes to the...
0
1922
by: Sin Jeong-hun | last post by:
I've found that if a MessageBox (called by alert/confirm from Javascript) or a web page modeless dialog is popped up, I cannot call Navigate of the WebBrowser control. If I do, a COM exception occures. How can I disable javascript MessageBoxes or modeless dialogs? I disabled normal popup windows by adding e.Cancel=true at the browsers newwindow event. But I couldn't disable modeless dialog or javascript MessageBox. I tried to remove some...
1
1518
by: BillE | last post by:
Is there a trick so I can use a modeless dialog box in a master/content webform? I can display the modeless dialog, but it vanishes when a new content page is loaded. I need it to stay visible while the content pages are navigated.; I was able to do this using framesets by putting the javascript which displays the dialog in the frameset but this doesn't work with a master page, since it is reloaded when the content page is changed.
3
3865
by: btotten | last post by:
I am trying to create a modeless dialog from an event thread not generated from the original form. When created in one of the event handlers from the form, the dialog works properly. Also it works properly if it is a modal dialog. I made this little example that will just pop up a modeless dialog (form2) after a second. Here's some code.... System.Timers.Timer timer = new System.Timers.Timer(); bool timer1 = false;...
0
9978
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, weíll explore What is ONU, What Is Router, ONU & Routerís main usage, and What is the difference between ONU and Router. Letís take a closer look ! Part I. Meaning of...
0
11232
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. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10819
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10923
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
7169
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 into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5851
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 the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
6045
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4676
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
3
3277
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 effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.