[Last Call] Learn about multicloud storage options and how to improve your company's cloud strategy. Register Now

x
?
Solved

Showing A Dialog in the main thread from a Seperate thread

Posted on 2003-10-21
10
Medium Priority
?
296 Views
Last Modified: 2010-04-16
I am doing some checking of data in a seperate thread other than the main GUI thread.  Occasionally, I want to notify the user of something and I want the Dialog to behave like a normal Modal Dialog Box.  How can I accomplish this?   (if you call ShowDialog in a seperate thread, the dialog is non-modal).  
0
Comment
Question by:jjacksn
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
10 Comments
 
LVL 10

Expert Comment

by:ptmcomp
ID: 9592642
Create a synchronized queue: Queue myQueue = Queue.Synchronized(new Queue);
Then fill the queue with data (enqueue) in your working thread and dequeue the items in the main thread and show the messages. You can use a System.Windows.Forms.Timer to check periodically the queue. Attention: There are 3 classes named "Timer"! Only one is working in the main thread.
0
 

Accepted Solution

by:
GtG earned 400 total points
ID: 9594304
I think the best way is using invoke()
taken from Threading Support in the .NET Framework ->By Markus Egger, Software Architect, EPS Software Corporation
......
private void DisplayCustomers(DataSet dsCustomers)
{
   this.dataGrid1.DataSource =dsCustomers.Tables["customers"];
}

private void DisplayCustomers_TS(DataSet dsCustomers)
{
   DisplayCustomersDelegate oDel = new
   DisplayCustomersDelegate(  this.DisplayCustomers);
   DataSet[] args = {dsCustomers};
   this.Invoke(oDel,args);
}
delegate void DisplayCustomersDelegate(DataSet dsCustomers);

The first line instantiates a delegate that points to our original DisplayCustomers()
method. (Note that the last line in this snippet defines the delegate.) When then proceed
to create an array of DataSet objects with a single item (our customer DataSet). Finally,
we call the Invoke() method with our delegate and the DataSet in the array of
arguments. If you are not familiar with Delegates, this may look a bit confusing at first,
but it is really quite straightforward. We simply point the Invoke() method to another
method and say ?See this method there? Go ahead and run it for me??.
And that does the trick! The only thing that?s left to do is make the LoadCustomers() call
DisplayCustomers_TS() instead of DisplayCustomers(). Now our little application is
perfectly thread-safe!
..................

I think you can use this mechanism for your code ...
0
 
LVL 10

Expert Comment

by:ptmcomp
ID: 9597016
@GtG:
How are you telling the delegate in which thread it should be executed??

What could help are ContextBound objects but I've never used them. Else Microsoft is also suggesting methods like the one I described above.

BTW: I'm also Software Architect in a world wide Software Company, but here we should not try to impress with names and titles but with facts.
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 48

Expert Comment

by:AlexFM
ID: 9597026
0
 
LVL 10

Expert Comment

by:ptmcomp
ID: 9597329
Ok, Control.Invoke works, you're right. Control.Invoke does the synchronizing stuff. Delgates alone don't do any synchronizing. I didn't realize that in "this.Invoke(oDel,args);" "this" is of type Control.
0
 
LVL 5

Author Comment

by:jjacksn
ID: 9599479
Does Invoke only work with delagates?  
0
 
LVL 10

Expert Comment

by:ptmcomp
ID: 9599838
Yes, as you see in the help or msdn Control.Invoke(Delegate) or Control.Invoke(Delegate, object[]).
0
 
LVL 10

Expert Comment

by:ptmcomp
ID: 9599888
If you use Invoke, the current thread is suspended until the main thread completed the delegate. You can use BeginInvoke / EndInvoke to call the method asynchronous then the current thread continues working.
0
 

Expert Comment

by:GtG
ID: 9610476
..............
taken from Threading Support in the .NET Framework ->By Markus Egger, Software Architect, EPS Software Corporation
..............
I was only talking about a whitepaper, which is the reference for the solution copied...
0
 
LVL 10

Expert Comment

by:ptmcomp
ID: 9615521
Yes, you're right it's good to list the reference. Since I can not edit my old comment it will forever stay...
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Article by: Ivo
C# And Nullable Types Since 2.0 C# has Nullable(T) Generic Structure. The idea behind is to allow value type objects to have null values just like reference types have. This concerns scenarios where not all data sources have values (like a databa…
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.
Video by: ITPro.TV
In this episode Don builds upon the troubleshooting techniques by demonstrating how to properly monitor a vSphere deployment to detect problems before they occur. He begins the show using tools found within the vSphere suite as ends the show demonst…
In a question here at Experts Exchange (https://www.experts-exchange.com/questions/29062564/Adobe-acrobat-reader-DC.html), a member asked how to create a signature in Adobe Acrobat Reader DC (the free Reader product, not the paid, full Acrobat produ…
Suggested Courses

650 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