1

Possible Duplicate:
C# WinForm Application - UI Hangs during Long-Running Operation

I created a Windows Form Application using C# and .NET Framework 4.0. I'm having a problem where while the program is doing work I can't move around the window and if I minimize it I can't get it to come back. I assume this is because I'm doing work on the same thread that the UI is running on.

Could this be the cause? If so how do I fix it?

Community
  • 1
  • 1
Kyle V.
  • 4,752
  • 9
  • 47
  • 81
  • 1
    It depends on what the work is. But you can try using the [Background Worker Class](http://msdn.microsoft.com/en-us/library/system.componentmodel.backgroundworker.aspx) – Mark Hall Jul 11 '12 at 04:13
  • @MarkHall It's mostly uploading files to a server – Kyle V. Jul 11 '12 at 04:20
  • 1
    I feel like this question is asked daily... can we start closing these out properly as duplicates? – roken Jul 11 '12 at 13:01

2 Answers2

3

If you don't want to get into the internals of threads and threading, I would recommend using the BackgroundWorker control to do your task. The Background worker control encapsulates a lot of threading stuff behind the scenes to give you a clean programming interface.

The following link might help you (written by me)

Correct way to use the BackgroundWorker

Pradeep Kumar
  • 6,836
  • 4
  • 21
  • 47
0

Try writing your code in:

            System.Threading.Tasks.Task.Factory.StartNew(new Action(() =>
            {
                //your code goes here
            }));

Because without tasks your code and UI runs on same thread it causes slow downs

Adil Mammadov
  • 8,476
  • 4
  • 31
  • 59
  • Should I be using Tasks or the BackgroundWorker class? – Kyle V. Jul 11 '12 at 04:24
  • 1
    @StickFigs See this MSDN Forum [link](http://social.msdn.microsoft.com/Forums/en-US/vbgeneral/thread/75bb20d8-a3a0-4500-b03f-c81ce6bd4f31) Essentially if you need to update the UI use Background Worker otherwise use the System.Threading Namespace – Mark Hall Jul 11 '12 at 04:29
  • I had the same problem in WPF and solved it using Tasks. I have never used BackroundWorker so I can't really advice on that. But I updated UI elements using Dispathcer.Invoke(). But I don't know if it is proper way – Adil Mammadov Jul 11 '12 at 04:34