I am making a program that reads 1 file and writes it to another file, and the main thing is to make a ProgressBar
there that will show progress, but when it starts, it swears that ProgressBar
and the charCount
variable that reads how many characters in a text file are in another stream. I used Dispatcher.Invoke(() =>
and the program worked but now it hangs until this thread finishes its job.
Thread thread = new Thread(
() =>
{
Dispatcher.Invoke(() =>
{
while (progBar.Value < charCount)
{
progBar.Value++;
Thread.Sleep(100);
}
});
});
thread.Start();
(progBar
: ProgressBar
name, charCount
: stores the number of characters in a text file).
here is a piece of that code. It's strange that the same code without Dispatcher
worked in Windows Forms and changed the value of the ProgressBar
without hanging. Here it either freezes if you put Sleep()
, or instantly changes the value of ProgressBar
without Sleep()
.
I need that while the program is running, the second thread that I create has the ProgressBar
value so that the bar fills up without braking the main program.
UPD: I have to use the Thread method.
Here is what the code should look like
Thread thread = new Thread(
() =>
{
while (progBar.Value < charCount)
{
progBar.Value++;
Thread.Sleep(100);
}
});
thread.Start();