How to avoid red cross and freeze when using a BackGroundWorker in Datagridview?


#1

Hello,
There are 2 issues when I tried adding data in datagridview when using backgroundworker,
1.The datagridview will freezing for a while like below:(Scrollbar cannot work)

question

The C# solution example:

private static object obj = new object();
private delegate void ChangeFunction(PackageMessage pm, string Message);
public void AddWarning(PackageMessage pm, string Message)
{
    if (dgv.InvokeRequired)
    {
       ChangeFunction c = new ChangeFunction(AddWarning);
       this.Invoke(c, new object[] { pm, Message });
    }
    else
    {
       lock (obj)
       {
          DataRow dr = MessageDT.NewRow();
          dr[0] = pm.GetSendTime().ToString();
          dr[1] = pm.GetSourceIP().ToString() + ":" + pm.GetSourcePort().ToString();
          dr[2] = pm.GetDestinaionIP().ToString() + ":" + pm.GetDestinationPort().ToString();
          dr[3] = Message;
          MessageDT.Rows.Add(dr);
        }
     }
}

2.The datagridview sometimes will show a big red cross:

red%20cross

The C# solution example:
https://community.pega.com/knowledgebase/articles/pega-rpa/robotic-automation-example-correcting-datagridview-red-x-error


I just wondering is that possible to fix the 2 issues via maxscript code?Thanks in advance.

This is the example code:

rollout test_red_cross "Red Cross"
(
	dotNetControl test_dgv "System.Windows.Forms.DataGridView" pos:[0,0] width:590 height:450
	on test_red_cross open do
	(
		Red_title=dotNetObject "System.Windows.Forms.DataGridViewTextBoxColumn"
		test_dgv.columns.add Red_title
		Red_title.width=500
			
		Fn Add_data=
		(
			for i=1 to 30 do test_red_cross.test_dgv.rows.add #(random 300 999)
		)

		test_backwork=dotnetobject "CSharpUtilities.SynchronizingBackgroundWorker"
		test_backwork.WorkerReportsProgress=true
		test_backwork.WorkerSupportsCancellation=true  
		dotNet.addEventHandler test_backwork "DoWork" Add_data
		
		if not test_backwork.IsBusy do test_backwork.RunWorkerAsync()	
	)
)
CreateDialog test_red_cross 600 450

#2

for the 1st , you can’t change UI in dowork , that’s forbidden , UI need change in ProgressChanged

rollout test_red_cross "Red Cross"
(
	dotNetControl test_dgv "System.Windows.Forms.DataGridView" pos:[0,0] width:590 height:450
	on test_red_cross open do
	(
		Red_title=dotNetObject "System.Windows.Forms.DataGridViewTextBoxColumn"
		test_dgv.columns.add Red_title
		Red_title.width=500
		fn dow s e=
		(
			for i = 1 to 30 do
				s.ReportProgress i
		)
		Fn Add_data s e =
		(
			test_red_cross.test_dgv.rows.add #(e.ProgressPercentage)
		)
		test_backwork=dotnetobject "CSharpUtilities.SynchronizingBackgroundWorker"
		test_backwork.WorkerReportsProgress=true
		test_backwork.WorkerSupportsCancellation=true 
	    dotNet.addEventHandler test_backwork "dowork" dow
		dotNet.addEventHandler test_backwork "ProgressChanged" Add_data
		if not test_backwork.IsBusy do test_backwork.RunWorkerAsync()	
	)
)
CreateDialog test_red_cross 600 450

I have no idea for 2cd , I got it sometime , it throws info no enough memory , but when I set memory to 50M from 15M , nothing help .


#3

Thanks for great help,it works!
The red cross issue maybe can be fixed via maxscript,I just don’t know how can do it:expressionless: