473,320 Members | 1,612 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.

multithreading issue

Sam
Hi,
I've written the following, inspired by a MSDN article.
First I want to know if what I do is correct. The reason I'm asking is
because if the value passed to CalcPi is great(eg.10000) then I can't
interact with the UI anymore (resize window, move window...)
Second, I don't know how to close Form2. If I do frm.Close in CalcPi
after the loop, I get the exception:

Cross-thread operation not valid: Control 'Form2' accessed from a
thread other than the thread it was created on.

Quick overview of the app:
I create a delegate to open a new form when I click on a button
The new form creates a delegate of CalcPi and update a progressbar
CalcPi does a loop from 0 to X and display "test" each time in a
textbox.

Can you help?
Thanks

CODE:

Public Class Form1

Dim frm As Form2
Dim sf As ShowFormDelegate

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click

sf = New ShowFormDelegate(AddressOf ShowForm)
sf.BeginInvoke(Nothing, Nothing)
End Sub

Public Delegate Sub CalcPiDelegate(ByVal digits As Integer)

Public Sub CalcPi(ByVal digits As Integer)
Dim pi As StringBuilder = New StringBuilder)

ShowProgress(pi.ToString, digits, 0)

If (digits > 0) Then
pi.Append(".")

For i As Integer = 0 To digits
pi.Append("test")
ShowProgress(pi.ToString, digits, i)
Next
'RAISED EXCEPTION HERE
frm.Close()
End If
End Sub

Delegate Sub ShowProgressDelegate(ByVal pi As String, ByVal
totalDigits As Integer, ByVal digitSoFar As Integer)

Public Sub ShowProgress(ByVal pi As String, ByVal totalDigits As
Integer, ByVal digitSoFar As Integer)
If Not piText.InvokeRequired Then
piText.Text = pi
frm.ProgressBar2.Maximum = totalDigits
frm.ProgressBar2.Value = digitSoFar
Else
Dim sp As New ShowProgressDelegate(AddressOf ShowProgress)
Dim ar As New ArrayList
ar.Add(pi.ToString)
ar.Add(totalDigits)
ar.Add(digitSoFar)
BeginInvoke(sp, ar.ToArray)
End If
End Sub

Delegate Sub ShowFormDelegate()

Public Sub ShowForm()
If Not InvokeRequired Then
frm = New Form2(Me)
frm.ShowDialog()
Else
Dim sf As New ShowFormDelegate(AddressOf ShowForm)
BeginInvoke(sf)
End If
End Sub

End Class

---------------------------------------------------------------

Public Class Form2

Private Sub Form2_Load(ByVal sender As Object, ByVal e As
System.EventArgs) Handles Me.Load

Dim cp As New Form1.CalcPiDelegate(AddressOf Form1.CalcPi)
cp.BeginInvoke(1000, Nothing, Nothing)

End Sub

End Class

Nov 21 '05 #1
7 1672
"Sam" <sa**************@voila.fr> schrieb:
I've written the following, inspired by a MSDN article.
First I want to know if what I do is correct. The reason I'm asking is
because if the value passed to CalcPi is great(eg.10000) then I can't
interact with the UI anymore (resize window, move window...)
Second, I don't know how to close Form2. If I do frm.Close in CalcPi
after the loop, I get the exception:

Cross-thread operation not valid: Control 'Form2' accessed from a
thread other than the thread it was created on.


I didn't take a look at the source code you posted, but you may find the
articles referenced in the Web page below useful:

Multithreading in Windows Forms applications
<URL:http://dotnet.mvps.org/dotnet/faqs/?id=multithreading&lang=en>

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://classicvb.org/petition/>

Nov 21 '05 #2
Sam
Herfried,
thanks for the link
I've already look at most of those article (my app is actually inspired
by one of them).
But I still don't understand my problem

Nov 21 '05 #3
How are you referencing the Form1.CalcPi method in Form2?
--------------------------------------------------------------------------------
All that glitters has a high refractive index.
www.mendhak.com
"Sam" <sa**************@voila.fr> wrote in message
news:11**********************@z14g2000cwz.googlegr oups.com...
Herfried,
thanks for the link
I've already look at most of those article (my app is actually inspired
by one of them).
But I still don't understand my problem

Nov 21 '05 #4
Sam
See code above.

i give it again:

