Menu on windows forms not refreshing.

M

mary jones

Hi:
When I try to refresh Windows forms which contains a menu
doesn't refresh the menu.
this is what my code looks like:

if( DialogResult.OK == MessageBox.Show( "Please UnZip the
file: " + fileName + " , and then press ok", "Some Text
Advisor", MessageBoxButtons.OKCancel ))
{
this.Parent.Parent.Refresh();
this.Parent.Parent.Hide();
if ( fileName.IndexOf ( ".esd" ) > 0 )
{
Cursor.Current = Cursors.WaitCursor;
//importing of files processing
}
else if ( fileName.IndexOf ( ".ra3" ) > 0 )
{
Cursor.Current = Cursors.WaitCursor;
//importing of files processing
}


string temp = fileName.Substring(

System.Math.Max( fileName.LastIndexOf( '\\' ),
0 ) );


string fileToBeMoved =
processedDirectory.FullName + "\\" + temp;

if ( File.Exists ( fileToBeMoved ))

File.Delete ( fileToBeMoved );

File.Move ( fileName, fileToBeMoved );

Cursor.Current = Cursors.Default;
}

When this loop is doing importing of files, and click on
any where on menu or any control on windows form, my menu
just whites out and the forms title says (not
responding ), though it is still doing import work.
How can I resolve this, please help, anybody faced same
problem?
 
J

John Elliot

The problem is that your file import code is running on the main thread.
Windows messages will not be processed until the file processing is
complete. You need windows messages to be processed so that your app can
respond to the user clicking on the menu and other controls. Your best
option is to run the file processing on another thread, allowing the UI to
remain responsive. Otherwise you could consider calling
Application.DoEvents() periodically during the import (although its not a
great idea).

John.
 
M

Mary Jones

Thanks for the response. There is a small mistype in my
previous message:

The code should look like this:

if( DialogResult.OK == MessageBox.Show( "Please UnZip the
file: " + fileName + " , and then press ok", "Some Text
Advisor", MessageBoxButtons.OKCancel ))
{
this.Parent.Parent.Refresh();
if ( fileName.IndexOf ( ".esd" ) > 0 )
{
Cursor.Current = Cursors.WaitCursor;
//importing of files processing
}
else if ( fileName.IndexOf ( ".ra3" ) > 0 )
{
Cursor.Current = Cursors.WaitCursor;
//importing of files processing
}
string temp = fileName.Substring(

System.Math.Max( fileName.LastIndexOf( '\\' ),
0 ) );
string fileToBeMoved =
processedDirectory.FullName + "\\" + temp;

if ( File.Exists ( fileToBeMoved ))
File.Delete ( fileToBeMoved );
File.Move ( fileName, fileToBeMoved );
Cursor.Current = Cursors.Default;
}

I deleted line: "this.Parent.Parent.Hide();", which is
not part of code.

My problem is, before I start importing files even in
same thread, I have set my cursor to 'WaitCursor', the
mouse click should not raise any event on the form and
should not white out the menu.
If we look at the above code, before I start importing
files, I am refreshing the form and setting the
Cursor.Current = Cursors.WaitCursor.
The problem is that your file import code is running on the main thread.
Windows messages will not be processed until the file processing is
complete.
This is exactly I want, but my mouse click during
importing files is raising some event and hiding the menu.

Please help.
 
Top