Windows Develop Bookmark and Share   
 index > Windows Forms General > A threading problem
 

A threading problem

I have an desktopapplication that fills some data in comboin the login form.While the data is being fetched form do not display. I tried to use threading but that too does not seem to be working as threading cannot access the combo created on another thread. Then I used datatable and assigned it as datasource to combo prior to starting the thread but this also does not work as datatable is filled but data is not shown in combo.

Can anybody help me out I just want that login form get displayed and in the background a thread fill the combo.For the duration of this time a message will be displayed on the form.

SunilKumar111  Thursday, October 04, 2007 5:34 AM

hi,

you can refer here for multithreaded window control example

http://msdn2.microsoft.com/en-us/library/3s8xdz5c.aspx

a example for you, you will see thelistBox1 is still being appended after form shown. And, at the same time, users can still click on the button to add the timestamp to list box2 .

Hope it can help you. Smile

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace WindowsApplication2
{
public delegate void UpdateListBox();

public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();


}

private void ShowNumbers()
{
toolStripStatusLabel1.Text = " List Box is being filled";
for (int i = 0; i <= 100000; i++)
{
listBox1.Items.Add(i.ToString());
Application.DoEvents();

}
toolStripStatusLabel1.Text = " List Box is completedly filled";
}

private void StartThreadWork()
{

if (this.listBox1.InvokeRequired)
{
UpdateListBox delegateToUpdateListBox = new UpdateListBox(ShowNumbers);
this.Invoke(delegateToUpdateListBox, null);
}
else
ShowNumbers();
}

private void Form1_Shown(object sender, EventArgs e)
{

StartThreadWork();


}

private void btnAddToListBox2_Click(object sender, EventArgs e)
{
listBox2.Items.Add( DateTime.Now.ToLongTimeString());
}


}
}

ching ching  Thursday, October 04, 2007 6:17 AM

hi,

you can refer here for multithreaded window control example

http://msdn2.microsoft.com/en-us/library/3s8xdz5c.aspx

a example for you, you will see thelistBox1 is still being appended after form shown. And, at the same time, users can still click on the button to add the timestamp to list box2 .

Hope it can help you. Smile

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace WindowsApplication2
{
public delegate void UpdateListBox();

public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();


}

private void ShowNumbers()
{
toolStripStatusLabel1.Text = " List Box is being filled";
for (int i = 0; i <= 100000; i++)
{
listBox1.Items.Add(i.ToString());
Application.DoEvents();

}
toolStripStatusLabel1.Text = " List Box is completedly filled";
}

private void StartThreadWork()
{

if (this.listBox1.InvokeRequired)
{
UpdateListBox delegateToUpdateListBox = new UpdateListBox(ShowNumbers);
this.Invoke(delegateToUpdateListBox, null);
}
else
ShowNumbers();
}

private void Form1_Shown(object sender, EventArgs e)
{

StartThreadWork();


}

private void btnAddToListBox2_Click(object sender, EventArgs e)
{
listBox2.Items.Add( DateTime.Now.ToLongTimeString());
}


}
}

ching ching  Thursday, October 04, 2007 6:17 AM

Hi,

Sorry for this delayedreply. I have tried this control it worked perfected. Thank you very much.

SunilKumar111  Saturday, October 06, 2007 7:08 AM

You can use google to search for other answers

Custom Search

More Threads

• New Laptop
• Docking components...what the heck?
• Displaying Flash with webbrowser control res protocol...doesn't work....why?
• Datagridview with Unbounded Checkbox Column Not Refresh C#
• Where are the events for a FolderBrowserDialog?
• LISTVIEW WATERMARK with .NET EXPRESS VB
• Maximizing application doesn't work properly
• Looking for a rich text editor sample app
• Shortcut keys problem with Datagrid
• Saving Resource to File