Solved

Messagebox.showdialog() and multithread

Posted on 2006-11-06
2
2,664 Views
Last Modified: 2012-08-13
Hello. In my c# app I want to use a Form as a dialog showing progress of time consuming operation. I mean:
I have the mainform, running on the unique main thread. When user click button a long update of data to a remote webservice is launched. I want the following beahvior: user click button, data update runs on the main thread (cause it update main form objects too); while the webservice is running I want that a form dialog is shown to the user (running on a second thread). The form will contain a progressbar update with the progress of the application. When the webservice finished, the second form will close. Here is my code:

//..main Thread:
System.Threading.Thread TR1 = new System.Threading.Thread(u);
Italia(); // <-- Webservice taking long time
TR1.Start();

private void u()
        {
            int y = this.Location.Y;
            int nav = ((SisCos)MdiParent).NavTree.Width;
            int parentlocx = this.MdiParent.Location.X;
            int parentlocy = this.MdiParent.Location.Y;
            Point pnt = new Point(parentlocx + nav + 5, parentlocy + y + 10);

            SisCosExplorer.UIsVarie.MessBoxSisCos MB = new SisCosExplorer.UIsVarie.MessBoxSisCos(); // <-- Derived from Form class: it's a form without border
            MB.Height = Convert.ToInt16(System.Math.Round(this.Height * .95, 0));
            MB.Width = Convert.ToInt16(System.Math.Round(this.Width * .95, 0));
            MB.Location = pnt;

            MB.ShowDialog();
        }

It' s all working great except for one thing: The second form (MB) it's not working like a dialog (even if i call MB.Showdialog). I mean, if the user click on the main form, main form is focused. And I don't want it at all cause the webservice it's uplaoding the main form. I think it's a thread problem. What can I do to make MB working like a real Dialog?
Thanks in advance
0
Comment
Question by:puckkk
2 Comments
 
LVL 48

Accepted Solution

by:
AlexFM earned 500 total points
ID: 17881021
This happens because you show modal dialog in the worker thread. Dialog is modal only inside of thread which created it.
General approach is creating all UI elements in the main thread, and running worker thread in background. Worker thread doesn't have any UI elements. It can pass progress and any other information to UI using Invoke or BeginInvoke methods:
http://www.codeproject.com/csharp/workerthread.asp

If you want to continue with your current approach, try to disable main form for the time when MB dialog is active. This can prevent main form to be activated by mouse (I didn't test this because I always use first way to run threads).
0
 

Author Comment

by:puckkk
ID: 17888707
I used the first (and best) way reading the article. That's exactyl what i neede. Thx a lot.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Check my code out(2) 2 53
fomat Json objects 6 28
Achieve json result 2 64
Data is not showing from images 15 37
Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …
This is a video describing the growing solar energy use in Utah. This is a topic that greatly interests me and so I decided to produce a video about it.

932 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now