Thursday, July 26, 2012

How to update text box from MFC Worker Thread



I had to write an MFC application in which I was using a second thread to perform some time consuming process. The main UI thread was waiting for the completion of that event by calling ::WaitForSingleObject(). During execution, I always noticed that the cursor keeps circling and the UI does not respond. How can this be avoided?

1. Create an MFC Worker Thread to handle a synchronous/blocking call.

CWinThread *thread = AfxBeginThread(DelegateThread, this, THREAD_PRIORITY_NORMAL, 0, 0, NULL);

2. In DelegateThread(), invoke the blocking call. After this, lets say you want to update a text box from the result of the blocking call. This can be done in the following way. Define the following function in main Cwnd (i.e., main UI thread)

MainCwnd::UpdateTextBox(BSTR bstr)
{
    CEdit *pEdit = (CEdit *)GetDlgItem(IDC_EDIT);
    pEdit->SetWindowTextW(bstr);
}

3. Make the following call from DelegateThread()

BSTR test = SysAllocString(L"Test String");
MainCwnd->UpdateTextBox(test);
SysFreeString(test);

If all is right, this will update your text box! Also, this will avoid calling ::WaitForSingleObject() which gives a better UI feel. However, this is useful only when you need to display the result got from calling the blocking call in DelegateThread(). If there is some extra processing to be done after invoking the blocking call (eg:creating another thread that needs this result), then I cannot guarantee that this method will work.

Note: It is VERY important to note that you cannot make the call to edit the text box from the worker thread. However, you can INVOKE the function to edit the text box from the worker thread. This function should belong to the main dialog box class.

Saturday, April 12, 2008

Have Fun

If you are a beginner and just want to admire netbeans, I suggest you to have an overview of all the sample projects present in netbeans. Trust me,you are going to completely enjoy it. Moreover,this is the perfect way to get a trip of the platform. The one which I enjoyed most was the paint application. To view this, click on files->new project. In categories click on samples->Netbeans modules. Here, choose the paint app and click on finish.This is set as the main project. Build this project by clicking F11 and to run the program,click on F6. Then during run time,the first picture that pops up is as shown below.








After this,you can paint on the screen as shown below. To view the white sheet,click on file-> new canvas. Finally,you can play around with colours and have fun!








Similarly as shown,you can download many more loaded sample programs and have fun with them. In C applications,you have two loaded programs.
i) sample C application which prints the arguments.
ii) application which reads an input string through standard inout and prints it through standard output.
C++ applications also are two in number which includes
i) program which prints the welcome message.
ii) a multi processing application.

Like this,you have many applications. The others include the mobile application in mobility using game builder and many more applications in ruby,UML etc.

Tuesday, April 8, 2008

Keyboard Shortcuts

I have been working on netbeans for quite some time now and I realised that there were still so many avenues that I am yet to explore,so many barriers that I need to cross. Thats one of the main advantages of blogging. Its you and your journey in that platform(which is netbeans in my case). No horrid lectures,no boring classrooms,nothing! Its all about feeling like a discoverer and actually being one!


I have been harping on netbeans making life simple in a few of my previous posts. And I have been giving a lot of keyboard shortcuts in my previous posts which were actually pretty obvious. You can realise this if you had just opened netbeans once. But today,I came across a feature that ACTUALLY gave me a wide grin. Forget API,web services, GUI and all that! One can set their own keyboard shortcuts! I don't know about you but for someone like me who is pathetic in remembering shortcuts,this was such a pleasant idea. It was like finding an old friend in an isolated island! :)


To do this,click on tools->options and then select keymap. Then,you will get a list of actions for which you can add shortcuts. You can choose one action. For example,I choose to add a shortcut for obtaining the contents of the help menu. Observe the screenshot below.







To add, choose the action and click on add. Then you get another window as shown.





After doing this,click on OK and your new keyboard shortcut is set. Apart from setting new shortcuts,you can even change the existing ones. This and much much more!!

Wednesday, February 20, 2008

Need for Netbeans



Why is netbeans necessary??


"Necessity is the mother of all inventions", goes the old saying. Well my dear friend,its the same case here also. Although Java has become inseparably linked with the Internet, it is important to realise that java is first and foremost a programming language. Let us just take a ride down the history lane and look at the development of programming languages. We all know that it was C which first got the status of a "programmer's language". C was developed from 'B' and this fact is well known. However, C++ was also in the coming and it came! Why did this happen? Did the arrival of C++ prove C to be an incompetent language?

NO! As the complexity and demand grew, so did all the computer languages. So after C and C++, the stage was set for Java!! After installing jdk and jre, one can execute java applications at the command prompt. In this feature, it is important for the programmer to remember all commands and recollect them at the appropriate time. Isn't this all a little tedious? Don't we like everything at the click of a button? We always aim for more compact and steady platforms! Many paradigms of view.. god o god!

And all this brings us to confluence of IDE and java applications onto a single platform.. NETBEANS IDE!!! :D I know I seemed like it was the most obvious thing.. ha ha.. Well well,I just saved you a lot of work which would have taken you a lot of time to figure out.. All my blog readers.. consider yourselves blessed!! :D

Experiment Time



Netbeans is NOT the place where one can gain expertise in java! So if you are a novice, I say-"stick to routines". Just to make my point felt, I want to cite an example. Consider this program.



class Fun
{
public static void main(String[] args)
{
int count,i=0;
String words;
count=args.length;
System.out.println("Number of arguments= " +count);
while (i < count)
{
words=args[i];
i++;
System.out.println(i+ ":" + "Java is " +words+ "!");
}
}
}



Execute this program in jdk and at the command for compilation,give the following statement(let your filename be Fun!)

java Fun Robust Simple Secure Portable Multithread

The output of your program would be as follows:

Number of arguments=5
1: Java is Robust!
2: Java is Simple!
3: Java is Secure!
4: Java is Portable!
5: Java is Multithread!

Isn't this such a cool output!!! Now, you know the use of String[] args!!! If you were a starter, I sure can swear that you wouldn't have figured this out.. As far as my knowledge with netbeans goes, I don't think there is an option where I can provide arguments at run-time.. (If there is that option, I am yet to discover it then!!)