I tried to search for solutions but none work so I’m asking directly. I’m using a BackGroundWorker
and while I’m drawing a Bitmap I’d like to show it in the PictureBox
. It seems tricky to do so at least I wanted to show a progress percentage in a label but the label does not update during the process. The Work calls a function passing parameters (a Bitmap and a String). To show the value in the label I’d use the ProgressChange
event (also it’s what you’d expect since it’s literally stated in a MS Docs example). During the function I`m doing this:
uint dimen = (uint)pbo.Width * (uint)pbo.Height;
uint count = 0;
Bitmap pbn = new Bitmap(pbo, new Size(pbo.Width * 2, pbo.Height * 2));
for loop (some values)
{
//do stuff
count++;
backgroundWorker1.ReportProgress((int)((count / dimen) * 100), text);
}
for loop (other values)
{
//do stuff
count++;
backgroundWorker1.ReportProgress((int)((count / dimen) * 100), text);
}
for loop (other values)
{
//do stuff
count++;
backgroundWorker1.ReportProgress((int)((count / dimen) * 100), text);
}
Literally the progress event just calls label1.Text = $"{e.UserState} {e.ProgressPercentage}%";
. Whatever you don’t see in this code doesn’t matter. This is mainly the part that doesn’t work. The report function of the BGW is set to true (in fact it kinda works but not as I’d expect since it seems to work very slowly, meaning not during my function, or not at all).
I know I’m not refreshing. And I don’t see why I should, since MS Docs don’t do that. If I try to refresh the label after setting its content like that, I get this error:
Full error messange: Managed Debugging Assistant ‘DisconnectedContext’ : ‘Transition into COM context 0xad4f80 for this RuntimeCallableWrapper failed with the following error: An outgoing call cannot be made since the application is dispatching an input-synchronous call. (Exception from HRESULT: 0x8001010D (RPC_E_CANTCALLOUT_ININPUTSYNCCALL)). This is typically because the COM context 0xad4f80 where this RuntimeCallableWrapper was created has been disconnected or it is busy doing something else and cannot process the context transition. No proxy will be used to service the request on the COM component and calls will be made to the COM component directly. This may cause corruption or data loss. To avoid this problem, please ensure that all COM contexts/apartments/threads stay alive and are available for context transition, until the application is completely done with the RuntimeCallableWrappers that represents COM components that live inside them.’
Any ideas on how to change the code and still maintain kinda this structure? I also mean continuing using the BGW
.