Click here to Skip to main content
15,887,027 members
Home / Discussions / C#
   

C#

 
GeneralRe: CLosing threads Pin
Ista30-Dec-04 4:53
Ista30-Dec-04 4:53 
GeneralRe: CLosing threads Pin
Adam Goossens30-Dec-04 13:03
Adam Goossens30-Dec-04 13:03 
GeneralThread and Not Responding Pin
brunoconde29-Dec-04 9:48
brunoconde29-Dec-04 9:48 
GeneralRe: Thread and Not Responding Pin
Tom Larsen29-Dec-04 10:49
Tom Larsen29-Dec-04 10:49 
GeneralRe: Thread and Not Responding Pin
Heath Stewart29-Dec-04 11:23
protectorHeath Stewart29-Dec-04 11:23 
GeneralRe: Thread and Not Responding Pin
Tom Larsen30-Dec-04 4:29
Tom Larsen30-Dec-04 4:29 
GeneralRe: Thread and Not Responding Pin
Heath Stewart30-Dec-04 5:17
protectorHeath Stewart30-Dec-04 5:17 
GeneralRe: Thread and Not Responding Pin
Heath Stewart29-Dec-04 11:06
protectorHeath Stewart29-Dec-04 11:06 
There's a couple ways of doing that. You can use Thread.Join in your main application thread. You would do something like this:
Thread t = new Thread(new ThreadStart(SomeMethod));
t.Start();
// ...
t.Join()
Now your main application thread (or in whichever thread you called Join will block until the thread terminates.

Another way is through asynchronous processing and gives you added functionality, like callbacks. Many objects define asynchronous methods already (the Begin* and End* method pairs), but you execute any method (including property getters and setters, which are just methods) asynchronously like so:
using System;
using System.Threading;
 
delegate void VoidCallback();
 
class Test
{
  static void Main()
  {
    Test t = new Test();
    VoidCallback callback = new VoidCallback(t.DoSomething);
    IAsyncResult result = callback.BeginInvoke(null, null);
 
    Thread.Sleep(1000);
    Console.WriteLine("Still in Main() on main thread");
 
    callback.EndInvoke(result);
    Console.WriteLine("Done");
  }
 
  void DoSomething()
  {
    Console.WriteLine("Waiting for 3 seconds in new thread...");
    Thread.Sleep(3000);
  }
}
While this is possible, however, an application should never appear hung (which is what you're doing). If you don't want the user to be able to interact with the application while still allowing the control thread (the thread on which the controls were created) to paint the controls (so the application doesn't appear hung), then simply set the wait cursor by doing Cursor.Current = Cursors.WaitCursor;. You can do this for a particular control (including the form, which extends Control) by setting Control.Cursor or for the entire application by using Cursor.Current. Be sure to restore the original cursor, however, or the user won't be able to interact (i.e., give intput) with your application.

For example:
Cursor orig = Cursor.Current;
try
{
  Cursor.Current = Cursors.WaitCursor;
  // Do stuff...
}
finall
{
  Cursor.Current = orig;
}


This posting is provided "AS IS" with no warranties, and confers no rights.

Software Design Engineer
Developer Division Sustained Engineering
Microsoft

[My Articles] [My Blog]
GeneralRe: Thread and Not Responding Pin
PaleyX1-Apr-05 2:50
PaleyX1-Apr-05 2:50 
GeneralRe: Thread and Not Responding Pin
Heath Stewart1-Apr-05 7:03
protectorHeath Stewart1-Apr-05 7:03 
GeneralRe: Thread and Not Responding Pin
PaleyX1-Apr-05 7:17
PaleyX1-Apr-05 7:17 
GeneralRe: Thread and Not Responding Pin
Heath Stewart2-Apr-05 1:25
protectorHeath Stewart2-Apr-05 1:25 
GeneralRe: Thread and Not Responding Pin
PaleyX2-Apr-05 1:53
PaleyX2-Apr-05 1:53 
GeneralRe: Thread and Not Responding Pin
Heath Stewart3-Apr-05 6:41
protectorHeath Stewart3-Apr-05 6:41 
GeneralRe: Thread and Not Responding Pin
PaleyX4-Apr-05 5:37
PaleyX4-Apr-05 5:37 
GeneralRe: Thread and Not Responding Pin
Heath Stewart4-Apr-05 8:23
protectorHeath Stewart4-Apr-05 8:23 
GeneralRe: Thread and Not Responding Pin
Ashar Khaliq29-Dec-04 11:17
Ashar Khaliq29-Dec-04 11:17 
GeneralRe: Thread and Not Responding Pin
brunoconde30-Dec-04 2:47
brunoconde30-Dec-04 2:47 
GeneralRe: Thread and Not Responding Pin
Heath Stewart30-Dec-04 5:14
protectorHeath Stewart30-Dec-04 5:14 
GeneralTAPI Programming in C# Pin
SyedMazharAli29-Dec-04 9:27
SyedMazharAli29-Dec-04 9:27 
GeneralRe: TAPI Programming in C# Pin
Michael P Butler29-Dec-04 10:10
Michael P Butler29-Dec-04 10:10 
GeneralRe: TAPI Programming in C# Pin
Ray Cassick29-Dec-04 10:14
Ray Cassick29-Dec-04 10:14 
GeneralRe: TAPI Programming in C# Pin
Ray Cassick29-Dec-04 10:11
Ray Cassick29-Dec-04 10:11 
GeneralRe: TAPI Programming in C# Pin
SyedMazharAli29-Dec-04 10:38
SyedMazharAli29-Dec-04 10:38 
GeneralMany to many relationships Pin
wlowerycolumbus29-Dec-04 7:03
professionalwlowerycolumbus29-Dec-04 7:03 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.