Firing events in loop are not updating UI in sequence - c#

I was trying to update status on UI for a Long Running Operating. I've created a demo form based application, task it have multiple rows, each row is having days and default values in each column of datagrid is 0, once computation file computes one iteration for one day it will update UI and set 1 for that day.
I am using threading, delegates and events to implement this and it is working as expected if I put Thread.Sleep(100) between two event calls. If I put "Thread.Sleep(100)" inside last nested for loop then it updates UI as expected but as soon as I remove it and run loop without sleep, then it skips some of the columns on UI and directly update last few/random columns, as you can see in attached image link(Image of output of my code without thread sleep) only last column is getting updated.
If I am not mistaken all the events are getting fired in sequence then they should update UI in sequence too but it's not happening and I don't know why. I don't want to do this Sleep thing because I have around 14 calls in actual application for UI status update and it will run under a loop so if It put sleep(100) then it will cost me a lot, is there any way to do it without SLEEP?
Image of output of my code without thread sleep
public class Class1 : IGenerate
{
public event MessageEventHandler OnMessageSending;
public void LongOperationMethod(BindingList<Status> _statusData)
{
if (OnMessageSending != null)
{
MessageEventArgs me = new MessageEventArgs();
/// Loop for 2-3 Weeks
for (; ; ){
/// Loop for 7 day
for (; ; )
{
/// Calculation on everyday
for (int j = 0; j != 1000; ++j)
{
// to do
}
me.weekNo = k;
me.DayNo = i;
OnMessageSending(me);
}
}
me.Message = "Process completed successfully...";
OnMessageSending(me);
}
else
{
throw new ArgumentException("Event hasn`t been rised, so we cannot continue working.");
}
}
}
**UI file:**
<pre><code>
public partial class Form1 : Form
{
BindingList<Status> _statusData = new BindingList<Status>();
delegate void StringParameterDelegate(string value);
Class1 cls = new Class1();
public Form1()
{
InitializeComponent();
labelProgress.Text = "";
}
private void button1_Click_1(object sender, EventArgs e)
{
for (int i = 1; i <= 2; ++i)
{
_statusData.Add(new Status { Week = "Week" + i, Day1 = 0, Day2 = 0, Day3 = 0, Day4 = 0, Day5 = 0, Day6 = 0, Day7 = 0 });
}
dataGridView1.DataSource = _statusData;
}
private void button2_Click(object sender, EventArgs e)
{
Thread t1 = new Thread(() => StartingThread(_statusData));
t1.Start();
}
void StartingThread(BindingList<Status> _statusData)
{
IGenerate generate = new Class1();
generate.OnMessageSending += new MessageEventHandler(generate_OnMessageSending);
generate.LongOperationMethod(_statusData);
}
private void generate_OnMessageSending(MessageEventArgs e)
{
int weekNo = e.weekNo;
int dayNo = e.DayNo;
this.dataGridView1.BeginInvoke(new MethodInvoker(() => dataGridView1.Rows[e.weekNo].Cells[e.DayNo + 1].Value = 1));
this.labelProgress.BeginInvoke(new MethodInvoker(() => this.labelProgress.Text = e.Message));
}
}
</code></pre>

It looks like you are sending the same instance of MessageEventArgs every time, and just updating that one instance on the background thread. This means that your event handler on the UI thread will retrieve the exact same instance of MessageEventArgs that is being updated in the loop! By the time your UI handler gets the MessageEventArgs, its .weekNo and .DayNo properties could well have been modified by the next iteration of the loop, since they are running on separate threads.
To fix this, create a new instance of MessageEventArgs every time you call OnMessageSending().
Relevant snippet:
MessageEventArgs me = new MessageEventArgs();
me.weekNo = k;
me.DayNo = i;
OnMessageSending(me);

Related

BackgroundWorker - reporting progress with "sub tasks"

