• 0

[VB .NET 2008] Application "not responding mode"


Question

Hello everyone,

I'm having a little headache right now. I have a vn .net 2008 program (.net 4) that does a lot of job in the background. When the user click on a button, the whole form disabled and another form is poped with a progress bar and status information. Then, in the main form, subs and function are ran which send update information to the progress form. Thing is, after a while, the UI thread freeze and the program enter "not responding mode". I've read about doing threads, I tried doing thread but what I want is the main form to stay disabled while the thread is running and the progressform to still be updated.

When using threads, I can't change another thread, it's a protection. So I checked on the web and there is a command to check if the code is ran from the right thread and then change thread if it's not the case and do the job. I've tried these but I get errors.

Then again, for the form to stay disabled, I ran a loop that check if the thread is ended. But then again, it lock up the program.

Thanks

4 answers to this question

Recommended Posts

  • 0

You have to use threading to accomplish your task. The reason the user sees the "not responding" notice is your UI thread is too busy to respond to Window's constant messages to ensure the application is still responding.

Try reading through something like this on threading in .NET.

I haven't read that one as I don't have time at the moment, but if it isn't sufficient it should at least get your feet wet and point you in the right direction.

  • 0

I'd use a background worker to do what you want to do personally. Then just have it bounce back progress to the progress bar, and until the doWorkComplete keep form1.enabled = false.

As for threading, the way I handle cross thread stuff is just invoke what I need done, I make subs that their only purpose is to invoke an update for a label.

ie)


public void UpdateStatusLabel(string msg)
{
try
{
this.Invoke(new MethodInvoker(delegate
{
//Update That label Yo!
lblStatus.Text = msg;
}));
}
catch
{
try
{
//Update That label Yo!
lblStatus.Text = msg;
}
catch (Exception e)
{
//Log what broke
clsMain.Debug.logData("Erroring lblStatus's Caption to: " + msg + ". Error: " + e.Message.ToString(), 1);
}
}
}
[/CODE]

Granted it's C#.. but it works.

  • 0

Ok, it's currently using thread this way:

button click

...

threadImport = New Thread(AddressOf import)

threadImport.SetApartmentState(ApartmentState.MTA)

Me.Enabled = False

threadImport.Start()

threadImport.Join()

Me.Enabled = True

end click

because theadImport isn't part of the same thread as the UI, I can't send the main form (me) the enabled=true command from the thread, it say crossthread operation. Because of that, I use thread.join() to wait for the thread to finish, thus creating a lock.

  • 0

First off, the easy and straight forward way to do this is to simply add the following code in between your hard working method:

Application.DoEvents()

Simply add the Application.DoEvents on the line after you update the progress bar and the method will make windows see that the program is responding.

This is very easy to implement, easy to understand, but also very very limited.

For most applications, you want to do the job (the import) in a separate thread like you have attempted at. There are lots of things you have to remember when working with threads however:

Like you said, threads cannot change UI, because UI elements are only allowed to change through the "UI" thread (the thread that runs when you click a button). To solve this, you have something called delegates which is basicly that your thread tells the UI to run some code next chance it has.

If one thread (like the button click function) has to wait for another thread (your threadImport) you write thread.Join() - this makes the thread lock up and you are back to square one again, UI becomes unresponsive. Unless you have a good reason, stay away from Join().

You have already understood how to run things in their own thread, so here's a link that shows you a bit more about how to update your UI on a separate thread (delegates and invokes and all that fancy stuff). What you need to do is move the Me.Enabled back into the thread using delegates, and remove the threadImport.Join() from your click function.

http://msdn.microsoft.com/en-us/library/zyzhdc6b.aspx

There's a very nice and readable example that should help you understand more about invoke and delegate.

This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.
  • Posts

    • WWDC 2025: Apple AirPods gain new remote control feature on iPhone and iPad by Aditya Tiwari Apple's annual developer event, WWDC 2025, is now up and running. Alongside the much-hyped Liquid Glass design for its software ecosystem, the Cupertino giant introduced new features for its wireless headphones. The AirPods Pro 2, AirPods 4, and AirPods 4 with Active Noise Cancellation (ANC) are being updated with studio-quality audio recording capabilities. Building on the Voice Isolation feature from last year, it allows users and content creators to record their content with greater sound quality. Apple has also improved voice call quality on these AirPods models to add "more natural vocal texture and clarity" when using FaceTime, CallKit-enabled apps, and regular phone calls. Meanwhile, the upgraded audio recording capabilities work across the Camera app, Voice Memos, dictation in Messages, video conferencing apps like Webex, and compatible third-party camera apps. You have already been able to use your Apple Watch as a wireless remote for your iPhone's camera. This capability is now available on AirPods Pro 2, AirPods 4, and AirPods 4 ANC. You can press and hold the stem of your AirPods to snap a photo or start and stop video recording. Apple AirPods already support head gestures when responding to the Siri voice assistant. You can nod your head to say yes or shake it horizontally to say no when responding to the virtual assistant. Now, you can also use your AirPods to capture content from a distance. "For users who like capturing themselves singing or dancing, the new features make it easy to perform in sync with a soundtrack while simultaneously recording the video," Apple said. The camera remote feature on AirPods works with the Camera app or supported third-party camera apps on your iPhone or iPad. These new AirPods features are now available for testing through the Apple Developer Program and will be offered through public beta next month. Their public rollout will happen later this year through a firmware update shipped alongside iOS 26, iPadOS 26, and macOS Tahoe 26.
    • Anyone remembers times when Apple already had everything glassy looking? I do and I didn't even use Apple products back then...
    • This app has become so bloated. I just want to see my money, send and receive it. Not offers and recommendations.
    • >it will be hiring 1,250 operators, technicians, and engineers to help manage the facilities Um, that's chump change employment numbers for any sector. Any word on what the cost will be to PA for "securing" this investment?! How much do the PA taxpayers have to eat?
  • Recent Achievements

    • Rookie
      CHUNWEI went up a rank
      Rookie
    • Enthusiast
      the420kid went up a rank
      Enthusiast
    • Conversation Starter
      NeoToad777 earned a badge
      Conversation Starter
    • Week One Done
      VicByrd earned a badge
      Week One Done
    • Reacting Well
      NeoToad777 earned a badge
      Reacting Well
  • Popular Contributors

    1. 1
      +primortal
      480
    2. 2
      +FloatingFatMan
      281
    3. 3
      ATLien_0
      255
    4. 4
      Edouard
      204
    5. 5
      snowy owl
      201
  • Tell a friend

    Love Neowin? Tell a friend!