views:

69

answers:

5

I am displaying dialog on the main form, now one for loop is working behind but when that dialog will displayed code execution will be stopped, but I don't want to let stop the execution of the code, is any other any way to do that ?

EDIT: I am using now thread to do that and my code is like

    Thread t;
    private void StartParsingByLoop()
    {
       t = new Thread(new ThreadStart(RunProgress));
       t.Start();
       for (int i = 0; i < dialog.FileNames.Length; i++)
       {
          cNoteToParsed.AllContrctNotesFilePath.Add(dialog.FileNames[i].ToString());
       }
          cNoteToParsed.ParseContractNote();
          if (cNoteToParsed.NOfContrctNoteParsed > 0)
                LoadTransactionsInDataGridView();
          t.Abort();
    }

    private void RunProgress()
    {
        frmProgress progressForImportingTran = new frmProgress("Importing Transactions", "ok");
        progressForImportingTran.ShowDialog();
    }

Now I have problem is that the dialog that shows the progress does not behave like dialog and gives access of the main form and if we try to access the main form then dialog goes to hide. And I dont want to make the dialog be hide.

+1  A: 

You can let a different thread handle the loop.

Response to edit: Can you provide more details, perhaps some code? what is the loop doing? what form do you display?

Oren A
A: 

(this answer is based on the assumption that we are taking about a winforms app)

Show the form using the Show method, rather than ShowDialog. By passing a reference to the main form, the dialog will stay on top of the main form even if it is not modal:

TheDialog dialog = new TheDialog();
dialog.Show(this);

Note though that the user can still interact with the controls on the main form, so you might want to disable some controls, depending on your scenario.

You state in your question that there are requirements that prevent you from using threading for this. This kind of requirement strikes me as odd, and it is a pity because this is one of the typical scenarios when you would want to use some sort of asynchronous construct. By performing heavy work on the UI thread, you get some drawbacks, including:

  • The UI will not be responsive - if you want to allow the user to cancel the work by clicking a button, that will be tricky to achieve in a robust manner.
  • The UI will not redraw properly since the UI thread is busy performing other work.
Fredrik Mörk
I want to shot that form modally because it displays some process that is working behind to the user.
Harikrishna
@Harikrishna: Then this approach should work quite well for you, even though you might get issues with the UI not updating properly, since that is handled by the same thread that is performing the work. Some sort of asynchronous model (thread/threadpool, background worker) is really the way to go in this kind of situation.
Fredrik Mörk
Is not possible to display show form modally without using thread or background worker ?
Harikrishna
@Harikrishna Its not possible :D
SaeedAlg
@Fredrik, this work like passing form as reference, and and dialog shows as a modally also , but label within the that dialog loses its text , I want to show that text only, is it possible ?
Harikrishna
@Harikrishna: it's impossible to give more specific advise without seeing some code showing what you are trying to do.
Fredrik Mörk
A: 

Do not use ShowDialog() but Show() to display the dialog windows. Show() will immediately return to the caller whereas ShowDialog() will hold the execution. Note that when using Show() your dialogs won't be modal anymore.

Marius Schulz
@Marius, I want to shot that form modally because it displays some process that is working behind to the user.
Harikrishna
A: 

When u want to show it modal the thread execution will pass to the modal form, so u should use other thread to do ur background, its not possible using modal and no other thread and have a ... also u can use Parallel Programing in .net 4

SaeedAlg
A: 

OK, so you don't need modal dialog, you need a mechanism for your user not to be able to select your main form while processing is enabled.

Modal dialog doesn't mean that execution is stopped - it just happens somewhere else.

There are several methods to do this, and you ruled out new thread creation (don't know why, it would solve it elegantly). If don't use thread, you'll have another problem - your processing should be done in CHUNKS and every little while you'll have to do something like Application.DoEvents() to enable your application to process messages and not be frozen to the user.

So, if you can create a method in the main form that shows your 'please wait' dialog which will perform some work and later do some more until is finished, you can do this:

  • create a new form (wait dialog)
  • start a timer inside of it and wire timer to the PARENT form
  • timer interval should be 1
  • ShowDialog() the form
  • on timer event do small amount of work (don't allow it to go more then 1/10 of seconds)

Can you do that?

Anyway:

  • task can't be split into small workable pieces
  • you can't use threads
  • you want your UI to be responsive

PICK 2. You can't have all 3 in Winforms.

Daniel Mošmondor