16. november 2009 - 22:38Der er
8 kommentarer og 1 løsning
InvokeRequired ckeckbox
Har en baggrundstråd, der skal ændre state af en checkbox. Klassen, hvor dette skulle foregå ses nedenfor. Jeg har forsøgt at implementerer en delegate, men kan ikke få det til at virke. Hvad er galt ?
using System; using System.Windows.Forms;
namespace Hysteresis { public class ValveAlarmView { CheckBox checkBoxVac; CheckBox checkBoxAtm; private delegate void CheckboxDelegate(bool alarm);
Første problem der lige springer mig i øjene er din "SetAlarmVac" if (checkBoxVac.InvokeRequired) checkBoxVac.Invoke(new CheckboxDelegate(SetAlarmVac), new[] {alarm }); else checkBoxVac.Checked = alarm;
Du mangler en else i den ... eller bliver checkBoxVac jo sat lige meget om det er form thread owner eller ej ...
System.ArgumentException: Object of type 'System.Boolean[]' cannot be converted to type 'System.Boolean'. at System.Windows.Forms.Control.MarshaledInvoke(Control caller, Delegate method, Object[] args, Boolean synchronous) at System.Windows.Forms.Control.Invoke(Delegate method, Object[] args) at Hysteresis.ValveAlarmView.SetAlarmVac(Boolean alarm) in C:\Documents and Settings\mko\My Documents\Visual Studio 2008\Projects\Hysteresis\Hysteresis\ValveAlarmView.cs:line 23 at Hysteresis.SerielValve.CloseValveVac() in C:\Documents and Settings\mko\My Documents\Visual Studio 2008\Projects\Hysteresis\Hysteresis\SerielValve.cs:line 75
if (checkBoxVac.InvokeRequired) checkBoxVac.Invoke(new CheckboxDelegate(SetAlarmVac), new {alarm }); else checkBoxVac.Checked = alarm; } public void SetAlarmAtm(bool alarm) { if (checkBoxAtm.InvokeRequired) checkBoxAtm.Invoke(new CheckboxDelegate(SetAlarmAtm), new { alarm }); else checkBoxAtm.Checked = alarm; } } }
Det giver fejlen:
11:09:09: System.ArgumentException: Object of type '<>f__AnonymousType0`1[System.Boolean]' cannot be converted to type 'System.Boolean'. at System.Windows.Forms.Control.MarshaledInvoke(Control caller, Delegate method, Object[] args, Boolean synchronous) at System.Windows.Forms.Control.Invoke(Delegate method, Object[] args) at Hysteresis.ValveAlarmView.SetAlarmVac(Boolean alarm) in C:\Documents and Settings\mko\My Documents\Visual Studio 2008\Projects\Hysteresis\Hysteresis\ValveAlarmView.cs:line 23 at Hysteresis.SerielValve.CloseValveVac() in C:\Documents and Settings\mko\My Documents\Visual Studio 2008\Projects\Hysteresis\Hysteresis\SerielValve.cs:line 75
Tilladte BB-code-tags: [b]fed[/b] [i]kursiv[/i] [u]understreget[/u] Web- og emailadresser omdannes automatisk til links. Der sættes "nofollow" på alle links.