use thread for progressbar

use thread for progressbar

  • (2 Pages)
  • +
  • 1
  • 2

19 Replies - 18486 Views - Last Post: 01 September 2010 - 12:16 AM Rate Topic: -----

#1 b.mahsa  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 29
  • Joined: 04-August 09

use thread for progressbar

Posted 28 August 2010 - 10:49 AM

Hi every body .
I have a process (adapter.fill) it takes much time . to execut
I want to use thread for progress bar to move.
I use code like below
thread = New Threading.Thread(AddressOf Me.showform)
thread.SetApartmentState(Threading.ApartmentState.STA)
thread.Start()


showform is a form that contains progress bar and in its form load progress bar is move .

but it has a error and it dosent work correctly.
is there another way to use progress bar with thread????

Is This A Good Question/Topic? 0
  • +

Replies To: use thread for progressbar

#2 Core  Icon User is offline

  • using System.Linq;
  • member icon

Reputation: 774
  • View blog
  • Posts: 5,097
  • Joined: 08-December 08

Re: use thread for progressbar

Posted 28 August 2010 - 11:01 AM

What exactly is the error you are getting? From what you're saying, I can think that you are trying to access a UI component (running in the UI thread) from a secondary thread. You cannot directly perform cross-thread communication like this, so you could have a property, that is being set by the thread, and at the same time, the progress bar will be getting its value from that specific property.
Was This Post Helpful? 0
  • +
  • -

#3 b.mahsa  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 29
  • Joined: 04-August 09

Re: use thread for progressbar

Posted 28 August 2010 - 11:09 AM

thank you for reply
no there is no join in threads values...
my thread is just for progress the progressbar.
for users to know they should wait.

imagine it like 2 forms . 1 is like a parent of the other.(thread_form)
while another form is executing adapter .fil.
the thread_form opens (top most) and show the progressbar prosses.
this is the way i use. and i think its not currect.
what is your suggestion ? for using progress bar until another execution(like adapter.fill) is doing?

This post has been edited by b.mahsa: 28 August 2010 - 11:13 AM

Was This Post Helpful? 0
  • +
  • -

#4 Core  Icon User is offline

  • using System.Linq;
  • member icon

Reputation: 774
  • View blog
  • Posts: 5,097
  • Joined: 08-December 08

Re: use thread for progressbar

Posted 28 August 2010 - 11:29 AM

Could you please show the code?

You don't have to join the threads to cause an exception. If you are making direct cross-thread calls between the UI thread and any other thread, that is an illegal operation and it is not allowed.
Was This Post Helpful? 0
  • +
  • -

#5 _HAWK_  Icon User is online

  • Master(Of Foo)
  • member icon

Reputation: 1066
  • View blog
  • Posts: 4,173
  • Joined: 02-July 08

Re: use thread for progressbar

Posted 28 August 2010 - 07:38 PM

Why do you need a second form to do the adapter.fill? How about running your code in a Sub that is on the other thread then we just need to create a delegate to update the progress bar.
Was This Post Helpful? 0
  • +
  • -

#6 Guest_b.mahsa*


Reputation:

Re: use thread for progressbar

Posted 29 August 2010 - 02:45 AM

Could you please show the code?

dear Core I put my code in first post.

and thanks hawkvalley1 for reply.
I have a form in witch adapter.fill is execute.
it takes much time to execute. and i need to use a progressbar . to show user should waiting.
when adapter.fill is executing nothing else would be execute.
when I use for cycle in the cycle i can progress the progressbar but in adapter.fill I cant do any thing.
so I use another form in its form_load i progress the progress bar.

here is my second forms code:
Private Sub Form_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
 RadWaitingBar1.StartWaiting()
 End Sub


and I call the form.show in a thread. to show progress bar process
(while my firs form executes the adapter.fill).

Am I wrong ?
Is there another way?
Was This Post Helpful? 0

#7 b.mahsa  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 29
  • Joined: 04-August 09

Re: use thread for progressbar

Posted 29 August 2010 - 02:51 AM

Could you please show the code?

dear Core I put my code in first post.

and thanks hawkvalley1 for reply.
I have a form in witch adapter.fill is execute.
it takes much time to execute. and i need to use a progressbar . to show user should waiting.
when adapter.fill is executing nothing else would be execute.
when I use for cycle in the cycle i can progress the progressbar but in adapter.fill I cant do any thing.
so I use another form in its form_load i progress the progress bar.

here is my second forms code:
Private Sub Form_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
 RadWaitingBar1.StartWaiting()
 End Sub


