Z
zorrothefox.groups
Hi,
I have a C# Windows Form which creates a background
thread for downloading data to a serial port. In the background
thread, I need to update the GUI controls with the progress so far,
log messages etc. Initially I tried directly accessing the form
control from the background thread, which gave me a cross-thread
operation exception.
So I am now using the following approach:
1) Define a delegate and a corresponding member function for each
operation that I do.
2) In the background thread, I call the member function.
3) In the function, I check whether the control.InvokingRequired is
true
if true, I invoke the control in the proper thread
else
I call the control's relevant method (as this is
the main thread)
for example
1) delegate void ClearList(ListView list);
2) BackgroundThread()
{
ListClear(myForm.myListView);
}
2) public void ListClear(ListView list)
{
if(list.InvokeRequired == true)
{
ClearList cl = new ClearList(ListClear);
this.Invoke(cl,new object[]{list});
}
else
{
list.Clear();
}
}
However, the above technique seems to be a bit klunky (IMO). Is there
no way that this could be automated such that for each control's
function, I need not add a delegate? Since there are quite a few
controls that I need to access in the background thread (at least for
read purpose), the number of delegates and functions seem to be
excessive.
After all, at least logically, each control knows already which thread
it belongs to. Could it not be arranged in such a way that we add some
code to the form such that whenever a control is accessed, the form
automatically checks for the thread accessing the control, and passes
the same request onto the proper thread?
I know that this might be too much to ask, but given some kick-ass C#
features like reflection, it at least seems that there should be SOME
way of achieving this.
Thanks in advance,
Bharat
I have a C# Windows Form which creates a background
thread for downloading data to a serial port. In the background
thread, I need to update the GUI controls with the progress so far,
log messages etc. Initially I tried directly accessing the form
control from the background thread, which gave me a cross-thread
operation exception.
So I am now using the following approach:
1) Define a delegate and a corresponding member function for each
operation that I do.
2) In the background thread, I call the member function.
3) In the function, I check whether the control.InvokingRequired is
true
if true, I invoke the control in the proper thread
else
I call the control's relevant method (as this is
the main thread)
for example
1) delegate void ClearList(ListView list);
2) BackgroundThread()
{
ListClear(myForm.myListView);
}
2) public void ListClear(ListView list)
{
if(list.InvokeRequired == true)
{
ClearList cl = new ClearList(ListClear);
this.Invoke(cl,new object[]{list});
}
else
{
list.Clear();
}
}
However, the above technique seems to be a bit klunky (IMO). Is there
no way that this could be automated such that for each control's
function, I need not add a delegate? Since there are quite a few
controls that I need to access in the background thread (at least for
read purpose), the number of delegates and functions seem to be
excessive.
After all, at least logically, each control knows already which thread
it belongs to. Could it not be arranged in such a way that we add some
code to the form such that whenever a control is accessed, the form
automatically checks for the thread accessing the control, and passes
the same request onto the proper thread?
I know that this might be too much to ask, but given some kick-ass C#
features like reflection, it at least seems that there should be SOME
way of achieving this.
Thanks in advance,
Bharat