473,320 Members | 1,951 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,320 software developers and data experts.

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 FinishButtonClicked(object sender, System.EventArgs e)
{
// Enter the mutex.
m_oCloseLock.WaitOne();

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

// Release the mutex.
m_oCloseLock.ReleaseMutex();
}

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

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

// Release the lock.
m_oCloseLock.ReleaseMutex();

// Block on the semaphore.
// NOTE - THIS IS WHERE THE MODELESS DIALOG FREEZES.
m_oCloseSignal.WaitOne();
}
}
Nov 16 '05 #1
1 4218
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 FunctionThatInvokesIntensiveWork()
{
Thread worker = new Thread(new ThreadStart(WorkerFunction));
DialogThatDisplaysUpdate update = new DialogThatDisplaysUpdate();
update.Show();
worker.Start();
}

public void WorkerFunction()
{
while (intensiveWorkNotFinished)
{
// 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***********@yahoo.com (Carmine)
Newsgroups: microsoft.public.dotnet.languages.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**********@google.com
NNTP-Posting-Date: Mon, 28 Jun 2004 20:35:27 +0000 (UTC)
Path: cpmsftngxa10.phx.gbl!TK2MSFTNGXA01.phx.gbl!cpmsftn gxa06.phx.gbl!TK2MSFTNGP08
.phx.gbl!newsfeed00.sul.t-online.de!t-online.de!border2.nntp.dca.giganews.co
m!border1.nntp.dca.giganews.com!nntp.giganews.com! news.glorb.com!postnews2.g
oogle.com!not-for-mailXref: cpmsftngxa10.phx.gbl microsoft.public.dotnet.languages.csharp:254726
X-Tomcat-NG: microsoft.public.dotnet.languages.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();
}
}
ProgressDialog
{
private Mutex m_oCloseLock;
private AutoResetEvent m_oCloseSignal;
private void FinishButtonClicked(object sender, System.EventArgs e)
{
// Enter the mutex.
m_oCloseLock.WaitOne();

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

// Release the mutex.
m_oCloseLock.ReleaseMutex();
}

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

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

// Release the lock.
m_oCloseLock.ReleaseMutex();

// 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
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())...
1
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...
1
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...
2
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...
8
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...
0
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...
0
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...
1
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...
3
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...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

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.