Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / .NET Framework / CLR / January 2006

Tip: Looking for answers? Try searching our database.

Threading in .NET

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
sonali_reddy123@yahoo.com - 28 Dec 2005 13:46 GMT
Hi all,

I have a problem regarding the threading issues in .NET. Actually I
have a application in which i execute a process by invoking the thread
in the background and at the same time I
want to display a modal dialog which will only block my UI. It doesn't
have to do anything with my thread executing in the background.

Actually the dialog is getting displayed as result of the process
carried out by a thread in background. So the thread accepts the output
from the dialog and continues with furter
processing.

The problem is the dialog which I am displaying is not getting
displayed as a modal dialog.
And I don't want the user to interact with the background UI while the
dialog is getting displayed.

Is it that Calling ShowDialog from the thread event handler causing the
problem if so what is the solution.

Thanks in advance
Stefan Simek - 29 Dec 2005 11:07 GMT
> Hi all,
>
[quoted text clipped - 18 lines]
>
> Thanks in advance

You should call Control.Invoke from the background thread to call a
method in the UI thread that will show the modal dialog. When the method
completes, the control will be returned to both the UI and the
background thread.

HTH,
Stefan
Ibrahim DURMUS - 04 Jan 2006 23:22 GMT
<<
>> Hi all,
>>
[quoted text clipped - 26 lines]
> HTH,
> Stefan
Lucvdv - 01 Jan 2006 12:28 GMT
> Hi all,
>
[quoted text clipped - 3 lines]
> want to display a modal dialog which will only block my UI. It doesn't
> have to do anything with my thread executing in the background.

Maybe I misunderstood what you're trying to do, but it seems you don't
need/want to use a separate thread at all.

When you start another process (another application), it starts in a
thread of its own automatically.

If you want your code to wait until it completes, you tell so when you
start the process.

In VB you can use the Shell function:

       Shell("notepad.exe", AppWinStyle.NormalFocus, True)

"True" as the third parameter means "wait until it finishes".

Another solution that does the same:

       Dim p As New System.Diagnostics.Process
       p.StartInfo.FileName = "notepad.exe"
       p.Start()
       p.WaitForExit()

Both forms "freeze" your application until the process exits though.  
Trying to avoid that by using a separate thread just pushes the
problem toward the horizon: you can create another thread and make
that wait until the process exits, but then you have to make your app
wait for that thread, and the easiest way to do that is to make your
app freeze ;)

Also, if you use a separate thread, always make sure that all objects
(forms, buttons, ...) are accessed only from within the thread that
created them.  The CLR provides functionality to accomplish this.

I find a small function added to a form's code the easiest.  VB sample
to change a TextBox's text:

   Private Delegate Sub dSetText(ByVal NewText As String)
   Public Sub SetText(ByVal NewText As String)
       If Me.InvokeRequired Then
           Me.Invoke(New dSetText (AddressOf SetText), _
               New Object() {NewText})
       Else
           Me.TextBox1.Text = NewText
       End If
   End Sub

You can call SetText from within any thread, it will always set the
text in the context of the right thread, so you don't have to keep
track of where it's being called from.

Note that InvokeRequired can be called on all forms-related objects,
but it isn't shown by intellisense (you could also use
Textbox1.InvokeRequired and TextBox1.Invoke here, instead of Me.*).
Yogesh - 02 Jan 2006 03:20 GMT
Hi Sonali

I think you are calling the ShowDialog() function from the secondary
thread that is why its only blocking that particular thread(Secondry
Thread) .For blocking the GUI please call the ShowDialog() from the
primary thread.

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.