I asked a similar question here; thanks to everyone who provided suggestion! However, it seems that my problem is bigger than the one described above, so I am posting a new question.
The issue is that I need to keep my UI responsive when I am loading a large document using a third party control called document
. The document
is a class embedded in a third party control, there is no way for me to new it, and it doesn't have Invoke
function.
I am trying to use Backgroundworker
for this. The code I try is of this form:
public class MyForm: Form
{
private delegate bool OpenFile(string filePath);
private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
{
OpenFile oF = new OpenFile(document.Open);
var openFile = this.Invoke(oF, MyFileName);
e.Result = openFile;
}
}
Now, in the UI thread, where document
belongs, when it is doing the Open
operation, a dialog box is supposed to come up and updating itself and the main thread is supposed to remain responsive ( i.e., I can still drag and drop the form anyway I like). This is what I was trying to accomplish.
The problem is that the above code will make both of my threads ( the main UI thread and the dialog box thread) hang during the time of loading. My question is,is there anyway to fix this issue?
P/S: I have tried var openFile = Invoke(oF, MyFileName)
and var openFile = BeginInvoke(oF, MyFileName)
and var openFile = OtherControl.Invoke(oF, MyFileName)
, all didn't help.