Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium


Threading - load data on a different thread and bind and display a messagebox after done.

Posted on 2007-10-15
Medium Priority
Last Modified: 2008-01-09
I am using C# 1.1.

I have a form with a Load button. When the button is clicked, it takes about 20 seconds to get the data and load. I want to do this over a different thread.

So I use a Thread, ThreadStart and a MethodInvoker. A new Thread is started using a ThreadStart and that thread calls back the main thread for binding the data obtained and for showing a message box saying "Data is loaded". So when the user clicks on the button and remains on the same window, the message box is shown as a Dialog window. But when the user clicks on the button and goes to a different Taskbar item, the message box is shown as a non-dialog item, with it's own entry on the taskbar and it is no more modal. The same is the case when some exception is thrown in the child thread.

How do I make the same behavior both when the current window is selected or otherwise?
Question by:srinivas_vemla
  • 3

Expert Comment

ID: 20081904
You need to display the message box using the Invoke method on the original window to have the message box appear in the thread of the original dialog.

Author Comment

ID: 20084900
smitcham... thanks for the reply... but i don't get it...

I am doing this:

I set the MethodInvoker to the BindData method as:
miDataLoaded = new MethodInvoker(BindData);

and then from my child thread, I call:

And the messageBox is within this BindData method. So I guess the thread is the same as the main thread. I checked the thread ID also using the Thread.GetHashCode() method and it turns out to be 169 always.

Can you please give me some code-snippet of what you are saying...

Author Comment

ID: 20085342
I am using this approach:


Only difference, the final DataBindToDataGrid() also has a MessageBox at the end saying "Data Loaded".

This works great when the currently selected window is the one which has requested the data load. But if we click on the button to load the data and go to some other application in the meantime, the messagebox does not appear as part of the application that requested it, but it becomes it's own application.

Accepted Solution

srinivas_vemla earned 0 total points
ID: 20086364
I got the fix. It has more to do with the parent of the Dialog MessageBox. I was using MessageBox.Show("Test") and it works when I change it to:
MessageBox.Show(this, "Test") //also assigning the parent that MessageBox.Show should use.


Expert Comment

ID: 20117508
Closed, 500 points refunded.
Community Support Moderator

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

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!
This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
Integration Management Part 2
When cloud platforms entered the scene, users and companies jumped on board to take advantage of the many benefits, like the ability to work and connect with company information from various locations. What many didn't foresee was the increased riskā€¦

580 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