C++ UI Invoke not Working - c++

When debugging,I found my program have stopped after
myServer->Invoke(myServer->myShowMessage);
I tried to find where it goes,and I set several breaks but I didn't find where it was going.However,I also set a break on ShowMessageMethod where it should go but I am sure it wasn't going to there.It was just look like dealing with other things and trapping in it.I suspect there was something wrong with the UI thread,but I have little knowledge about the underlying in Windows Message. I have create some other threads in main thread, is that makes things all different? I just followed the Invoke method code on msdn. And Here are my codes:
Server::Server(){
InitializeComponent();
myShowMessage = gcnew ShowMessageDelegate(this,&Server::ShowMessageMethod);
}
System::Void Server::start_Click(System::Object^ sender, System::EventArgs^ e){
Thread^ myThread = gcnew Thread(gcnew ThreadStart(this,&Server::ListenThreadFunc));
myThread->Start();
}
void Server::ShowMessageMethod(){
String^ message = "Get Successful";
this->APTBX->AppendText(message);
}
void Server::ListenThreadFunc(){
ServerListen^ mySL = gcnew ServerListen(this);
mySL->ListenThread();
}
ServerListen::ServerListen(Server^ _s){
myServer = _s;
}
void ServerListen::ListenThread(){
array<Object^>^myStringArray = { convert.toStringDelegate(information) };
Object^ re = myServer->Invoke(myServer->myShowMessage);
}
thanks in advance.

Related

Adding data into existing column SQL

