By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,320 Members | 2,224 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,320 IT Pros & Developers. It's quick & easy.

Problem with Threading in Win Forms

P: n/a
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
Share this Question
Share on Google+
3 Replies


P: n/a
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

P: n/a
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

P: n/a
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 discussion thread is closed

Replies have been disabled for this discussion.