A WinForms application with a custom control, LabelProgressBar, which has the ability to display both progress and some descriptive text and/or percentage completion. This is done by calling LabelProgressBar.statusInProgress(string message, int percentageCompletion).
One usage of this is as follows:
private void import_begin(System.Object sender, System.ComponentModel.DoWorkEventArgs args)
{
// first unpack the arguments
System.Object[] arguments = (System.Object[])args.Argument;
System.String filename = (System.String)arguments[0];
System.String why = (System.String)arguments[1];
// tasks:
// 1. read excel file and apply changes to model
// 2. gather changes and format them as XML
// 3. send request to server
// 4. commit/rollback changes
// grab the worker thread so we can report percentage progress
System.ComponentModel.BackgroundWorker worker = (System.ComponentModel.BackgroundWorker)sender;
// now do the work
#region Task1
Controller.Excel excel = new Controller.Excel(filename);
try
{
// the progress of this needs to be tracked
overall_result = excel.import_all(out modified_nodes);
}
catch (InvalidDataExcetpion invDataEx)
{
// deal with it
}
#endregion
worker.ReportProgress(25);
// complete remaining tasks...
}
The event handler for the worker reporting its progress is the following:
private void import_progress(object sender, System.ComponentModel.ProgressChangedEventArgs e)
{
Debug.WriteLine("Import percentage completion: " + e.ProgressPercentage);
labelProgressBar1.statusInProgress("Import", e.ProgressPercentage);
}
In short, the import_begin method is broken up into several "tasks". These are broken up into "subtasks". Taking the example of the import_all method:
public Command_Result import_all(out System.Collections.Generic.List<Model.Data_Node> nodes)
{
Command_Result overall_result = Command_Result.OK;
Command_Result this_result;
nodes = new System.Collections.Generic.List<Model.Data_Node>(excel.Workbook.Worksheets.Count);
Model.Data_Node destination;
// the intent is to report the progress of this particular subtask on the basis of how many worksheets have been processed in this for loop
foreach (OfficeOpenXml.ExcelWorksheet worksheet in excel.Workbook.Worksheets)
{
this_result = import_sheet(worksheet.Name, out destination);
nodes.Add(destination);
if (this_result > overall_result)
{
overall_result = this_result;
}
}
return overall_result;
}
The intent is to have this "subtask" report progress on the basis of how many sheets have been processed in the loop. Calculating a percentage for this is a trivial task, but it is not clear to me how this can be reported back to the import_begin method. When this "subtask" is completed, the overall task completion (from the POV of the import_begin method) should be 25%. Similarly for the other tasks. How can this be achieved?
import_begin don't really need to get the update, it can just call the subtasks, while also passing the BackgroundWorker, so the subtasks are responsible to directly report their progress. If "polluting" the subtasks with BackgroundWorker is unacceptable, then create a delegate to call the BackgroundWorker, so your subtasks will then call the delegate instead.
private void mainTask(object sender, DoWorkEventArgs e)
{
var worker = (BackgroundWorker)sender;
var report = new Action<int>(i => worker.ReportProgress(i)); //the delegate
smallTask1Clean(report); //this one pass the delegate
smallTask2(worker); //this one directly call background worker
worker.ReportProgress(100);
}
void smallTask1Clean(Action<int> a)
{
for (int i = 0; i < 20; i++)
{
Thread.Sleep(500);
a(i);
}
}
void smallTask2(BackgroundWorker w)
{
for (int i = 0; i < 5; i++)
{
Thread.Sleep(1000);
w.ReportProgress(i*80/5+20);
}
}
You can also insulate the subtasks from having to know their part in the larger tasks, in this case, the delegate should take two variables, the current internal progress of the subtasks and the total item it needs to process.
private void mainTask(object sender, DoWorkEventArgs e)
{
var worker = (BackgroundWorker)sender;
var preTaskProgress = 0;
var currentTaskTotalPercentage = 0;
var smarterDelegate = new Action<int, int>((current, total) =>
{
worker.ReportProgress(preTaskProgress + (current *currentTaskTotalPercentage/total));
});
currentTaskTotalPercentage = 30; //the following task will in total progressed the main task for 30%
smallTaskClean(smarterDelegate);
preTaskProgress = currentTaskTotalPercentage; //upate the main the progress before starting the next task
currentTaskTotalPercentage = 70; //the following task will in total progressed the main task for 70%
smallTaskClean(smarterDelegate);
worker.ReportProgress(100);
}
void smallTaskClean(Action<int,int> a)
{
for (int i = 0; i < 5; i++)
{
Thread.Sleep(1500);
a(i,5);
}
}

wpf Multiple dispatch timer for timer application : Run multiple timer simulteniously

