473,320 Members | 1,951 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

Problem with Threading in Win Forms

Hello to everybody

I'm new in win apllications and C#. I have a problem with Threading.
Here is my problem:
I have a form with button named: BupisiStudente, when I click on it a
call next method:
private void BUpisiStudente_Click(object sender, EventArgs e)
{
......
//calling method from object "mojaObrada"
if
(mojaObrada.UpisiStudentaZaIspit(IDIspita,idStuden ta,ImeProfesoraUFormi,PasswordProfesora))
.....

In Obrada class (object mojaObrada) "UpisiStudentaZaIspit" method:
public bool UpisiStudentaZaIspit(int ID_Ispit, int ID_Student, string
username, string pass)
{

...
//creating thread and starting it
Thread workerThread = new Thread(new
ThreadStart(ShowSacekajte));
workerThread.Start();

//calling service
if (mojServis.UpisiStudentaZaIspit(ID_Ispit, ID_Student,
Encrypt(sifra + IDIspitaString), username, pass))
{
//closing form "Molim"
Molim.Close();

//aborting thread
workerThread.Abort();
return true;
}
else
{

//closing form "Molim"
Molim.Close();

//aborting thread
workerThread.Abort();
return true;
}
//Method ShowSacekajte
public void ShowSacekajte()
{
Molim.ShowDialog();
}
Everithing works fine, but in 1000 click or 500 nevermind, i get
exception for thread. Can enyone help mi how to "get over" this
problem.

Thanx in advance.

PS. I'm working in VS2005

May 26 '06 #1
3 1587
hi

you cannot interact with UI elements from the worker thread, you have to use
Control.Invoke , see either MSDN or search in the archives for
"Control.Invoke"
--
--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation

<bi*****@gmail.com> wrote in message
news:11**********************@g10g2000cwb.googlegr oups.com...
Hello to everybody

I'm new in win apllications and C#. I have a problem with Threading.
Here is my problem:
I have a form with button named: BupisiStudente, when I click on it a
call next method:
private void BUpisiStudente_Click(object sender, EventArgs e)
{
.....
//calling method from object "mojaObrada"
if
(mojaObrada.UpisiStudentaZaIspit(IDIspita,idStuden ta,ImeProfesoraUFormi,PasswordProfesora))
.....

In Obrada class (object mojaObrada) "UpisiStudentaZaIspit" method:
public bool UpisiStudentaZaIspit(int ID_Ispit, int ID_Student, string
username, string pass)
{

...
//creating thread and starting it
Thread workerThread = new Thread(new
ThreadStart(ShowSacekajte));
workerThread.Start();

//calling service
if (mojServis.UpisiStudentaZaIspit(ID_Ispit, ID_Student,
Encrypt(sifra + IDIspitaString), username, pass))
{
//closing form "Molim"
Molim.Close();

//aborting thread
workerThread.Abort();
return true;
}
else
{

//closing form "Molim"
Molim.Close();

//aborting thread
workerThread.Abort();
return true;
}
//Method ShowSacekajte
public void ShowSacekajte()
{
Molim.ShowDialog();
}
Everithing works fine, but in 1000 click or 500 nevermind, i get
exception for thread. Can enyone help mi how to "get over" this
problem.

Thanx in advance.

PS. I'm working in VS2005

May 26 '06 #2
That is most likely _A_ issue but I don't think it is _THE_ issue.

//creating thread and starting it
Thread workerThread = new Thread(new
ThreadStart(ShowSacekajte));
workerThread.Start();
//calling service
if (mojServis.UpisiStudentaZaIspit(ID_Ispit, ID_Student,
Encrypt(sifra + IDIspitaString), username, pass))
{
//closing form "Molim"
Molim.Close();
//aborting thread
workerThread.Abort();
return true;
}
else
{
//closing form "Molim"
Molim.Close();
//aborting thread
workerThread.Abort();
return true;
}

There are alot of funny race conditions in this code.

if (mojServis.UpisiStudentaZaIspit(ID_Ispit, ID_Student,
Encrypt(sifra + IDIspitaString), username, pass))

Molim.Close();
What happens if the window hasn't been opened yet?
Also you should never be calling Thread.Abort() if you can avoid it ... You
should instead be using Thread.Join() you will then know that the thread has
completed its operation. Given your code here I would probably put the
Join() right after you call the service .. but this still has the issue of
the control and the other thread ... Given that you should probably contact
the service on a secondary thread and create the window on the main thread
while that is occurring.

Cheers,

Greg Young
MVP - C#
"Ignacio Machin ( .NET/ C# MVP )" <ignacio.machin AT dot.state.fl.us> wrote
in message news:eO**************@TK2MSFTNGP02.phx.gbl...
hi

you cannot interact with UI elements from the worker thread, you have to
use Control.Invoke , see either MSDN or search in the archives for
"Control.Invoke"
--
--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation

<bi*****@gmail.com> wrote in message
news:11**********************@g10g2000cwb.googlegr oups.com...
Hello to everybody

I'm new in win apllications and C#. I have a problem with Threading.
Here is my problem:
I have a form with button named: BupisiStudente, when I click on it a
call next method:
private void BUpisiStudente_Click(object sender, EventArgs e)
{
.....
//calling method from object "mojaObrada"
if
(mojaObrada.UpisiStudentaZaIspit(IDIspita,idStuden ta,ImeProfesoraUFormi,PasswordProfesora))
.....

In Obrada class (object mojaObrada) "UpisiStudentaZaIspit" method:
public bool UpisiStudentaZaIspit(int ID_Ispit, int ID_Student, string
username, string pass)
{

...
//creating thread and starting it
Thread workerThread = new Thread(new
ThreadStart(ShowSacekajte));
workerThread.Start();

//calling service
if (mojServis.UpisiStudentaZaIspit(ID_Ispit, ID_Student,
Encrypt(sifra + IDIspitaString), username, pass))
{
//closing form "Molim"
Molim.Close();

//aborting thread
workerThread.Abort();
return true;
}
else
{

//closing form "Molim"
Molim.Close();

//aborting thread
workerThread.Abort();
return true;
}
//Method ShowSacekajte
public void ShowSacekajte()
{
Molim.ShowDialog();
}
Everithing works fine, but in 1000 click or 500 nevermind, i get
exception for thread. Can enyone help mi how to "get over" this
problem.

Thanx in advance.

PS. I'm working in VS2005


May 26 '06 #3
Hi,

if (mojServis.UpisiStudentaZaIspit(ID_Ispit, ID_Student,
Encrypt(sifra + IDIspitaString), username, pass))

Molim.Close();
What happens if the window hasn't been opened yet?


I think that the possible problem is when the form has not initialized
correctly ( InitializeComponent() is not called yet).
This can only happen if the thread is called in the constructor.
If called after InitializeComponent it should work regardless if the form is
already displayed or not.

--
--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation
May 30 '06 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
by: Stephen Rice | last post by:
Hi, I have a periodic problem which I am having a real time trying to sort. Background: An MDI VB app with a DB on SQL 2000. I have wrapped all the DB access into an object which spawns a...
2
by: Stephan Zaubzer | last post by:
Hi all, I encountered a problem with NumericUpDown yesterday and managed to reproduce the error with a very easy examle. I have a Windows Application with only one Form, which contains only one...
9
by: esakal | last post by:
Hello, I'm programming an application based on CAB infrastructure in the client side (c# .net 2005) Since my application must be sequencally, i wrote all the code in the UI thread. my...
8
by: Gary | last post by:
I'm using an Act database. I was stuck on this a year ago and am still having trouble. I have three bits of code like so : - act.CActAppObj objACT = new act.CActAppObj(); act.CAIBaseView...
1
by: explode | last post by:
I made a oledbdataadapter with this code Public Sub Novo(ByVal nova1 As String, ByVal nova2 As String) Dim i As Integer Dim nova As OleDb.OleDbDataAdapter = New OleDb.OleDbDataAdapter Dim veza...
2
by: explode | last post by:
I made nova oledbdataadapter select update insert and delete command and connection veza. dataset is Studenti1data, I made it by the new data source wizard,and made datagridview and bindingsource...
0
by: =?Utf-8?B?RGF2ZQ==?= | last post by:
I am unable to execute the following statement in my current project. The class PosExplorer is in a dll file. PosExplorer explorer = new PosExplorer() but now I get this dreaded exception...
7
by: Mike P | last post by:
I am trying to write my first program using threading..basically I am moving messages from an Outlook inbox and want to show the user where the process is up to without having to wait until it has...
5
by: Martinez | last post by:
Hello I try declare Dim db as New BtDatabase() and execute metod db.open(par1,par2,par3) but i get error : System.NullReferenceException was unhandled
6
by: Scott Gravenhorst | last post by:
Windows XP SP3 My application is set to open a SaveFile dialog when an exit is requested. When I click the app's close button, the save dialog opens, but when I click to change the folder, the...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.