Problem with a worker thread causing app to not respond

  • Thread starter Thread starter Dante
  • Start date Start date
D

Dante

Hello all,

I have an application that is used for porting data from one system to
another. There is a scan button that starts a new thread that does the
processing.
Everything seems to work fine unless the application loses focus. If the
app loses focus the form will no longer refresh and it just sits there. If
I click where one of the buttons used to be the app just says not responding
in the title bar.

I am new to using threads so please be very descriptive in your reply.

Here is the code that is executed when the scan button is clicked:
System.Threading.ThreadStart ts = new
System.Threading.ThreadStart(this.runScan);
System.Threading.Thread scanthread = new System.Threading.Thread(ts);
scanthread.IsBackground = true;
scanthread.Start();

Thanks
 
1) Does the app work if it does not lose focus?

2) Are you sharing data between the worker thread and the GUI? If so how?

--Richard
 
Can you post the code from the thread function (runScan) as well please

Regards

Richard Blewett - DevelopMentor
http://staff.develop.com/richardb/weblog

nntp://news.microsoft.com/microsoft.public.dotnet.languages.csharp/<[email protected]>

Hello all,

I have an application that is used for porting data from one system to
another. There is a scan button that starts a new thread that does the
processing.
Everything seems to work fine unless the application loses focus. If the
app loses focus the form will no longer refresh and it just sits there. If
I click where one of the buttons used to be the app just says not responding
in the title bar.

I am new to using threads so please be very descriptive in your reply.

Here is the code that is executed when the scan button is clicked:
System.Threading.ThreadStart ts = new
System.Threading.ThreadStart(this.runScan);
System.Threading.Thread scanthread = new System.Threading.Thread(ts);
scanthread.IsBackground = true;
scanthread.Start();

Thanks




---
Incoming mail is certified Virus Free.
Checked by AVG anti-virus system (http://www.grisoft.com).
Version: 6.0.771 / Virus Database: 518 - Release Date: 28/09/2004



[microsoft.public.dotnet.languages.csharp]
 
Here is the runScan function:

==================================

btn_scan.Enabled = false;
btn_update.Enabled = false;
disableMenuItems();
pnl_progress.Visible = true;
pnl_progress.BringToFront();
lbl_progress.Text = "Gathering data... Please wait.";
pnl_progress.Refresh();

//Based on the tab that is selected, call the appropriate function
ScanFunctions sf = new ScanFunctions(this);
switch (this.tabs.SelectedTab.Name)
{
case "tabAccounts":
sf.scanAccounts();
break;
case "tabContacts":
sf.scanContacts();
break;
case "tabNewContracts":
sf.scanNewContracts();
break;
case "tabEquipment":
sf.scanEquipment();
break;
case "tabCalls":
sf.scanCalls();
break;
case "tabMeters":
sf.scanMeters();
break;
case "tabInvoices":
sf.scanInvoices();
break;
}

btn_scan.Enabled = true;
btn_update.Enabled = true;
statusBar1.Panels[0].Text = "";
enableMenuItems();
pnl_progress.Visible = false;
Cursor.Current = System.Windows.Forms.Cursors.Default;

==============================

the ScanFunctions class makes a connection to both a SQL server and a
web service where it gathers data and then populates two ListViews on
the main form (this).

Thanks,
Dante
 
OK, the problem is you are updating the UI from teh work thread - in WIndows the only thread that can work on the UI is the thread that created the UI elements. All of the interaction with the form and it's controls needs to be marshalled back to the UI thread which can be done via the BeginInvoke method of the Control class.

Regards

Richard Blewett - DevelopMentor
http://staff.develop.com/richardb/weblog

nntp://news.microsoft.com/microsoft.public.dotnet.languages.csharp/<[email protected]>

Here is the runScan function:

==================================

btn_scan.Enabled = false;
btn_update.Enabled = false;
disableMenuItems();
pnl_progress.Visible = true;
pnl_progress.BringToFront();
lbl_progress.Text = "Gathering data... Please wait.";
pnl_progress.Refresh();

//Based on the tab that is selected, call the appropriate function
ScanFunctions sf = new ScanFunctions(this);
switch (this.tabs.SelectedTab.Name)
{
case "tabAccounts":
sf.scanAccounts();
break;
case "tabContacts":
sf.scanContacts();
break;
case "tabNewContracts":
sf.scanNewContracts();
break;
case "tabEquipment":
sf.scanEquipment();
break;
case "tabCalls":
sf.scanCalls();
break;
case "tabMeters":
sf.scanMeters();
break;
case "tabInvoices":
sf.scanInvoices();
break;
}

btn_scan.Enabled = true;
btn_update.Enabled = true;
statusBar1.Panels[0].Text = "";
enableMenuItems();
pnl_progress.Visible = false;
Cursor.Current = System.Windows.Forms.Cursors.Default;

==============================

the ScanFunctions class makes a connection to both a SQL server and a
web service where it gathers data and then populates two ListViews on
the main form (this).

Thanks,
Dante



---
Incoming mail is certified Virus Free.
Checked by AVG anti-virus system (http://www.grisoft.com).
Version: 6.0.771 / Virus Database: 518 - Release Date: 28/09/2004



[microsoft.public.dotnet.languages.csharp]
 
Back
Top