Edit:
randomly searching through other languages i found the correct answer if anyone needs:
"update DataBase_Name set Column= '"+value+"' where ID='"+value+"';"
I need to insert data into a column and couldn't find much info about it on the web.
the code is
private: System::Void button1_Click(System::Object^ sender, System::EventArgs^ e)
{
if (this->listBox1->SelectedIndex!=-1)
{
String^ constring = L"datasource=localhost;port=3306;username=root;password=root;";
MySqlConnection^ conDataBase = gcnew MySqlConnection(constring);
MySqlCommand^ cmdDataBase =
gcnew MySqlCommand(insert into products_shop.productname where idProductName='"+this-listBox1->SelectedItem->ToString()+"' ('ProductX','ProductY') values('"+this->textBox2-Text+","+this->textBox3->Text+"');",conDataBase);
MySqlDataReader^ MyReader;
try
{
conDataBase->Open();
MyReader = cmdDataBase->ExecuteReader();
}
catch(Exception^ex)
{
MessageBox::Show(ex->Message);
}
}
else
{
MessageBox::Show(L"Select an item please");
}
}
insert into products_shop.productname where idProductName='"+this->listBox1-SelectedItem-ToString()+"' ('ProductX','ProductY') values('"+this->textBox2-Text+","+this->textBox3-Text+"');
this line is just my randm guess, any help on how the syntax should actually be? or if it works somehow different..
thanks for help

Changing picture box on mouseover, and resetting on mouseleave

I am developing a windows form program and having a hard time finding how to do this in C++. The MSDN has this page, http://msdn.microsoft.com/en-us/library/system.windows.forms.picturebox.image, but the C++ documentation is lacking compared to the VB.
This is what I have so far. This method is supposed to avoid the common flickering issues but I am not sure where to go from there since I need it to go back to the original image after the mouse leaves.
void InitializeComponent(void)
{
this->btnExit->BackColor = System::Drawing::Color::Transparent;
this->btnExit->BackgroundImageLayout = System::Windows::Forms::ImageLayout::None;
this->btnExit->Image = (cli::safe_cast<System::Drawing::Image^ >(resources->GetObject(L"btnExit.Image")));
this->btnExit->Location = System::Drawing::Point(764, 4);
this->btnExit->Name = L"btnExit";
this->btnExit->Size = System::Drawing::Size(30, 20);
this->btnExit->TabIndex = 3;
this->btnExit->TabStop = false;
this->btnExit->Click += gcnew System::EventHandler(this, &mainForm::btnExit_Click);
}
#pragma endregion
private: System::Void btnExit_OnMouseEnter(System::Object^ sender, System::EventArgs^ e) {
Image^ get ();
void set (Image^ value);
}
Thanks.
private: System::Void btnExit_MouseEnter(System::Object^ sender, System::EventArgs^ e) {
btnExit->Image = Image::FromFile("C:\\Users\\...\\image.png");
}
Works, not sure if it is the proper way to do it.

Referencing TabControls from another Form C++/CLI

I am trying to convert this tabbed browser into C++ from visual basic.
I am trying to reference the Tab Control from Form1.h.
Here is the code on Form1.h:
private: System::Void Form1_Load(System::Object^ sender, System::EventArgs^ e) {
String^ title = String::Concat("TabPage ",(tabControl1->TabCount + 1).ToString());
tab^ newtab = gcnew tab;
newtab->Show();
newtab->TopLevel = false;
newtab->Dock = System::Windows::Forms::DockStyle::Fill;
TabPage^ myTabPage = gcnew TabPage(title);
myTabPage->Controls->Add(newtab);
tabControl1->TabPages->Add(myTabPage);
}
The code on the second form that is trying to create another tab is this:
private: System::Void newTabToolStripMenuItem_Click(System::Object^ sender, System::EventArgs^ e) {
tab^ newtab = gcnew tab;
newtab->Show();
newtab->TopLevel = false;
newtab->Dock = System::Windows::Forms::DockStyle::Fill;
TabPage^ myTabPage = gcnew TabPage();
myTabPage->Controls->Add(newtab);
tabControl1->TabPages->Add(myTabPage);
}
In visual basic all that is required is to add Form1. to the beginning like so...:
//Original
tabControl1.TabPages.Add(myTabPage);
//New
Form1.tabControl1.TabPages.Add(myTabPage);
How could I do this same thing in C++?
Visual Basic provides a default instance of each class in your project. When you say Form1.tabControl1, you're actually getting a particular global instance of Form1, and accessing the tabControl1 field on that.
Add a way to send the instance of Form1 to the second form, and use that instead of Form1. Something simple like passing the instance of Form1 to the second form in its constructor will probably do the trick.

Rhino Mocks, MbUnit: Best way to check if object has raised an event

I have an object that I'm testing that raises an event. What is the best way of using Rhino Mocks to check that it was raised?
Best I could come up with (I am certain it gets better than this):
public void MyCallback(object sender, EventArgs e) { _flag = true;}
[Test]
public void DoSomethingRaisesEvent() {
_flag = false;
using(_mocks.Record()) {
Expect.Call(delegeate { _obj.DoSomething();});
}
using(_mocks.Playback()) {
_obj = new SomethingDoer();
_obj.SomethingWasDoneEvent += new EventHandler(MyHandler);
Assert.IsTrue(_flag);
}
}
I found this article by Phil Haack on how to test events using anonymous delegates
Here is the code, ripped directly from his blog for those too lazy to click through:
[Test]
public void SettingValueRaisesEvent()
{
bool eventRaised = false;
Parameter param = new Parameter("num", "int", "1");
param.ValueChanged +=
delegate(object sender, ValueChangedEventArgs e)
{
Assert.AreEqual("42", e.NewValue);
Assert.AreEqual("1", e.OldValue);
Assert.AreEqual("num", e.ParameterName);
eventRaised = true;
};
param.Value = "42"; //should fire event.
Assert.IsTrue(eventRaised, "Event was not raised");
}
I'm not sure how your test actually calls the DoSomething() Method. Maybe you're missing something to fire the event. Other than that, I think you have are on the right track for testing events with Rhino Mocks
In any case, here is another way I like to deal with events:
[Test]
public void MyEventTest()
{
IEventRaiser eventRaiser;
mockView = _mocks.CreateMock<IView>();
using (_mocks.Record())
{
mockView.DoSomethingEvent += null;
eventRaiser = LastCall.IgnoreArguments();
}
using (_mocks.Playback())
{
new Controller(mockView, mockModel);
eventRaiser.Raise(mockView, EventArgs.Empty);
}
}

Best practice for webservices

I've created a webservice and when I want to use its methods I instantiate it in the a procedure, call the method, and I finally I dispose it, however I think also it could be okay to instantiate the webservice in the "private void Main_Load(object sender, EventArgs e)" event.
The thing is that if I do it the first way I have to instantiate the webservice every time I need one of its methods but in the other way I have to keep a webservice connected all the time when I use it in a form for example.
I would like to know which of these practices are better or if there's a much better way to do it
Strategy 1
private void btnRead_Click(object sender, EventArgs e)
{
try
{
//Show clock
this.picResult.Image = new Bitmap(pathWait);
Application.DoEvents();
//Connect to webservice
svc = new ForPocketPC.ServiceForPocketPC();
svc.Credentials = new System.Net.NetworkCredential(Settings.UserName, Settings.Password);
svc.AllowAutoRedirect = false;
svc.UserAgent = Settings.UserAgent;
svc.PreAuthenticate = true;
svc.Url = Settings.Url;
svc.Timeout = System.Threading.Timeout.Infinite;
svc.CallMethod();
...
}
catch (Exception ex)
{
ShowError(ex);
}
finally
{
if (svc != null)
svc.Dispose();
}
}
Strategy 2
private myWebservice svc;
private void Main_Load(object sender, EventArgs e)
{
//Connect to webservice
svc = new ForPocketPC.ServiceForPocketPC();
svc.Credentials = new System.Net.NetworkCredential(Settings.UserName, Settings.Password);
svc.AllowAutoRedirect = false;
svc.UserAgent = Settings.UserAgent;
svc.PreAuthenticate = true;
svc.Url = Settings.Url;
svc.Timeout = System.Threading.Timeout.Infinite;
}
private void btnRead_Click(object sender, EventArgs e)
{
try
{
//Show clock
this.picResult.Image = new Bitmap(pathWait);
Application.DoEvents();
svc.CallMethod();
...
}
catch (Exception ex)
{
ShowError(ex);
}
}
private void Main_Closing(object sender, CancelEventArgs e)
{
svc.Dispose();
}
It depends on how often you are going to be calling the web service. If you're going to be calling it almost constantly, it would probably be better to use method #2. However, if it's not going to be getting called quite so often, you are better off using method #1, and only instantiating it when you need it.
Right now I made a solution for a mobile device and it turns to be used on irregular times, it could be used in 10 minutes, 1 hour, 4 hours its very variable, it seems that the better aproach is the first strategy.
Last year we went on a project where we used webservices, the fact is that we instantiated our webservices at the Sub New() procedure and it run it very well, however, sometimes some users claimed at us that they woke up from their chairs and when they returned and tried to continue on the application they received a timeout error message and they had to re-login again.
We thougth that maybe that was Ok because maybe the users went out for a very long time out of their seats, but once in a presentation of the application with the CEOs it happened exactly the same scenario and personally I didn't like that behaviour and that's why the question.
Thanks for the answer.