Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Visual C# Multi-threading Problem

Posted on 2009-07-01
4
Medium Priority
?
413 Views
Last Modified: 2012-05-07
Im using Visual Studio 2008 and Visual C#

Im developing a Windows application (not Internet-based). I click on a button which calls a function which reads data from some hardware. This can take up to ten minutes. When the data acquisition is complete the data is processed and displayed in charts etc.

The problem is without using threads my application locks up for ten minutes during the acquisition phase. Id like to be able to open some other windows while the data is being acquired.

The attached code shows what Ive tried so far. This basically works correctly as it is shown. When the AcquireData procedure is running I can still use all the other menu options in my application. However when I substitute the Sleep function with the code that actually acquires the data from the hardware then my application locks up and I get the "white-out" effect when I try and click on menus/windows.The data is acquired OK and the results are displayed OK (in the ProcessData function) its just my application is still locked up during the data acquisiion phase.

Does anyone know whats going on. As I say it does what I expect when the Sleep function is in the AcquireData function but nt when I replace this with the code that actually acquires the data.
public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
 
        private delegate void ProcessDataDelegate();
 
        private void button1_Click(object sender, EventArgs e)
        {
            Thread trd = new Thread(new ThreadStart(this.AcquireData));
            trd.IsBackground = true;
            trd.Start();
        }
 
        private void AcquireData()
        {
            // ...simulated acquiring data...
            System.Threading.Thread.Sleep(100000); 
            this.Invoke(new ProcessDataDelegate(ProcessData));
        }
 
        private void ProcessData()
        {
            // ...this is now back on the main UI thread...
 
            this.label1.Text = "Processed data...";
        }
    }

Open in new window

0
Comment
Question by:rangers99
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
4 Comments
 
LVL 86

Expert Comment

by:Mike Tomlinson
ID: 24755064
I would think we'd have to look at the hardware acquisition code...

Are you using any third-party controls or libraries?
0
 

Accepted Solution

by:
rangers99 earned 0 total points
ID: 24764764
Ive just contacted the hardware manufacturer and Ive been informed I cannot multi-thread the data acquisition procedure in a Visual C# application.

Does anyone know how I close this question?
0
 
LVL 86

Expert Comment

by:Mike Tomlinson
ID: 24765315
Glad we narrowed down the problem...tis unfortunate that we can't solve it for you though!  =\

0

Featured Post

Vim Reference Guide

Vim is a powerful text editor favored by many sysadmins and developers - here are some commands that you'll want to keep in your back pocket!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK (http://www.microsoft.com/en-us/download/details.aspx?id=27876) for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
The goal of this video is to provide viewers with basic examples to understand and use structures in the C programming language.
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use while-loops in the C programming language.

715 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question