In your case I'd recommend creating a method on your form that accepts a
custom object (structure) that contains the label values and the progress bar
values. I'd then invoke this method in the secondary thread. This optimizes
your performance by making only a single cross-thread call per loop instead
of 3. Here is some sample code:
struct UpdateData
{
public UpdateData ( string status, long progress, params string[] items )
{
Status = status;
Progress = progress;
Items = items;
}
public string Status;
public string[] Items;
public long Progress;
}
class MyForm : Form
{
delegate void UpdateStatusDel egate ( UpdateData data );
void UpdateStatus ( UpdateData data )
{
if (InvokeRequired )
Invoke(new UpdateStatusDel egate(UpdateSta tus), new object[] { data
});
else
{
foreach(string item in data.Items)
listbox1.Items. Add(item);
label7.Text = data.Status;
statusBarPanel2 .Reset();
statusBarPanel2 .Step();
statusBarPanel2 .ProgressPositi on = data.Progress;
};
}
}
public void PrinterStatusNP I7FC6D2()
{
string trayvalue = string.Empty;
///Tray 3
RFC1157.Mgmt mib = new RFC1157.Mgmt();
Snmp.ManagerSes sion sess=new
Snmp.ManagerSes sion(Class1.sel ectLJPrinter,"p ublic");
Snmp.ManagerIte m mi=new
Snmp.ManagerIte m(sess,mib.OID( "1.3.6.1.2.1.25 .3.5.1.1.1"));
UpdateData data = new UpdateData();
string trayvalue = mi.Value.ToStri ng();
data.Items = new string[] { "Tray 3", trayvalue };
data.Status = String.Concat(" Printer Status: ", trayvalue);
f (trayvalue == "0")
{
//This could be bad
PrinterBreaks = "BreaksOn";
Tray3 frmText3 = new Tray3();
frmText3.ShowDi alog();
}
//Maybe this is separate??
data.Progress = (long)Class1.to NerlOng;
//UI update
frmObj.UpdateSt atus(data);
}
One thing that I'd be concerned about is that form creation in your
secondary thread. That is generally a bad idea because you are creating a
new form on a background thread. I'd try to do that some other way instead.
Michael Taylor - 9/16/05
"trint" wrote:
Michael,
When it reaches gotoForeach();, from there a statusbar is constantly
updated and so are several label controls. I will have to somehow
address each call to a control correctly throughout the code.From the snipit I have given, it doesn't show the calls to update the
status bar or the text labels. Here is an another example of where I
need help in protecting this code:
public void PrinterStatusNP I7FC6D2()
{
string trayvalue = string.Empty;
///Tray 3
RFC1157.Mgmt mib = new RFC1157.Mgmt();
Snmp.ManagerSes sion sess=new
Snmp.ManagerSes sion(Class1.sel ectLJPrinter,"p ublic");
Snmp.ManagerIte m mi=new
Snmp.ManagerIte m(sess,mib.OID( "1.3.6.1.2.1.25 .3.5.1.1.1"));
// listBox1.Items. Add("Tray 3".ToString( ));
// listBox1.Items. Add(mi.Value.To String());
trayvalue = Convert.ToStrin g(mi.Value);
label7.Text = string.Format(" Printer Status: {0}",trayvalue) ;
if (trayvalue == "0")
{
PrinterBreaks = "BreaksOn";
Tray3 frmText3 = new Tray3();
frmText3.ShowDi alog();
}
}
one more:
statusBarPanel2 .Reset();
statusBarPanel2 .Step();
statusBarPanel2 .ProgressPositi on = ((long)(Class1. toNerlOng));
Really would appreciate your help.
Thanks,
Trint