Click here to Skip to main content
15,947,613 members
Please Sign up or sign in to vote.
3.00/5 (1 vote)
See more:
I have a function in c# which has name "fetchData()".This is to fetch some data from internet
using api.But sometimes it takes long time depending on internet connection speed.So
I want to show a marquee progress bar when my application is fetching data.For a loop I can use
application.DoEvent() to keep winform active.But this is not loop.Just a code with webresponse takes long time.So how to do this?I have read so many articles of backgroundworker but
concept is not clear.They all want to sleep a thread.But I want to make two threads working simultaneously.Please help.


string webUrl = "http://api.worldweatheronline.com/free/v1/weather.ashx?q=" + city + "&format=xml&num_of_days=2&key=my key";
string[,] retData = fetchProcess(webUrl);

this code is written on a button click event."fetchProcess()" is string array function.In this function webresponse code is written which takes long time to execute.Now where and how to apply this keeping my form active.
Posted
Updated 29-May-13 20:10pm
v2
Comments
Sergey Alexandrovich Kryukov 29-May-13 23:40pm    
There is nothing working "simultaneously", but things can work in parallel, asynchronously...
—SA

There are lots of ways to do this if you google it, but a popular way is to use Threads

Make sure you have this namespace included.
C#
using System.Threading;

Then in the function where you normally call fetchData() insert this snippet.
C#
Thread background = new Thread(() => fetchData());
background.Start();

//Do something with progress bar

Then make sure at the end of your fetchData() you make a Thread-Safe Call to end the Progressbar marquee or whatever you'd like.
 
Share this answer
 
v5
Comments
Sergey Alexandrovich Kryukov 29-May-13 23:44pm    
It's pretty good, with one little problem: there is no such thing as "thread-safe call" to UI methods/properties. Actually, all calls are done in UI thread itself. Some call-like functionality in a non-UI thread is achieved via delegation a call (actually, a delegate to be inoveled) to the UI thread, which is done by Control.Invoke, Control.BeginInvoke, Dispatcher.Invoke or Dispatcher.BeginInvoke (yes, WPF dispatcher can be used in Forms, too). So, I voted 4. I think I'll need to add another solution to complement yours.
—SA
Sergey Alexandrovich Kryukov 29-May-13 23:45pm    
I've added this solution, please see Solution 2.
—SA
souvikcode 30-May-13 1:56am    
not helpful.still my form remain inactive until the webresponse code get response from internet.
souvikcode 8-Jun-13 13:19pm    
if you have time please write the exact code for my problem.I have no idea about invoke.
In addition to Solution 1:

Please see my comment to this solution. You cannot call anything related to UI from non-UI thread. Instead, you need to use the method Invoke or BeginInvoke of System.Windows.Threading.Dispatcher (for both Forms or WPF) or System.Windows.Forms.Control (Forms only).

You will find detailed explanation of how it works and code samples in my past answers:
Control.Invoke() vs. Control.BeginInvoke()[^],
Problem with Treeview Scanner And MD5[^].

See also more references on threading:
How to get a keydown event to operate on a different thread in vb.net[^],
Control events not firing after enable disable + multithreading[^].

—SA
 
Share this answer
 
Comments
austinbox 30-May-13 0:27am    
Ah, thanks for the clarification, I thought you could by:

delegate ExampleInvoke(parameters);

void UIInvoke(parameters)
{
if(control.InvokeRequired)
{
control.Invoke(new ExampleInvoke(UIInvoke(parameters));
return;
}
dostuff()
}

Thanks adding for the second solution and exaplnation.
Sergey Alexandrovich Kryukov 30-May-13 0:53am    
InvokeRequired is usually not needed. It is always required when called from any thread other then the UI thread where the control is inserted.
—SA

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