Dim cp As New Form1.CalcPiDelegate(AddressOf Form1.CalcPi)
cp.BeginInvoke(1000, Nothing, Nothing)

Nov 21 '05 #5
Sam
Ok, I've changed the way I do it using proper multithreading of the
System.Threading Class.

Now I'm facing another issue. To pass parameter to my function executed
by the thread, I've encapsulated it into a class and the parameter
correspond to member variable. To return a value I use a delegate and I
raise an event to update the progressbar:
Public Sub Calcul()
Dim i As Integer
For i = 0 To Me._digits
While Me.manualEvent.WaitOne(100, False)
End While
RaiseEvent InProgress(Me, New InProgressEventArgs(i))
Next
Me.manualEvent.Close()
RaiseEvent Complete()
End Sub

The issue is that I don't want to encapsulate all of the methods in my
project that are candidate to be executed by a thread ! That would be
silly considering the number of functions!
So how can I do a generic class? I can't pass the address of the
function to be executed to Calcul because of the code in the For loop.

Nov 21 '05 #6
Den
Sam,

I have recently started with the problem of multiple threads and forms, and
I see that you are way, way ahead. If I may be permitted some questions, have
you seen the "Task Class" in:

David Hill's WebLog:
http://blogs.msdn.com/dphill/articles/183503.aspx

which was apparently derived from Microsoft patterns & practices:
http://msdn.microsoft.com/library/de.../scag-ch06.asp

If so, have you found it useful, or have you taken another approach?

In your first post in this thread, you closed a form from (I think) another
thread:

'RAISED EXCEPTION HERE
frm.Close()

How do you do this now?

Thank you kindly.

-Den

"Sam" wrote:
Ok, I've changed the way I do it using proper multithreading of the
System.Threading Class.

Now I'm facing another issue. [snip] The issue is that I don't want to encapsulate all of the methods in my
project that are candidate to be executed by a thread ! That would be
silly considering the number of functions!
So how can I do a generic class? I can't pass the address of the
function to be executed to Calcul because of the code in the For loop.


Nov 23 '05 #7
Sam
Hi Den
Sorry for late reply. No I never looked at this link before.
My multithreading is working very well now so I don't think I'm going
to change anything to it :)

Regarding the exception, basically it was raised because I was trying
to access a control (form) which had not been created by the thread I
was trying to access it from. This issue should not occur in VS2003,
but in VS2005 it does as it is more sensitive. To overcome this
problem, I use BeginInvoke and I pass it a delegate of the method I
want to access.

Hope that helps.

Sam

Nov 23 '05 #8

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

Similar topics

47
by: mihai | last post by:
What does the standard say about those two? Is any assurance that the use of STL is thread safe? Have a nice day, Mihai.
11
by: Mark Yudkin | last post by:
The documentation is unclear (at least to me) on the permissibility of accessing DB2 (8.1.5) concurrently on and from Windows 2000 / XP / 2003, with separate transactions scope, from separate...
16
by: Robert Zurer | last post by:
Can anyone suggest the best book or part of a book on this subject. I'm looking for an in-depth treatment with examples in C# TIA Robert Zurer robert@zurer.com
5
by: MS Newsgroups | last post by:
Hi, I have an application that runs a timer and executes a method in a class as a new thread every 60 seconds, the thread takes 65 seconds to execute so there is a 5 second overlap when 2...
2
by: Rich | last post by:
Hello, I have set up a multithreading routine in a Test VB.net proj, and it appears to be working OK in debug mode and I am not using synchronization. Multithreading is a new thing for me, and...
55
by: Sam | last post by:
Hi, I have a serious issue using multithreading. A sample application showing my issue can be downloaded here: http://graphicsxp.free.fr/WindowsApplication11.zip The problem is that I need to...
6
by: David Veeneman | last post by:
I am looking for a resource to learn how to do multithreading in a data bound app. So far, I have figured out that I can't update a databound object on a worker thread, because that triggers a...
9
by: facugaich | last post by:
Hi, I have some questions regarding a multithreaded server I'm working on. Since the main function (the primary thread) and the handle_client function (called by all the other threads) use the...
1
by: krsk4u | last post by:
Hi all, Am new to Multithreading. I need a help from you. I want to write a WindowsService in VB.Net, which will ping all the systems in the network using its IP Address which is stored in a...
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
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
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: 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)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
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...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

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.