Problem with Threading in Win Forms

B

bilosta

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,idStudenta,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
 
I

Ignacio Machin \( .NET/ C# MVP \)

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"
 
G

Greg Young

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#
 
I

Ignacio Machin \( .NET/ C# MVP \)

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top