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

Posted on 2007-10-15
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
    LVL 4

    Expert Comment

    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

    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

    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

    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.

    LVL 1

    Expert Comment

    Closed, 500 points refunded.
    Community Support Moderator

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Free Trending Threat Insights Every Day

    Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

    This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
    Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
    This video is in connection to the article "The case of a missing mobile phone (". It will help one to understand clearly the steps to track a lost android phone.
    Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…

    760 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

    8 Experts available now in Live!

    Get 1:1 Help Now