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

Problem showing message boxes by threads

Hi
I need to set my main form as the owner of an error dialog box shown when
there's an exception in a background thread. This is to make sure that the
error form is closed down if the user pulls their security card while the
error window is being shown.
In visual studio 2003, my main form set a static "owner" variable within my
frmWarning class definition and if the error form was needed (and null owner
passed) then this static variable was used. It's not pretty but it was the
only way I could think of for making sure that error dialogs could be closed
by the main form.
This worked fine when there was an error thrown somewhere in my thread loop
in vis studio 2003.
I've just transferred it all across to 2005 and now my error form won't
appear. If I remove the line that looks for the static owner member variable
then it shows ok. But that's no good because then pulling the card doesnt
shut the form down.
I guess I'm doing something naughty, but is there a way of changing this
following function so that I can use the frmWarning.owner properly plus if
the main form (created by main process) then tries to close the form (that
has been created by the thread) later then that will work fine too.
thank you (hope that was all understandable)
Claire

public static DialogResult ShowError(System.Windows.Forms.IWin32Window
Owner, string ErrorMessage)
{
string fnName = "ShowError";
DialogResult result = DialogResult.OK;
frmWarning frm = new frmWarning();
try
{
frm.lblText.Text = ErrorMessage;
// if (Owner == null) Owner = frmWarning.owner;
result = frm.ShowDialog(Owner);
}
catch (Exception e)
{
Log.Exception(typeof(frmWarning), fnName, e);
}
finally
{
frm.Dispose();
}
return result;
}

/// <summary>
/// Destroy child forms which are owned by the
/// root form. Closing the root alone won't destroy the
/// child forms automatically. All children have to be
/// created by passing their owning form in ShowDialog(Form owner)
/// parameter
/// An iterative function called from main form.
/// </summary>
/// <param name="Root">The base form</param>
static public void DestroyOwned(Form Root)
{
for ( int d=Root.OwnedForms.GetLength(0) - 1; d >= 0; d--)
{
DestroyOwned(Root.OwnedForms[d]);
Root.OwnedForms[d].Close();
}
}// function
Sep 3 '07 #1
1 1627
In article <uV*************@TK2MSFTNGP06.phx.gbl>, cc@nospam.com says...
Hi
I need to set my main form as the owner of an error dialog box shown when
there's an exception in a background thread.
Windows Forms are not threadsafe. VS2003 let you get away with some
threading stuff that VS2005 won't.

Windows forms objects must be accessed only by the thread that created
them. You can use a delegate along with Control.Invoke to marshal a
call back to the UI thread. Here's a quick example:

// define a delegate the background thread will use
public delegate void UpdateStatusDelegate(string message);

// in our form, define a method that will update status text
// NOTE: this method matches the signature of our delegate
public void UpdateStatus(string statusMessage)
{
txtStatus.Text = statusMessage;
}

With the above in place, you can now safely update the "txtStatus"
control from a different thread by wrapping the call in a delegate. In
your background thread, do this:

UpdateStatusDelegate upd = new UpdateStatusDelegate(UpdateStatus);
this.Invoke(upd);

This will marshal the call to UpdateStatus back to the UI thread where
it will execute sucessfully.

This is a simple example that you'll need to extend to your app. Any
manipulation of a form (closing, updating controls, moving, etc...) must
be done in the main UI thread (the thread that created the control).

--
Patrick Steele (pa*****@mvps.org)
http://weblogs.asp.net/psteele
Sep 3 '07 #2

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

Similar topics

9
by: Dustin | last post by:
Here's what I am trying to do: (Names represent CSS classes.) I want to create a photo gallery. I want the entire gallery to be surrounded by a box named PhotoGallery. I want a fluid placement...
4
by: Dave Boyd | last post by:
Hi, I have two very similar forms each with a subform. The main form gets a few fields from the user and passes this back to a query that the subform is bound to. The requery is done when the...
4
by: John Tyce | last post by:
The problem that I am now having, involves refreshing controls. I have allot of text boxes, several combo boxes and grids. These controls are showing live data from an Oracle database. I have dates...
2
by: Ludovic SOEUR | last post by:
Have everyone tried to create controls in separated threads ? I have a problem that I do not understand. To simplify the explanations, I wrote theses few lines to show an example of the problem....
1
by: geoff | last post by:
I have a web form with different text boxes on it. 3 of the 5 text boxes are web controls and the other 2 are just regular html input text boxes. All of these are showing up as little 10 pixels...
6
by: Alice | last post by:
Hi -- I am trying to code a "mad libs" style program, in which the user puts in nouns, adjectives, etc., into text boxes. Then the user clicks on a button which opens a message box. Inside the...
4
by: billsahiker | last post by:
Why is my application crashing on a windows 98 machine? It runs fine on the development machine with xp pro. I installed framework 2, with dotnetfx.exe, and windows installer 2 on the windows 98...
2
by: silpa | last post by:
Hi, I have placed two text boxes(server side) for Name and Age in asp.net 2.0. Their ids are txtPatientName and txtPatientAge. The validation to be done is both text boxes should not be...
2
by: =?Utf-8?B?aGVsZmk=?= | last post by:
Hi all, I have replied with my own questions to an older post entry but after a while I thought it's better to start a new thread based on the previous one. Perhaps with the new thread I will...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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,...
0
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,...
0
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...
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.