0

So my problem is this. I have a form with a panel in it. This panel serves as the container for multple different usercontrols, of which only one is visible at a time. Some of said usercontrols display a lot of data from the DB, so loading them takes a little bit of time (the data is shown in a bound datagridview). I tried creating a LoadData-method for these controls that I then launch in separate threads and once they've done their work, they enable the actual buttons on the main form for displaying them.

There were, however, many different problems. First, I can't call this.Invoke on the usercontrol until its handle is created, which seems quite hard to force, especially if I want to show a splash screen during the initialization (the main form's handle isnt created yet).

I did manage to force this by setting the form.Visible = true and then calling form.CreateControl followed by form.visible = false. This does, however, show the form blinking on the screen which doesn't look nice.

I also tried not using Invoke if the handle is not yet created, but this brings me to the problem of my data object being created in a different thread and then not being accessible for the control's "normal" thread.

So as is probably quite obvious, I'm quite lost when it comes to multithreading, especially so with winforms, and even more so at the launch of the application. My explanation might also be rather confusing, but I'll try and clarify if it's needed.

So what is the correct way of doing this?

bobblez
  • 1,340
  • 20
  • 35
  • If your user can't interact with your program while you load your data, isnt't it pointeless to move the data loading inside a thread? – Steve Mar 15 '12 at 08:34
  • The controls with less data are loaded during the splash, and are available immediately. – bobblez Mar 15 '12 at 14:54

1 Answers1

0

Hard to know where to start with this.

Can't figure out whether your problems are premature optimistion, or trying to retro fit multi-threading.

If I had say a six buttons and one panel and the buttons flipped a usercontrol in the panel to visible, My thread would return a user control, which I'd then add to the panel and then enable it's related button.

Takes all the synchronisation stuff right out of the equation, and gives you maximum scope for optimistion the getting and setting up of the controls.

Or you could setup all the user controls but not bind them and get your threads to bind on completion, I prefer the former way though, a bit more abstraction and you could get to define a view, mark it up with some useful attribute and just get you main form to kick off a process which would "just do it".

Tony Hopkinson
  • 20,172
  • 3
  • 31
  • 39
  • It's more of a retro fitting multh-threading -kinda problem. And yes that description of the functionality of controls and the panel is quite accurate. And this sounds like a good solution, but how exactly can you make a thread return something? I didnt even know such a thing was possible. – bobblez Mar 15 '12 at 14:55
  • Well yeah, that's what I'm using but that's passing data TO the thread, not the thread returning it. But oh well, guess I'll manage to figure it out. – bobblez Mar 15 '12 at 20:35
  • This should help. Several options. http://stackoverflow.com/questions/1314155/returning-a-value-from-thread – Tony Hopkinson Mar 15 '12 at 23:17