and I call the form.show in a thread. to show progress bar process
(while my firs form executes the adapter.fill).

Am I wrong ?
Is there another way?
Was This Post Helpful? 0
  • +
  • -

#8 _HAWK_  Icon User is online

  • Master(Of Foo)
  • member icon

Reputation: 1066
  • View blog
  • Posts: 4,173
  • Joined: 02-July 08

Re: use thread for progressbar

Posted 29 August 2010 - 03:01 AM

I don't think you need the second form. Here is an example of using the BackGroundWorker, which I think will fit your requirements.

This post has been edited by hawkvalley1: 29 August 2010 - 03:01 AM

Was This Post Helpful? 0
  • +
  • -

#9 b.mahsa  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 29
  • Joined: 04-August 09

Re: use thread for progressbar

Posted 29 August 2010 - 09:52 AM

Dear Hawkvalley thanks. but the link dosent work.

As I know using backgroundworker allow my user to do any thing else while the background worker execute and all the buttons are enable.(Am I wrong?)
I want to freeze my form while the (adapter.fill) is executing.
users cant do any thing until execution ends.

This post has been edited by b.mahsa: 29 August 2010 - 09:53 AM

Was This Post Helpful? 0
  • +
  • -

#10 _HAWK_  Icon User is online

  • Master(Of Foo)
  • member icon

Reputation: 1066
  • View blog
  • Posts: 4,173
  • Joined: 02-July 08

Re: use thread for progressbar

Posted 29 August 2010 - 10:09 AM

You can set the form's Enabled property to False when you start the process then set it back to True when done. Maybe show a splash screen while it is working this will look good. You can also Hide the form too.

This post has been edited by hawkvalley1: 29 August 2010 - 10:10 AM

Was This Post Helpful? 1
  • +
  • -

#11 b.mahsa  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 29
  • Joined: 04-August 09

Re: use thread for progressbar

Posted 29 August 2010 - 10:18 AM

Thanks hawkvalley.
disabling form . make its color gray and its not seems to be good.
I think thread is more beautiful.

This post has been edited by b.mahsa: 29 August 2010 - 10:20 AM

Was This Post Helpful? 0
  • +
  • -

#12 _HAWK_  Icon User is online

  • Master(Of Foo)
  • member icon

Reputation: 1066
  • View blog
  • Posts: 4,173
  • Joined: 02-July 08

Re: use thread for progressbar

Posted 29 August 2010 - 10:33 AM

I guess I am having a hard time figuring out what you are needing. I agree with you, it looked like that was what you were asking for to block the user form using the form - which that would do it.

BackGroundWorker here on DIC
Was This Post Helpful? 0
  • +
  • -

#13 Core  Icon User is offline

  • using System.Linq;
  • member icon

Reputation: 774
  • View blog
  • Posts: 5,097
  • Joined: 08-December 08

Re: use thread for progressbar

Posted 29 August 2010 - 10:44 AM

The code you posted is not really enough to see the source of the problem. You are most likely making a cross-thread call. Be it via BackgroundWorker, or Thread - doesn't matter. Direct cross-thread calls to a UI thread are not allowed.

This means, that you need to create a property, that will hold the value. When a secondary thread is executed, the property is updated. The ProgressBar control should be taking its value from the property and not through another thread.
Was This Post Helpful? 0
  • +
  • -

#14 b.mahsa  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 29
  • Joined: 04-August 09

Re: use thread for progressbar

Posted 30 August 2010 - 10:10 AM

My first form's code is this
thread = New Threading.Thread(AddressOf Me.showform)
thread.SetApartmentState(Threading.ApartmentState.STA)
thread.Start()
adapter.fill(ds.tables(0))
thread.abort()


And my second form's code is like bellow
Private Sub Form_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
 RadWaitingBar1.StartWaiting()
 End Sub


This post has been edited by b.mahsa: 30 August 2010 - 10:11 AM

Was This Post Helpful? 0
  • +
  • -

#15 _HAWK_  Icon User is online

  • Master(Of Foo)
  • member icon

Reputation: 1066
  • View blog
  • Posts: 4,173
  • Joined: 02-July 08

Re: use thread for progressbar

Posted 30 August 2010 - 10:34 AM

You are not filling the adapter in the Sub that your thread is calling - thats why your app freezes. When you call Start on a thread you are calling the Sub you reference with the AddressOf pointer. What is showForm anyway - it should be a Sub not a form? If you tried to use the BGW like the example shows your problems would be done. The BGW example shows how to run a long process and update a progress bar while doing it.

This post has been edited by hawkvalley1: 30 August 2010 - 10:35 AM

Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2