Click here to Skip to main content
15,923,273 members
Home / Discussions / Visual Basic
   

Visual Basic

 
AnswerRe: Disable some hot keys Pin
Zaegra18-Apr-09 5:36
Zaegra18-Apr-09 5:36 
AnswerRe: Disable some hot keys Pin
Eddy Vluggen18-Apr-09 6:57
professionalEddy Vluggen18-Apr-09 6:57 
AnswerRe: Disable some hot keys Pin
Mycroft Holmes18-Apr-09 17:22
professionalMycroft Holmes18-Apr-09 17:22 
GeneralRe: Disable some hot keys Pin
AndyASPVB18-Apr-09 22:41
AndyASPVB18-Apr-09 22:41 
QuestionRefreshing the UI during a long process using VB.NET 3.5 without Application.DoEvents - believe me Ive tried! Pin
Marcus J. Smith18-Apr-09 4:14
professionalMarcus J. Smith18-Apr-09 4:14 
QuestionRe: Refreshing the UI during a long process using VB.NET 3.5 without Application.DoEvents - believe me Ive tried! Pin
Eddy Vluggen18-Apr-09 6:51
professionalEddy Vluggen18-Apr-09 6:51 
AnswerRe: Refreshing the UI during a long process using VB.NET 3.5 without Application.DoEvents - believe me Ive tried! Pin
Luc Pattyn18-Apr-09 7:15
sitebuilderLuc Pattyn18-Apr-09 7:15 
AnswerRe: Refreshing the UI during a long process using VB.NET 3.5 without Application.DoEvents - believe me Ive tried! Pin
Luc Pattyn18-Apr-09 7:22
sitebuilderLuc Pattyn18-Apr-09 7:22 
Hi again,

I'm adding a standard text of mine about cross-thread access to GUI Controls, since you will probably need such info anyway:

Controls are not thread-safe, hence they should be touched (that is: their methods or properties called) only by the thread that created them, which normally is the main thread (aka GUI thread). Creating some controls on a different thread is unlikely to be successful, since all Controls get linked somehow: they reside on Forms, Forms are related to each other (by Parent, by Z-Order, etc), so normally all are created on a single thread.

If you violate the “don’t touch Controls from another thread” rule and are running .NET version 2.0 or above you will get an InvalidOperationException (“Cross-thread operation not valid”), which should be remedied by changing the code.

Do not set Control.CheckForIllegalCrossThreadCalls false, since that does hide the exception but does not cure the fundamental flaw in your code, so it just postpones the moment of failure, which typically will show as a non-responsive and possibly badly painted GUI.

Here are some ways to get another thread:
- explicitly launching a Thread instance
- explicitly delegating some work to a ThreadPool thread
- using a BackgroundWorker; a BGW is a separate thread with the advantage that two of its events (ProgressChanged and RunWorkerCompleted) execute on the GUI thread; however the bulk of the work normally is handled in the DoWork handler which runs on a distinct thread.
- using timers other than System.Windows.Forms.Timer; the Forms timer ticks on the GUI thread, all other use different threads to handle the periodic event;
- using asynchronous input/output, such as the DataReceived event of the SerialPort class

Any of these touching a single method or property of a Control is sufficient to create havoc; there are 5 exceptions:
- the InvokeRequired property
- the Invoke, BeginInvoke, EndInvoke and CreateGraphics methods (the latter only if the handle for the control has already been created).

If there is a need to touch the Control from another thread, one must use an Invoke pattern, which basically looks like this (using C# code, same can be done in VB.NET):

public void SetText(string text) {
    if (myControl.InvokeRequired) {
        // this runs on the foreign thread and causes the
        // invocation of this same method on the GUI thread
        myControl.Invoke(new Action< string >(SetText),
            new object[] {text});
    } else {
        // this runs on the GUI thread only
        myControl.Text=text;
    }
}


PS: if you follow my earlier advice you should not need Application.DoEvents() at all.

Smile | :)

Luc Pattyn [Forum Guidelines] [My Articles]

Avoiding unwanted divs (as in "articles needing approval") with the help of this FireFox add-in


QuestionMouse-event Improvement Pin
User 584223717-Apr-09 23:33
User 584223717-Apr-09 23:33 
AnswerRe: Mouse-event Improvement Pin
Eddy Vluggen18-Apr-09 0:24
professionalEddy Vluggen18-Apr-09 0:24 
GeneralRe: Mouse-event Improvement Pin
User 584223718-Apr-09 1:24
User 584223718-Apr-09 1:24 
GeneralRe: Mouse-event Improvement Pin
Eddy Vluggen18-Apr-09 1:33
professionalEddy Vluggen18-Apr-09 1:33 
GeneralRe: Mouse-event Improvement Pin
User 584223718-Apr-09 5:03
User 584223718-Apr-09 5:03 
GeneralRe: Mouse-event Improvement Pin
Eddy Vluggen18-Apr-09 5:52
professionalEddy Vluggen18-Apr-09 5:52 
GeneralRe: Mouse-event Improvement Pin
User 584223718-Apr-09 6:22
User 584223718-Apr-09 6:22 
GeneralRe: Mouse-event Improvement Pin
Eddy Vluggen18-Apr-09 6:50
professionalEddy Vluggen18-Apr-09 6:50 
GeneralRe: Mouse-event Improvement Pin
Mycroft Holmes18-Apr-09 2:03
professionalMycroft Holmes18-Apr-09 2:03 
AnswerRe: Mouse-event Improvement [modified] Pin
Johan Hakkesteegt20-Apr-09 2:39
Johan Hakkesteegt20-Apr-09 2:39 
GeneralRe: Mouse-event Improvement Pin
User 584223725-Apr-09 5:45
User 584223725-Apr-09 5:45 
QuestionHow can i create salary format in Textbox ? for example i want create 2000.00 . Pin
pramod251717-Apr-09 19:55
pramod251717-Apr-09 19:55 
AnswerRe: How can i create salary format in Textbox ? for example i want create 2000.00 . Pin
Rajesh Anuhya17-Apr-09 20:08
professionalRajesh Anuhya17-Apr-09 20:08 
AnswerRe: How can i create salary format in Textbox ? for example i want create 2000.00 . Pin
Andy_L_J17-Apr-09 22:16
Andy_L_J17-Apr-09 22:16 
AnswerRe: How can i create salary format in Textbox ? for example i want create 2000.00 . Pin
Johan Hakkesteegt20-Apr-09 1:41
Johan Hakkesteegt20-Apr-09 1:41 
QuestionError in using MemoryStream in VB.Net Pin
Viram Pandey17-Apr-09 3:57
Viram Pandey17-Apr-09 3:57 
AnswerRe: Error in using MemoryStream in VB.Net Pin
Dave Kreskowiak17-Apr-09 4:26
mveDave Kreskowiak17-Apr-09 4:26 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.