|
The main application fetches data from the database. How to display a progress bar while this fetching is in progress?
|
|
|
|
|
Well, you know that you want to use a BackgroundWorker (your thread title gives this away). This has an event to report the ProgressChanged . Basically, you need to call ReportProgress from your worker thread, and this raises the ProgressChanged event where you can update the progress bar. When you instantiate your background worker, you have to set WorkerReportsProgress to true to be able to report the progress.
"WPF has many lovers. It's a veritable porn star!" - Josh Smith As Braveheart once said, "You can take our freedom but you'll never take our Hobnobs!" - Martin Hughes.
My blog | My articles | MoXAML PowerToys | Onyx
|
|
|
|
|
problem is :it displays the progress bar after it has finished fetching !!
|
|
|
|
|
when user clicks an item in the toolbar , the main thread fetches the corresponding data. Meanwhile the background worker thread is supposed to show progress . But instead it is activated only after the fetching is done. any suggestions on how to improve this?
A snippet of my code is as given below:
< my code>
private void toolStripMain_ItemClicked(object sender, ToolStripItemClickedEventArgs e)
{
bwProgress.RunWorkerAsync();
}
private void bwProgress_DoWork(object sender, DoWorkEventArgs e)
{
// update the progress bar
for (int i = 0; i <= 100; i+=10)
{
bwProgress.ReportProgress(i);
}
}
private void bwProgress_ProgressChanged(object sender, ProgressChangedEventArgs e)
{
toolStripProgressBar1.Visible = true;
toolStripProgressBar1.Value = e.ProgressPercentage;
}
private void bwProgress_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
toolStripProgressBar1.Style = ProgressBarStyle.Blocks;
toolStripProgressBar1.Value = 100;
}
|
|
|
|
|
The problem with your code sample here is that it runs too fast for you to see the progress changed event in action. You need a lengthy operation to see the effect, so youy should really pause the execution between each iteration through the loop.
"WPF has many lovers. It's a veritable porn star!" - Josh Smith As Braveheart once said, "You can take our freedom but you'll never take our Hobnobs!" - Martin Hughes.
My blog | My articles | MoXAML PowerToys | Onyx
|
|
|
|
|
That would suggest you're using it in the wrong way. Here's a small sample demonstrating how to update a progress bar while a loop is incrementing,
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Threading;
namespace SampleBackgroundWorker
{
public partial class MainForm : Form
{
private BackgroundWorker _worker = new BackgroundWorker();
private static readonly object SyncLock = new object();
public MainForm()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
_worker.WorkerReportsProgress = true;
_worker.ProgressChanged += new
ProgressChangedEventHandler(ProgressChanged);
_worker.DoWork += new DoWorkEventHandler(DoWork);
_worker.RunWorkerAsync();
}
void DoWork(object sender, DoWorkEventArgs e)
{
for (int i = 1; i <= 10; i++)
{
lock (SyncLock)
{
Monitor.Wait(SyncLock, 1000);
}
_worker.ReportProgress(i * 10);
}
}
void ProgressChanged(object sender, ProgressChangedEventArgs e)
{
barProgress.Value = e.ProgressPercentage ;
}
}
}
"WPF has many lovers. It's a veritable porn star!" - Josh Smith As Braveheart once said, "You can take our freedom but you'll never take our Hobnobs!" - Martin Hughes.
My blog | My articles | MoXAML PowerToys | Onyx
|
|
|
|
|
Little demo in this tip[^]
DaveIf this helped, please vote & accept answer!
Binging is like googling, it just feels dirtier. (Pete O'Hanlon)
BTW, in software, hope and pray is not a viable strategy. (Luc Pattyn)
|
|
|
|
|
|
Hi,
I am getting the error while saving the excel work sheet in the windows 2003 server. Same piece of code is working fine in my local desktop. below is the error message. Given required permissions to the folder to IUSR, IWAM, Network service and ASP.NET accounts. Though i am getting the below error
Retrieving the COM class factory for component with CLSID {00024500-0000-0000-C000-000000000046} failed due to the following error: 8000401a.
Getting error in the below piece of code-
Line 1005: Microsoft.Office.Interop.Excel.ApplicationClass objApp = new Microsoft.Office.Interop.Excel.ApplicationClass();
Any help is appriciate.
Thanks In Advance.
|
|
|
|
|
Take a look at Considerations for server-side Automation of Office[^].
It seems that M$ do not advise doing this.
I found the link above by pasting your entire error message into the Google search box. Give it a go. There are loads of people out there that think they have a solution. Whether any of them actually resolve the problem, you'll have to find out for yourself.
Good luck!
Henry Minute
Do not read medical books! You could die of a misprint. - Mark Twain
Girl: (staring) "Why do you need an icy cucumber?"
“I want to report a fraud. The government is lying to us all.”
Why do programmers often confuse Halloween and Christmas?
Because 31 Oct = 25 Dec.
|
|
|
|
|
hi my friends
i have an application that i designed it myself with c#.
i want to change it to work in a system with multi phone number in central phone device.
normally with central devices you can not achieve the caller id or at least i can't. so please someone if know the solution for this problem guide me in right direction or give me a sample.
thank you
nobody help you...
you have to help you yourself
and this is success way.
|
|
|
|
|
hello,
I am using Threads in my app that runs a process of writing data on Pole(Customer)-Display Hardware Device. Now this thread calls every single second. And what is happening is that the 1st thread have'nt write its content completely and 2nd thread called and it disturbs the 1st one as well. I have'nt played much with Threads, Can i make thread to wait in a way that If a thread is called it should check 1st that if any other thread already running the same method, it should wait for that thread to end.
Thanks in Advance,
Asfand
|
|
|
|
|
Synchronization in multithreading environments can be done with semaphores[^] and mutexes[^].
Here[^] is a mutex example.
I have no smart signature yet...
|
|
|
|
|
yes you can configure the propertiese of your thread to wait for complete execute befor newest thread be running.
the method is join() (ThreadName.join()).
i hope this method help you to solve your problem.
nobody help you...
you have to help you yourself
and this is success way.
|
|
|
|
|
thnx alot, it worked
|
|
|
|
|
It seems to me that if the writing threa is busy, you swould be better advised to ignore the current write request, and just write when the thread isn't busy. Eventually, you're going to deadlock the machine because you're waiting for the thread to finish.
You may even want to have the timer adjust its interval so that there's less of a chance of the thread being in a busy state.
.45 ACP - because shooting twice is just silly ----- "Why don't you tie a kerosene-soaked rag around your ankles so the ants won't climb up and eat your candy ass..." - Dale Earnhardt, 1997 ----- "The staggering layers of obscenity in your statement make it a work of art on so many levels." - J. Jystad, 2001
|
|
|
|
|
If you are using a timer to initiate the threads then in the tick event you need to disable the timer till it is completed processing.
Never underestimate the power of human stupidity
RAH
|
|
|
|
|
This might sound daft (it probably is ) but if you cannot perform operation 2 until operation 1 has finished what on earth is the benefit of using threads?
Henry Minute
Do not read medical books! You could die of a misprint. - Mark Twain
Girl: (staring) "Why do you need an icy cucumber?"
“I want to report a fraud. The government is lying to us all.”
Why do programmers often confuse Halloween and Christmas?
Because 31 Oct = 25 Dec.
|
|
|
|
|
You're absolutely right. Simply having two threads continuously waiting one on the other isn't necessarily better than having just one thread handle everything.
However, assuming a multi-core processor is used, a ping-pong approach (two buffers, one filled by the producer, one emptied by the consumer, and switching places based on an overall clock) could double the throughput.
Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles]
I only read formatted code with indentation, so please use PRE tags for code snippets.
I'm not participating in frackin' Q&A, so if you want my opinion, ask away in a real forum (or on my profile page).
|
|
|
|
|
Luc Pattyn wrote: (two buffers, one filled by the producer, one emptied by the consumer, and switching places based on an overall clock) could double the throughput.
I thought something like that when I read the original question. There's probably a name for the technique, although I am not aware of it. Some form of Queue-like structure that the producers add their output to with a consumer that takes the head of the queue and displays it.
Henry Minute
Do not read medical books! You could die of a misprint. - Mark Twain
Girl: (staring) "Why do you need an icy cucumber?"
“I want to report a fraud. The government is lying to us all.”
Why do programmers often confuse Halloween and Christmas?
Because 31 Oct = 25 Dec.
|
|
|
|
|
|
we've always referred to it as "ping-pong buffering" and that is also mentioned near the middle of this[^].
Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles]
I only read formatted code with indentation, so please use PRE tags for code snippets.
I'm not participating in frackin' Q&A, so if you want my opinion, ask away in a real forum (or on my profile page).
|
|
|
|
|
i have an assembly (dll) deployed to a remote machine. i have the visual studio project for that assembly opened in my local desktop. due to my requirements i need to debug the assembly and run through the code locally when it is loaded in the remote machine. is it possible and how? tia.
----------------------------------------------------------
Lorem ipsum dolor sit amet.
|
|
|
|
|
Have a look here.
My signature "sucks" today
|
|
|
|
|
I have a windows service in C# .net 2.0. At the moment all it is doing is being instantiated, setting up a timer and then every interval (eg 1 minute) writing to a log file. The memory usage on TaskManager goes up and up ...
I can't see any objects that I've left lying around - I've taken out pretty much everything it is meant to do ... perhaps I'm being paranoid and the garbage collector will come and clean up when it is ready? How long should I leave it? It's been a while since the last Windows Service I wrote ... any ideas?
Thanks in advance
Here are some excerpts from the code...
The LogFile is a static class with a single static method "Write"
public static class LogFile
{
public static void Write(string message)
{
string logFileName = (String)(new AppSettingsReader()).GetValue("log-file-path", typeof(String)) + "FileLoaderServiceLog.txt";
File.AppendAllText(logFileName, DateTime.Now.ToString("dd-MMM-yyyy HH:mm:ss") + " FileLoaderService:" + " " + message + Environment.NewLine);
}
The Service class itself
public partial class FileLoaderService : ServiceBase
{
private Timer serviceTimer;
public FileLoaderService()
{
InitializeComponent();
serviceTimer = new Timer();
double intervalInMinutes = (double)(new AppSettingsReader()).GetValue("timer-interval-in-minutes", typeof(double));
serviceTimer.Interval = intervalInMinutes * 60 * 1000;
serviceTimer.Elapsed += new ElapsedEventHandler(serviceTimer_Elapsed);
}
protected override void OnStart(string[] args)
{
LogFile.Write("Service Started - starting timer");
serviceTimer.Start();
}
protected void serviceTimer_Elapsed(object sender, ElapsedEventArgs e)
{
serviceTimer.Stop();
LogFile.Write("Checking Queue...");
serviceTimer.Start();
}
protected override void OnStop()
{
serviceTimer.Stop();
serviceTimer = null;
LogFile.Write("Service Stopped");
}
}
And Program Main ...
static class Program
{
static void Main()
{
ServiceBase[] ServicesToRun;
ServicesToRun = new ServiceBase[] { new FileLoaderService() };
ServiceBase.Run(ServicesToRun);
}
}
modified on Tuesday, June 1, 2010 6:24 PM
|
|
|
|