Click here to Skip to main content
15,885,366 members
Please Sign up or sign in to vote.
3.50/5 (2 votes)
See more:
Hi All,

I am developing a application in WPF and C#.

I am using a thread to accomplished an opration.
The operation takes few second to finish.
The problem is that when the thread is on process my window (e.g. user interface) become Inactive even in Task Manager's Application tab the application status become Not Responding until the thread is finish.
After finishing, the user interface become normal.

So i want that...
Once a thread is started,it shouldn't be blocking the UI and also wait to finish the thread.


I try all in below.

Try 1 using Sleep :

C#
Thread th1 = new Thread(operation1);
th1.Start();
Thread.Sleep(5000);


Try 2 using Join:

C#
Thread th1 = new Thread(operation1);
th1.Start();
Thread.Join();



Try 3 using AutoResetEvent :

C#
static EventWaitHandle _waitHandle = new AutoResetEvent (false);
Thread th1 = new Thread(operation1);
th1.Start();
_waitHandle.WaitOne();

private void operation1()
       {
           // do the operation

           _waitHandle.Set();
       }
Posted
Updated 16-Mar-11 23:50pm
v4
Comments
Dalek Dave 16-Mar-11 4:50am    
Edited for Readability.

If you use Thread.Join in UI thread, you screw it up exactly you if you did not create any thread and did it in UI thread in first place. This call is the blocking one. It looks like you're seriously confused about threads.

There is a number of good patterns. I have a collection of my past Answers on the topic here: How to get a keydown event to operate on a different thread in vb.net[^], see other Answers, show more of your code and ask follow-up Questions.

—SA
 
Share this answer
 
Comments
Olivier Levrey 16-Mar-11 7:24am    
I agree. Voted 5.
Sergey Alexandrovich Kryukov 16-Mar-11 12:12pm    
Thank you.
--SA
Espen Harlinn 16-Mar-11 16:54pm    
I'll join in with my 5
Sergey Alexandrovich Kryukov 16-Mar-11 17:14pm    
Thanks for joining our party :-)
--SA
Albin Abel 17-Mar-11 0:16am    
My 5
SA is right. Using Join or WaitOne or Sleep will block your application. If you want to wait for the thread to finish then it means you don't need a thread.

If you want to wait and show its progress to the user, then maybe this will be useful:
ProgressForm: a simple form linked to a BackgroundWorker[^]
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 16-Mar-11 12:13pm    
That's all correct, my 5, only we don't know what OP really wants, so further instructions should be done if OP posts a follow-up Question.
--SA
Espen Harlinn 16-Mar-11 16:57pm    
Nice link Olivier - that's one way to get a 10 :)
Albin Abel 17-Mar-11 0:17am    
My 5
Olivier Levrey 17-Mar-11 5:00am    
Thank you guys :)
What do you WANT to happen? Once a thread is started, it shouldn't be blocking the UI.

C#
public void button_Click(...)
{
    Thread thread = new Thread(new ThreadStart(ThreadProc));
    thread.Start();
}

public void ThreadStart()
{
   // do something
}


Even if you use a BackgroundWorker objec, it should run without blocking the UI.

If you need the thread to update the UI as it's running, that's simple ennough.

BTW, your first example is going to start the thread, and then sleep the UI for 5 seconds.

Further, why use a thread if you're going to use Join? You should use Join to couple threads together so that one thread doesn't finish until the joined thread is also finished.

It looks like you don't have a firm grasp of how threading works in .Net.
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 16-Mar-11 12:14pm    
Sure, my 5. That's the thing: who knows what OP wanted?
--SA
sufi2008123 17-Mar-11 5:47am    
thanks to all

I Want

Once a thread is started,it shouldn't be blocking the UI and also wait to finish the thread.
Sergey Alexandrovich Kryukov 17-Mar-11 13:04pm    
You should not wait for the finish of the thread. This is very rare (unless it is a very end of application lifetime, where you may or may not need to wait). The robust patters look like this, the thread spawn works independently but sends notifications to the UI thread via Invoke or BeginInvoke (method of Dispatcher or System.Windows.Forms.Control).
--SA
Albin Abel 17-Mar-11 0:17am    
My 5
Sergey Alexandrovich Kryukov 17-Mar-11 13:01pm    
Thank you, Albin.
--SA
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 16-Mar-11 17:19pm    
Wow! I'm familiar this this series, good to know, so 5.
--SA
Espen Harlinn 16-Mar-11 17:23pm    
Thank you, SAKryukov!
Albin Abel 17-Mar-11 0:18am    
Good Series. my 5
Espen Harlinn 17-Mar-11 9:42am    
Thank you, AlbinAbel!
When you use .join to wait until all the yarn and the next instruction is
But the string .sleep where he holds the time that you specified
So you must like the 2010 visual studio version of semaphore can use instead of sleep
And cause failure of your application processor allocation is wrong

C#
class Program
    {
        static SemaphoreSlim sem1 = new SemaphoreSlim(1);
        static SemaphoreSlim sem2 = new SemaphoreSlim(0);
        
        static void Main()
        {
            Thread t1 = new Thread(f1);
            Thread t2 = new Thread(f2);
            t1.Start();
            t2.Start();
            t1.Join();
            t2.Join();
            Console.Read();
        }

        static void f1()
        {
            for (int i = 1; i <= 100; i++)
            {
                sem1.Wait();
                Console.WriteLine("Hello");
                sem2.Release();
            }
        }

        static void f2()
        {
            for (int i = 1; i <= 100; i++)
            {
                sem2.Wait();
                Console.WriteLine("Bye");
                sem1.Release();
            }
        }
    }
 
Share this answer
 
v3
Comments
BobJanova 12-Apr-11 5:48am    
Is this not a guaranteed deadlock?
esmailian 13-Apr-11 13:36pm    
Yes, it guarantees that the source of the hello and bye to print

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900