I have looked around the internet and found a nice solution which I am incorporating into my code below however it doesn't quite do exactly what I want, it works when just calling an update but I want to run a method in another class then let that method call the method that will report back to the UI and just pass some information so this mock up is just changing the button content before the operation is ran.
Using a Dispatcher I can get a UI control to update however I don't just wish to do that I want to perform some functions then have the UI Update.
So there maybe some theory I am not getting, I know the Invoke is a synchronous operation and breaking through the code it does run but the UI doesn't update.
MainWindow
Has a single button with content "CLICK ME"
Code Behind
public partial class MainWindow : Window
{
public static Button windowButton;
public MainWindow()
{
InitializeComponent();
windowButton = btnStart;
}
private void btnStart_Click(object sender, RoutedEventArgs e)
{
// Run a Process in another class on a different Thread
ProcessClass processClass = new ProcessClass();
Task processTask = new Task(() =>
{
processClass.DoSomething();
});
processTask.Start();
}
}
}
ProcessClass
class ProcessClass:MainWindow
{
public static void UpdateUI()
{
App.Current.Dispatcher.Invoke(delegate
{
windowButton.Content = "CHANGED CONTENT";
});
}
public void DoSomething()
{
UpdateUI();
int counter = 0;
for(int i = 1; i < 100; i++)
{
counter += i;
Thread.Sleep(100);
}
MessageBox.Show($"Task Completed, answer is {counter}");
}
}
Assuming that ProcessClass is your own code that you can update, change the signiture of DoDomething() to
public async Task DoSomething(IProgress<string> progress)
{
progress.Report("Begin DoSomething()");
var counter = 0;
for(var i = 1; i < 100; i++)
{
counter += i;
await Task.Delay(100).ConfigureAwait(false);
progress.Report($"DoSomething() - i = {i}");
}
progress.Report($"DoSomething() Completed, answer is {counter}");
}
Now your button click handler can be written
private async void btnStart_Click(object sender, RoutedEventArgs e)
{
// usually you would update some other control such as a TextBlock
// for the feedback, rather than the button content
var progress = new Progress<string>(s => btnStart.Content = s);
ProcessClass processClass = new ProcessClass();
await processClass.DoSomething(progress).ConfigureAwait(false);
}
Related
when I run the following code that runs fine, but not as expected, it is assumed to run on a secure thread, but all components are frozen until it finishes running the thread, it is not supposed to run on a new thread so you can use other controls?
using System;
using System.Drawing;
using System.Windows.Forms;
using System.Threading;
public class MyFormControl : Form
{
public delegate void AddListItem();
public AddListItem myDelegate;
private Button myButton;
private Thread myThread;
private ListBox myListBox;
public MyFormControl()
{
myButton = new Button();
myListBox = new ListBox();
myButton.Location = new Point(72, 160);
myButton.Size = new Size(152, 32);
myButton.TabIndex = 1;
myButton.Text = "Add items in list box";
myButton.Click += new EventHandler(Button_Click);
myListBox.Location = new Point(48, 32);
myListBox.Name = "myListBox";
myListBox.Size = new Size(200, 95);
myListBox.TabIndex = 2;
ClientSize = new Size(292, 273);
Controls.AddRange(new Control[] {myListBox,myButton});
Text = " 'Control_Invoke' example";
myDelegate = new AddListItem(AddListItemMethod);
}
static void Main()
{
MyFormControl myForm = new MyFormControl();
myForm.ShowDialog();
}
public void AddListItemMethod()
{
String myItem;
for(int i=1;i<6;i++)
{
myItem = "MyListItem" + i.ToString();
myListBox.Items.Add(myItem);
myListBox.Update();
Thread.Sleep(300);
}
}
private void Button_Click(object sender, EventArgs e)
{
myThread = new Thread(new ThreadStart(ThreadFunction));
myThread.Start();
}
private void ThreadFunction()
{
MyThreadClass myThreadClassObject = new MyThreadClass(this);
myThreadClassObject.Run();
}
}
// The following code assumes a 'ListBox' and a 'Button' control are added to a form,
// containing a delegate which encapsulates a method that adds items to the listbox.
public class MyThreadClass
{
MyFormControl myFormControl1;
public MyThreadClass(MyFormControl myForm)
{
myFormControl1 = myForm;
}
public void Run()
{
// Execute the specified delegate on the thread that owns
// 'myFormControl1' control's underlying window handle.
myFormControl1.Invoke(myFormControl1.myDelegate);
}
}
it is assumed to run on a secure thread, but all components are frozen
until it finishes running the thread
When you are invoking some delegate on control, the delegate will run on UI thread. I.e. this code will run on UI thread:
public void AddListItemMethod()
{
String myItem;
for(int i=1;i<6;i++)
{
myItem = "MyListItem" + i.ToString();
myListBox.Items.Add(myItem);
myListBox.Update();
Thread.Sleep(300); // freeze UI thread
}
}
it is not supposed to run on a new thread so you can use other
controls?
You cannot use controls from non-UI threads.
Purpose of using background threads is a long-running operations which are not related to UI. E.g. you can read some file from disk, query api, or you can run some long-running calculation (n-th fibonacci number). If you'll run these kind of things on UI thread, then your application will freeze. So you should run such operations on non-UI thread and return to UI after you have finished long-running operation (though you can notify user about progress of long-running operation).
If you want to periodically do something with UI then consider using System.Windows.Forms.Timer component. Set timer interval to 300 and add Tick event handler:
private void Button_Click(object sender, EventArgs e)
{
timer.Start();
}
private void Timer_Tick(object sender, EventArgs e)
{
myListBox.Items.Add($"MyListItem{myListBox.Items.Count + 1}");
}
The problem is that Invoke runs the delegate on the UI thread, so you're just creating a thread that just tells the UI thread to do all the work. Instead you can use async and await along with Task.Delay to simplify your code.
private async void Button_Click(object sender, EventArgs e)
{
String myItem;
for(int i=1;i<6;i++)
{
myItem = "MyListItem" + i.ToString();
myListBox.Items.Add(myItem);
myListBox.Update();
await Task.Delay(300);
}
}
That will run the code on the UI, but now the await Task.Delay(300) will not block the UI thread allowing it to handle other UI events and stopping the freezing problem.
Hi,guys, I don't understand how to handle the UI thread, so I try to create many UI elements in a child thread, but,
How to modify the following code so that the window don't block:
public void CreateCtr(string name)
{
Button btn = new Button();
btn.Content = name;
btn.Width = 10;
btn.Height = 10;
content.Children.Add(btn);
}
private void Button_Click(object sender, RoutedEventArgs e)
{
int count = 100000;
Task.Factory.StartNew(()=> {
for (int i = 0; i < count; i++)
{
this.Dispatcher.BeginInvoke(new Action(()=> {
CreateCtr(i.ToString());
}));
}
});
}
You need to give the UI thread some space to breathe. With the current code, the UI thread is busy processing all the create button tasks and cannot get to processing user input. Put a minimal pause between creating each individual button. A few milliseconds are enough:
private void Button_Click(object sender, RoutedEventArgs e)
{
int count = 100000;
Task.Factory.StartNew(() => {
for (int i = 0; i < count; i++)
{
this.Dispatcher.BeginInvoke(new Action(() => {
CreateCtr(i.ToString());
}));
Thread.Sleep(5);
}
});
}
The longer the pause, the more responsive the form becomes.
You will note that the form becomes less responsive again after some time, because after a certain amount of components on a form, adding another component takes very long. This is where my original comment becomes relevant: There is no usecase for having that many components on a form, so the framework is not designed to handle it well.
What might work is not putting all buttons on the same parent container but creating a tree of components where each component has no more than say 100 children. But again: I don't see any relevant usecase.
Might work to disable the dispatcher when adding the controls.
int count = 100000;
Task.Factory.StartNew(() => {
using (var d = Dispatcher.DisableProcessing())
{
for (int i = 0; i < count; i++)
{
this.Dispatcher.BeginInvoke(new Action(()=> {
CreateCtr(i.ToString());
}));
}
}
});
I have used delegates in the past to update the current UI (for example a textbox) with a value that is being processed in my thread. I was under the assumption that async and await took care of this for you.
When I run the code below -when the button is pressed start a new task that will count up to 100 every 100ms. The count should be displayed on the UI, but I get a targetInvocationException instead:
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
textBox1.Text = "Press button";
}
private async void button1_Click(object sender, EventArgs e)
{
await Task1();
}
private Task Task1()
{
return Task.Factory.StartNew(() => CountMethod());
}
private void CountMethod()
{
for (int i = 0; i < 100; i++)
{
Task.Delay(100).Wait();
//Console.WriteLine(i.ToString());
textBox1.Text = i.ToString();
}
}
}
The problem is that you are using a thread-pool thread to access the UI. You are doing this by using the Task.Factory.StartNew method.
Here is how you can fix it:
private async Task Task1()
{
for (int i = 0; i < 100; i++)
{
await Task.Delay(100);
textBox1.Text = i.ToString();
}
}
await will capture the current SynchronizationContext so that after the asynchronous wait is complete, the code that follows executes on the UI thread. This is true because await was called by the UI thread (in the case of my example).
In your current code, await is called on a thread-pool thread. In this case the current SynchronizationContext is null and therefore the code that follows the asynchronous wait will be executed also on a thread-pool thread.
It is know that Invoke method is used when u need to update gui from other thread. But How can I implement this without binding control to code?
Here's my test class:
class test
{
public List<Thread> threads = new List<Thread>();
public int nThreads = 0;
public int maxThreads = 5;
public void DoWork(object data)
{
string message = (string)data;
//MessageBox.Show(message);
}
public void CreateThread(object data)
{
if (nThreads >= maxThreads)
return;
Thread newThread = new Thread(DoWork);
threads.Add(newThread);
newThread.IsBackground = true;
newThread.Start(data);
nThreads++;
}
public void WindUpThreads()
{
//MessageBox.Show("count: " + nThreads.ToString());
for(int i = 0; i < threads.Count; i++)
{
if (threads[i].IsAlive == false)
{
threads[i].Abort();
threads.RemoveAt(i);
//MessageBox.Show("removing at " + i.ToString());
}
}
nThreads = threads.Count;
}
}
The question is = what tecnique I must use in order to update gui but not hardcode control into class? I've tried to pass delegate to DoWork Method, but this doesn't work (http://pastebin.com/VaSYFxPw). Thanks!
I'm using WinForms, .NET 3.5
Here's the button_click handler:
private void button1_Click(object sender, EventArgs e)
{
button1.Enabled = false;
test thTest = new test();
string[] strings;
try
{
strings = File.ReadAllLines("C:\\users\\alex\\desktop\\test.txt");
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
return;
}
bool flag = true;
int counter = 0;
int dataCount = strings.Length;
while (flag == true)
{
if (counter >= dataCount)
{
flag = false;
}
while (thTest.nThreads < thTest.maxThreads)
{
if (flag == false)
break;
thTest.CreateThread(strings[counter]);
//Data d = new Data();
//d.deleg = AddItem;
//d.mess = strings[counter];
//thTest.CreateThread((object)d);
//MessageBox.Show(counter.ToString());
counter++;
}
thTest.WindUpThreads();
if (flag == false)
{
do
{
thTest.WindUpThreads();
} while (thTest.nThreads != 0);
}
}
listBox1.Items.Add("Done");
}
The idea is that I'am launching threads for each task I want to process. After while I'am checking are there completed tasks, then they being shutdowned and new ones are launched until there no more tasks left.
Rather than making DoWork responsible for updating the UI with the results of the operation it performs, simply have it return the value:
//TODO change the type of the result as appropriate
public string DoWork(string message)
{
string output = "output";
//TODO do some work to come up with the result;
return output;
}
Then use Task.Run to create a Task that represents that work being done in a thread pool thread. You can then await that task from your button click handler.
private async void button1_Click(object sender, EventArgs e)
{
button1.Enabled = false;
test thTest = new test();
//I'd note that you really should pull out reading in this file from your UI code;
//it should be in a separate method, and it should also be reading
//the file asynchronously.
string[] strings;
try
{
strings = System.IO.File.ReadAllLines("C:\\users\\alex\\desktop\\test.txt");
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
return;
}
foreach (var line in strings)
{
var result = await thTest.DoWork(line);
listBox1.Items.Add(result);
}
listBox1.Items.Add("Done");
}
If you really want to be old school about it, you can use a BackgroundWorker instead. Simply do your work in the DoWork handler, setting the result (through the argument) when you've computed it, and update the UI with the result in the RunWorkerCompleted event handler. This lets you keep the UI and non-UI work separate, although it's far less powerful, general purpose, and extensible, as the newer features.
The question is = what tecnique I must use in order to update gui but not hardcode control into class? I've tried to pass delegate to DoWork Method, but this doesn't work
This is indeed the one of the possible techniques. It doesn't work because you have a blocking loop in the UI thread - the most of the code inside the button1_Click handler. It doesn't matter that you spawn additional worker threads - that code keeps the UI thread busy, thus Control.Invoke / Control.BeginInvoke doesn't work because they are processed by the UI thread message loop, which in this case has no chance to do that. The end result is a classical deadlock.
So, you can use the delegate approach, but to make it work, you need to move that code in a separate thread. Something like this
private void button1_Click(object sender, EventArgs e)
{
button1.Enabled = false;
var worker = new Thread(DoWork);
worker.IsBackground = true;
worker.Start();
}
private void OnWorkComplete(Exception error)
{
if (error != null)
MessageBox.Show(error.Message);
button1.Enabled = true;
}
private void DoWork()
{
Exception error = null;
try { DoWorkCore(); }
catch (Exception ex) { error = ex; }
Invoke(new Action(OnWorkComplete), error);
}
private void DoWorkCore()
{
test thTest = new test();
// NOTE: No try/catch for showing message boxes, this is running on a non UI thread
string[] strings = File.ReadAllLines("C:\\users\\alex\\desktop\\test.txt");
bool flag = true;
int counter = 0;
int dataCount = strings.Length;
// The rest of the code...
// Pass a delegate to the other threads.
// Make sure using Invoke when you need to access/update UI elements
}
I have a situation like this.
TestClass - a class defined in UI Layer
test - a class level variable
Submit button calls a function DoSomeThing()..It performs some operation in Busines Layer keeps on updating the Status Property of the class
Timercontrol getting the status (from same variable test)to display in UI
Submit button runs by a thread (say thread 1 ) and starts the operation. Is is this thread which updates the status property from the BL
Timer control creates a new thread each time to run the TimerEvent (Say thread 2 , 3 etc).
Issue here is that test.Status property , which is updated by thread1 is not accessible by other thread.. It is always null , even though the property has been updated by thread 1..
What is the solution for this ?
Thanks in advance
public class TestClass //---->#1
{
private test = new Test() ; //---->#2
protected void SubmitButon_Click(object sender, EventArgs e)
{
// This is performed by Thread1
test.DoSomeThing() //------>#3
}
protected void UpdateTimer_Tick(object sender, EventArgs e)
{
// Timer controls sends out a new thread each time
Label1.Text = test.Status; //------>#4
}
}
here's sample to use delegate and update UI ements from different thread
delegate string CallFunctionDelegate(string arg1, string arg2);
private void btnStart_Click(object sender, EventArgs e)
{
CallFunctionDelegate delegRunApps = new CallFunctionDelegate(DoSomeThingBig);
AsyncCallback CallBackAfterAsynOperation = new AsyncCallback(AfterDoingSomethingBig);
delegRunApps.BeginInvoke("", "", CallBackAfterAsynOperation, null);
}
private string DoSomeThingBig(string arg1, string arg2)
{
#region Implemetation of time consuming function
//Implemetation of time consuming function
for (int i = 0; i < 5; i++)
{
Thread.Sleep(1000);
if (btnStart.InvokeRequired)
{
btnStart.Invoke((new MethodInvoker(delegate { btnStart.Text = i.ToString(); })));
}
else
{
btnStart.Text = i.ToString();
}
}
#endregion
return arg1.Replace("freetime", arg2);
}
private void AfterDoingSomethingBig(IAsyncResult result)
{
MessageBox.Show("Finaly Done!! ;) ");
btnStart.Invoke((new MethodInvoker(delegate { btnStart.Text = "Start"; })));
}
Issue happens because a new instance is created by the timerthread eachtime after as Line #2 is executed..Hence test.Status is always null.. That was the reason for the issue