Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1901
  • Last Modified:

How to refresh WPF label by vb.net code?

i need to update a WPF label with lets say "Start Process" then loop some work and at the end update label with "Process complete." the problem is that the first "start Process" text never appear only the last text appear, WPF has no DOEvents code like VB FORMS.

i saw some C# code to do this with lambada delegates but i despretlly need VB.NET code that does this task.

Appretiate any help, Regards.
0
amir9383
Asked:
amir9383
1 Solution
 
CodeCruiserCommented:
I used the C# code from this site and converted it to VB.NET using and online code

http://dedjo.blogspot.com/2007/08/how-to-doevents-in-wpf.html

You can also still use the Application.DoEvents if you add the reference to the DLL. Check here
http://nmarian.blogspot.com/2007/09/doevents-in-wpf.html

Read this article as well
http://blogs.msdn.com/jfoscoding/archive/2005/08/06/448560.aspx

 
 
   1. Private Sub DoEvents()
   2.     Dim f As New DispatcherFrame()
   3.     Dispatcher.CurrentDispatcher.BeginInvoke(DispatcherPriority.Background, DirectCast(Function(ByVal arg As Object) Do
   4.         Dim fr As DispatcherFrame = TryCast(arg, DispatcherFrame)
   5.         fr.[Continue] = [True]
   6.     End Function, SendOrPostCallback), f)
   7.     Dispatcher.PushFrame(frame)
   8. End Sub

Open in new window

0
 
amir9383Author Commented:
hi, Thanks for the suggestions!

regard windows.forms.doevents() it does not achive the affect needed the application update the WPF label only after the loop processing (in my case some ftp connection check).

regard the converted code it has errors (for example the DO statement says "expression expected" error and the "arg is not declared.

I have googled up (a lot)  and find this solution in VB:

Imports System.Windows.Threading
Imports System.Threading

Public Sub DoEvents()

        Application.Current.Dispatcher.Invoke(DispatcherPriority.Background, New ThreadStart(AddressOf doNothing))

End Sub

Public Sub doNothing()

End Sub

i have added this to my custom control and call DoEvents() beofre my looping and the control has rendered properly, after my looping was over i have change the label again and the feedback to the user properly showned. (Strangely i had also to change the button event (the button that started the process) to "Release" mode. that refine the visual feedback even more.

hope that will help to others.

Regards,
0
 
petel2kCommented:
Application.Current.Dispatcher.Invoke(System.Windows.Threading.DispatcherPriority.Background, new System.Threading.ThreadStart(() => System.Threading.Thread.Sleep(1)));

Seems to solve the problem
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now