Read multiple stackoverflow, codeproject solution, could not integrate to my problem.
Have a datagrid in a usercontrol which is loaded in a window. Each DataRow in the DataGrid represents a timer setting.
Like:
timer name : Test 1 , Timer : 1h 3m
timer name : Test 2 , Timer : 2h 2m
timer name : Test 3 , Timer : 3h 1m
Selecting a row, clicking on the button Start, Starts the timer of that row. And with dispatcher tick event, it updates the grid I have done till this. Now I have to start another(or two or ...) timer which will do the same at the same time. I am stuck on this. Let me share what I have tried!
btnStartClickEvent in mainwindow.xaml.cs
if (btnStart.Content.ToString() == "Start")
{
if (_AUC == ActiveUserControl.Grid)
{
runningRow = (TaskGridData)_TG.dgEmployee.SelectedItem;
if (runningRow != null)
{
currentlyRunningID.Add(runningRow.ID);
btnStart.Content = "Stop";
//worker.RunWorkerAsync(runningRow);
StartTimer(runningRow);
}
}
}
else if (btnStart.Content.ToString() == "Stop")
{
btnStart.Content = "Start";
StopTimer();
}
private DateTime TimerStart { get; set; }
private void StartTimer(TaskGridData tgd)
{
dispatcherTimer = new DispatcherTimer();
dispatcherTimer.Interval = new TimeSpan(0, 0, 0, 1, 0);
dispatcherTimer.Tick += new EventHandler(dispatcherTimer_Tick);
TimerStart = DateTime.Now;
dispatcherTimer.Start();
//worker.RunWorkerAsync();
//string etime = DateTime.Now.Second.ToString();
}
private void StopTimer()
{
dispatcherTimer.Stop();
}
private void dispatcherTimer_Tick(object sender, EventArgs e)
{
var currentValue = DateTime.Now - TimerStart;
runningRow.Duration = DurationValueToString(currentValue);
temp = (List<TaskGridData>)_TG.dgEmployee.ItemsSource;
foreach (TaskGridData item in temp)
{
if (item.ID == runningRow.ID)
{
item.Duration = DurationValueToString(DurationStringToVlaue(item.Duration) - DurationStringToVlaue(runningRow.Duration));
break;
}
}
//_TG.dgEmployee.ItemsSource = null;
//_TG.dgEmployee.ItemsSource = temp;
Thread NewThreadforStartProcessAfterTraining = new Thread(() => UpdateGrid());
NewThreadforStartProcessAfterTraining.IsBackground = true;
NewThreadforStartProcessAfterTraining.SetApartmentState(ApartmentState.STA);
NewThreadforStartProcessAfterTraining.Start();
}
private void UpdateGrid()
{
this.Dispatcher.Invoke(DispatcherPriority.Normal, new Action(() =>
{
_TG.dgEmployee.ItemsSource = null;
_TG.dgEmployee.ItemsSource = temp;
}));
}
I know this code is for single timer. If I click a 2nd row and try to start timer, then it gets error in tick event, running row is found null.
I am wondering how can I keep this code and make it work for multiple timer. May be multithreading. A guide to do that, will be very helpful.
Thread NewThreadforStartProcessAfterTraining = new Thread(() => UpdateGrid());
NewThreadforStartProcessAfterTraining.IsBackground = true;
NewThreadforStartProcessAfterTraining.SetApartmentState(ApartmentState.STA);
NewThreadforStartProcessAfterTraining.Start();
All the above part where you start a new STA thread is unneeded and wrong in this context, since you can't update the visual tree in this way.
You can find a correct example of using a STA thread in one of my previous answers: https://stackoverflow.com/a/42473167/6996876
Try to understand the concept of thread affinity in WPF.
You simply need an UpdateGrid() where you have to delegate UI work to the dispatcher.
Furthermore, passing an argument to the Tick event is already explained here: https://stackoverflow.com/a/16380663/6996876
In your case you may want to change the current unique runningRow so that it's passed to the event instead.

Thread inside thread trying to invoke and update ui

