0

I have a problem with winform application which uses threads to update the UI. My application does upload and downloading of files from cloud. Same time I am also displaying Network speed details on the same window. These three operations (upload, download, display n/w speed) are invoked by 3 different threads. Problem is, when I start download/upload, whole window freezes and n/w speed display doesn't refresh (it is written to refresh at every 1sec interval). what would be the problem? Thanks in advance.

Code is as follows... same way i have written for download. If I call **Upload** first and then **Download** one after the other, first thread will pause and download thread starts. once **Download** is done then **Upload** continues. Its not going in parallel. Also UI doesn't respond immediately for other button click or window resize, move actions.

public delegate void UploadDelgt();
UploadDelgt UpldDlgtObj;
 private void Form1_Load(object sender, EventArgs e)
{
    UpldDlgtObj = new UploadDelgt(DoUpload);
}

public void load()
{
    Form1 form = this;
    form.Invoke(UpldDlgtObj);
}

private void button1_Click(object sender, EventArgs e)
{
    thrd = new Thread(new ThreadStart(load));
    thrd.Start();
    thrd.IsBackground = true;
}

public void DoUpload()
{
//uploads file block by block and updates the progressbar accordingly..
}
Pavan
  • 1,023
  • 2
  • 12
  • 25
  • 3
    you should provide some code. – adt Apr 04 '12 at 10:13
  • 2
    I expect your code is not doing what you think it is. Hard to say without seeing. Certainly somthing is stopping the main GUI thread from upating the labels. – Jodrell Apr 04 '12 at 10:14
  • 1
    Displaying performance data like network speed is usually done by a GUI-thread timer, rather than by notification/signalling from a thread. Network details change too fast for efficient and effective direct signalling on change and tends to clog up the GUI input queue - it's too fast for humans anyway. – Martin James Apr 04 '12 at 10:26
  • BTW, the other posters, jgauffin, Jodrell etc. are probably right as well. Post some code, especially if the words 'Join, 'Sleep', 'WaitOne', 'WaitAny', 'WaitAll' or 'DoEvents' occur anywhere in GUI event handlers. – Martin James Apr 04 '12 at 10:33
  • Thank you for the reply. @MartinJames, as you said i was using GUI thread for network speed details. Now its working fine after changing it to separate thread. – Pavan Apr 04 '12 at 12:11

2 Answers2

2

These three operations (upload, download, display n/w speed) are invoked by 3 different threads. Problem is, when I start download/upload, whole window freezes

One of your worker threads are blocking the UI thread. Make sure that none of those operations are done on the UI thread and that you use the InvokeRequired/Invoke as described here: http://www.codeproject.com/Articles/37642/Avoiding-InvokeRequired

jgauffin
  • 99,844
  • 45
  • 235
  • 372
1

Your UI freezes because you are calling form.Invoke in your load method. From MSDN about Invoke: Executes the specified delegate on the thread that owns the control's underlying window handle. So, thought you are calling DoUpload in a separate thread, it is still executed on the GUI thread (which owns the form handle), because it is called with Invoke.

Dmitrii Erokhin
  • 1,347
  • 13
  • 31
  • Thanks for reply. I agree on what you said. But if I don't use delegate invoke, it will throw CrossThreadOperation exception (since I am updating progressbar on UI). I found using delegate is the solution for that. So how can I solve this problem? – Pavan Apr 05 '12 at 12:54
  • 1
    I would suggest you call the DoUpload method in a separate thread directly (without Invoke), and inside this method use Invoke to update progress information. Something like [this](http://dotnetpad.net/ViewPaste/U3mHFc3hskmVQUvq8DGmdw#c0,c2,c4,). Only be sure not to update UI too often, or it still will be laggy. – Dmitrii Erokhin Apr 05 '12 at 13:03
  • though it solved problem of UI freezing, I am facing some other. I mean, if I want to update any label or some other components on UI inside the Upload function, then i cant do it directly. I have to use invoke delegate for each. Is it the common approach to update UI when threads are used? – Pavan Apr 10 '12 at 06:59
  • 1
    @user917634, yes, it is the common approach. Controls can be accessed only from the thread, which created them, that's why we need to use `Invoke`. I personally prefer to do it like in [this](http://stackoverflow.com/a/661662/701070) answer. – Dmitrii Erokhin Apr 10 '12 at 08:16