I have on winform with a textbox and on textchanged executes a background thread:
private void txtFathersLast_TextChanged(object sender, EventArgs e)
{
ThreadPool.QueueUserWorkItem(_ => WaitWhileUserTyping());
}
private void WaitWhileUserTyping()
{
var keepWaiting = true;
while (keepWaiting)
{
_keyPressed = false;
Thread.Sleep(TypingDelay);
keepWaiting = _keyPressed;
}
Invoke((MethodInvoker)(ExecuteSearch));
_waiting = false;
}
private void ExecuteSearch()
{
Thread.Sleep(200);
Task.Factory.StartNew(() =>
{
using (DataReference.SearchWCF search = new DataReference.SearchWCF())
{
_similaritySearchResults = search.SearchPersonBySimilarity(txtFathersLast.Text, txtMothersLast.Text, txtName.Text, DateTime.Now, 10);
}
}).ContinueWith(t=>{
if (this.InvokeRequired)
{
this.BeginInvoke(new Action(() =>
{
if (_similaritySearchResults != null && _similaritySearchResults.Tables["data"].Rows.Count > 0)
{
DataTable dt = _similaritySearchResults.Tables["data"];
Infragistics.Win.Misc.UltraTile newTile = null;
for (int index = 0; index < dt.Rows.Count; index++)
{
newTile = new Infragistics.Win.Misc.UltraTile("Person X");
newTile.Control = new CustomControls.Controls.PersonResult("123", "123", index + 150);
newTile.Tag = new Guid("90D27721-7315-4B86-9CFD-4F7D02921E9A");
newTile.DoubleClick += TileDoubleClick;
tilePanel.Tiles.Add(newTile);
}
}
}));
}
else
{
if (_similaritySearchResults != null && _similaritySearchResults.Tables["data"].Rows.Count > 0)
{
DataTable dt = _similaritySearchResults.Tables["data"];
Infragistics.Win.Misc.UltraTile newTile = null;
for (int index = 0; index < dt.Rows.Count; index++)
{
newTile = new Infragistics.Win.Misc.UltraTile("Person X");
newTile.Control = new CustomControls.Controls.PersonResult("123", "123", index + 150);
newTile.Tag = new Guid("90D27721-7315-4B86-9CFD-4F7D02921E9A");
newTile.DoubleClick += TileDoubleClick;
tilePanel.Tiles.Add(newTile);
}
}
}
}, TaskScheduler.FromCurrentSynchronizationContext());
}
This is working fine, the application goes to a database then get results and update the UI, adding tiles to a control depending of the number of records returned by database.
Now, the problem comes when I try to add another background thread into my custom control:
new CustomControls.Controls.PersonResult("123", "123", index + 150);
The code for the control is:
protected override void InitLayout()
{
// if I comment this then everything works fine
// but if I leave this, then the UI freezes!!
GetPictureAsync();
base.InitLayout();
}
/// <summary>
///
/// </summary>
private void GetPictureAsync()
{
// This line needs to happen on the UI thread...
TaskScheduler uiScheduler = TaskScheduler.FromCurrentSynchronizationContext();
Task.Factory.StartNew(() =>
{
Random sleep = new Random();
System.Threading.Thread.Sleep(sleep.Next(1000,3000));
if (this.pbPhoto.InvokeRequired)
{
this.pbPhoto.Invoke(new Action(() =>
{
this.Load(#"E:\Photos\" + PhotoId.ToString() + ".jpg");
//this.pbPhoto.Image = Utility.Common.GetResourceImage("woman_sample.jpg");
}));
}
else
{
this.Load(#"E:\Photos\" + PhotoId.ToString() + ".jpg");
//this.pbPhoto.Image = Utility.Common.GetResourceImage("woman_sample.jpg");
}
}, CancellationToken.None, TaskCreationOptions.None, uiScheduler);
}
So the problem seems to be that I first execute a thread for looking when to start search, then inside that thread I run another thread in order to get data from database, and then each control updated in the UI will run another thread to get a picture and update a picturebox.
Anyone knows how to solve this? or a way to work around this?
When you call
new CustomControls.Controls.PersonResult("123", "123", index + 150)
Is "123" a literal string, or are they being read from UI controls. For example,
new CustomControls.Controls.PersonResult(txtFathersName.Text", txtMothersName.Text, index + 150)
i cant test it right now, but isnt accessing the Text property not allowed from a thread other than the one that created the control?
I think the problem lies in you forcing the Task in GetPictureAsync to execute on UI thread and then you are calling Thread.Sleep(). This update UI in Task using TaskScheduler.FromCurrentSynchronizationContext question tackles the same problem as you are having. I would rewrite your code as:
private void async GetPictureAsync()
{
Random sleep = new Random();
await TaskEx.Delay(sleep.Next(1000,3000));
this.Load(#"E:\Photos\" + PhotoId.ToString() + ".jpg");
}

All threads doing same job

I want to write network address to my listview, in a range like 192.168.0.0 -192.168.255.255
and I wrote a thread application but when I run this app, all threads are trying to add addresses to listview, does it has a simple solution?
here is my code:
namespace ListNetworkComputers
{
public partial class frmMain : Form
{
public frmMain()
{
InitializeComponent();
}
const int step = 16777216;
int threadCount = 1;
private void frmMain_Load(object sender, EventArgs e)
{
ıpAddressControl1.Text = "192.168.0.0";
ıpAddressControl2.Text = "192.168.255.255";
}
private void btnShowPcc_Click(object sender, EventArgs e)
{
threadCount = Convert.ToInt32(nudThreads.Value);
Thread[] threads = new Thread[threadCount];
for (int i = 0; i < threadCount; i++)
{
threads[i] = new Thread(new ThreadStart(getPerformance));
threads[i].Name = string.Format(i.ToString());
}
foreach (Thread t in threads)
{
t.Start();
}
}
private void getPerformance()
{
uint startIntAdress, endIntAdress;
startIntAdress = BitConverter.ToUInt32(IPAddress.Parse(ıpAddressControl1.Text).GetAddressBytes(), 0);
endIntAdress = BitConverter.ToUInt32(IPAddress.Parse(ıpAddressControl2.Text).GetAddressBytes(), 0);
for (uint i = startIntAdress; i < endIntAdress; i = i + step)
{
string ipAddress = new IPAddress(BitConverter.GetBytes(i)).ToString();
lbNetworkComputers.Items.Add(ipAddress);
}
}
}
}
And an another problem is, my step method (increaseing adresses as 16777216 ...) isnt working healthy. it goes 192.168.0.0 to 192.168.0.255 but doesnt go on after that.
Because they get same startIntAdress and endIntAdress. Split the range evenly for all threads.
It should be like this:
Thread 1 starts at 192.168.0.0 and checks 32 addresses
Thread 2 at 192.168.0.31 and checks 32,
Thread 3 at 192.168.0.63 and checks 32,
etc
Each thread is running exactly the same code as your loop over the IP addresses is inside the method passed to each thread.
You should pass different start and end addresses into each thread.
You'll also have problems with the threads accessing the UI.
From the code you've posted I'm not sure this really needs to be threaded.

The calling thread cannot access this object because a different thread owns it

So I am making a simple brick breaking game in c#/wpf. I am running into an issue using timers, I feel like it is probably a simple fix but here is whats happening. Whenever t_Elapsed is fired it attempts to call Update() but when it does its like OMG Im not in the right thread so I cant do that sir. How do I invoke the method from the Game from the proper thread? (And yes I know the code is ugly and has magic numbers but I just kinda chugged it out without putting a lot of effort in. And yes I have zero experience programming games)
public partial class Game : Grid
{
public bool running;
public Paddle p;
public Ball b;
Timer t;
public Game()
{
Width = 500;
Height = 400;
t = new Timer(20);
p = new Paddle();
b = new Ball();
for (int i = 15; i < 300; i += 15)
{
for (int j = 15; j < 455; j += 30)
{
Brick br = new Brick();
br.Margin = new Thickness(j, i, j + 30, i + 15);
Children.Add(br);
}
}
Children.Add(p);
Children.Add(b);
p.Focus();
t.AutoReset = true;
t.Start();
t.Elapsed += new ElapsedEventHandler(t_Elapsed);
}
void t_Elapsed(object sender, ElapsedEventArgs e)
{
if (running)
{
Update();
}
}
void Update()
{
b.Update(); //Error here when Update is called from t_Elapsed event
}
void Begin()
{
running = true;
b.Initiate();
}
}
You should use the DispatcherTimer object instead, it will ensure that the timer events are published to the correct thread.
Timer elapsed events fire on a thread from the thread pool (http://www.albahari.com/threading/part3.aspx#_Timers) and not on the UI thread. Your best approach is to invoke the control's dispatcher through a call like this:
yourControl.Dispatcher.BeginInvoke(
System.Windows.Threading.DispatcherPriority.Normal
, new System.Windows.Threading.DispatcherOperationCallback(delegate
{
// update your control here
return null;
}), null);
The calling thread cannot access this object because a different thread owns it
this.Dispatcher.Invoke((Action)(() =>
{
...// your code here.
}));

Categories