i'm trying to create an application which connects to internet and consume web services for every of it's operation.As far as i'm concerned i'll like to useasync method which i'm using already to get a collection of Contacts.I've realized that when i'm doing the same for groups (meaning getting groups async) i'm having errors in the calls , but when using normal call there ins't.So i did some research online and find that a lot of people has the same problem.
Some of them are asked to use WCF (for which i don't know jack).I'll like to know if there is another way to overcome this. if not can somebody point me to reliable resource online and help me get through it? thanks for reading and helping
here is my code:
public partial class main : Window
{
//...
private static vmcSession session;
private MyService service = new MyService();
private contactInfo[] loadedcontact;
//....
public main()
{
InitializeComponent();
//service.addContactCompleted +=new addContactCompletedEventHandler(addContactCompleted);
service.getContactsCompleted += new getContactsCompletedEventHandler(getContactsCompleted);
service.getGroupsCompleted += new getGroupsCompletedEventHandler(getGroupsCompleted);
fillContents();
}
private void getGroupsCompleted(object sender, getGroupsCompletedEventArgs e)
{
try
{
groupListBox.ItemsSource = e.Result;
}
catch (Exception ex)
{
MessageBox.Show(ex.InnerException.Message);
}
}
private void getContactsCompleted(object sender, getContactsCompletedEventArgs e)
{
try
{
loadedcontact = e.Result;
contactListBox.ItemsSource = loadedcontact;
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
private void addContactCompleted(object sender, addContactCompletedEventArgs e)
{
throw new NotImplementedException();
}
public void fillContents()
{
displayUserInformation();
loadContacts();
service.getGroupsAsync(session.key, null);
//groupListBox.ItemsSource = service.getGroups(session.key, null);
cmbSenderIds.ItemsSource = service.getSenderIds(session.key, null);
if (cmbSenderIds.Items.Count > 0)
{
cmbSenderIds.SelectedIndex = 0;
}
loadGrid();
}
public void loadContacts()
{
service.getContactsAsync(session.key, null);
}
public void displayUserInformation()
{
lblName.Content = session.user.firstName;
lblEmail.Content = session.user.email;
lblCreditValue.Content = Decimal.Ceiling(session.user.balance).ToString();
}
public void loadGrid()
{
try
{
hitoryGrid.ItemsSource = service.viewMessages(session.key, null).ToList();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
}
solve it.there are 2 methods with async calls, one with additional parameter Unique ID.each of the call needed ID, so i pass new GUID to it and that's it.thanks for trying helping me
Related
I just learned how to pass arguments into methods, so I'm refactoring my code to make it cleaner. I have created a new "ValidateInput" class which holds a ValidateFinancialsInput method which I pass a string into. It then checks the string to see if it is correct, if it's not I want to show a messageBox, then stop execution of ALL of the code. If i use "return;", it just resumes execution of the Parent method. How do I stop execution of all of the code within the ValidateFinancialsInput method? I tried researching this for a while to no avail. Here is my code:
Class Parent
{
private void button2_Click(object sender, EventArgs e)
{ var CompanyVar = comboBox1.Text;
ValidateInput vi = new ValidateInput();
vi.ValidateFinancialsInput(CompanyVar);
//the rest of my code for the application is here
//the rest ...
//the rest...
}
}
class ValidateInput
{
public void ValidateFinancialsInput(string Co)
{
string[] validCompany = { "BVV", "LWDO" };
if (validCompany.Contains(Co) == false)
{
MessageBox.Show("You have entered an invalid company.");
//what do I put here to stop all code execution?
}
}
}
You should try and use return values state intent to calling methods
Class Parent
{
private void button2_Click(object sender, EventArgs e)
{ var CompanyVar = comboBox1.Text;
ValidateInput vi = new ValidateInput();
if(!vi.ValidateFinancialsInput(CompanyVar))
{
MessageBox.Show("You have entered an invalid company.");
return;
}
//the rest of my code for the application is here
//the rest ...
//the rest...
}
}
class ValidateInput
{
public bool ValidateFinancialsInput(string Co)
{
string[] validCompany = { "BVV", "LWDO" };
if (validCompany.Contains(Co) == false)
{
return false;
}
return true;
}
}
What I'm doing here is returning a true|false value to indicate whether the validation has passed, if it has not passed then I display the MessageBox, else it continues the execution of the "other" code.
Hope this helps
The simplest way is with an exception:
class Parent
{
private void button2_Click(object sender, EventArgs e)
{
try
{
var CompanyVar = comboBox1.Text;
ValidateInput vi = new ValidateInput();
vi.ValidateFinancialsInput(CompanyVar);
//the rest of my code for the application is here
//the rest ...
//the rest...
}
catch (ValidationException ex)
{
MessageBox.Show(ex.Message);
}
}
}
class ValidationException : Exception
{
public ValidationException(string message) : base(message)
{
}
}
class ValidateInput
{
public void ValidateFinancialsInput(string Co)
{
string[] validCompany = { "BVV", "LWDO" };
if (validCompany.Contains(Co) == false)
{
throw new ValidationException("You have entered an invalid company.");
}
}
}
This will stop execution of ValidateFinancialsInput and in button2_Click move execution inside the catch (ValidationException ex) where you can decide what to do with the validation error
You have a class that it's whole purpose is to validate, So you could add a public method IsValidated
You could add much more with the class, for example have a list of all business rules it violates and return them through another method or property.
class ValidateInput
{
public bool IsValidated {get; private set}
public bool ValidateFinancialsInput(string Co)
{
string[] validCompany = { "BVV", "LWDO" };
this.IsValidated = validCompany.Contains(Co)
}
}
This class should only know about the validation process and should do nothing else.
You have a few options. It looks like you have buttons in your program so I would guess this is not a console application. If you want the application to completely stop you can use Application.Exit or check out Environment.Exit https://msdn.microsoft.com/en-us/library/system.environment.exit(v=vs.110).aspx
However, I would suggest using exceptions so you do not terminate your entire program:
try
{
var CompanyVar = comboBox1.Text;
ValidateInput vi = new ValidateInput();
vi.ValidateFinancialsInput(CompanyVar);
//the rest of my code for the application is here
//the rest ...
//the rest...
}
catch (ValidationException ex)
{
MessageBox.Show(ex.Message);
}
}
public void ValidateFinancialsInput(string Co)
{
string[] validCompany = { "BVV", "LWDO" };
if (validCompany.Contains(Co) == false)
{
throw new ValidationException("You have entered an invalid company.");
}
}
In the overrided void onIncomingCall I open a new Background thread which shows a new (Popup) Form.
I try to pickup or decline the Call in this Form but then I get an System.AccessViolationException.
it seems that the current call object is locked and cannot be accessed from another Form.
If I use the currentcall.answer(prm) in the onIncomingCall void then the call is established successfully (without another Form and thread).
public class myaccount : Account
{
public override void onIncomingCall(OnIncomingCallParam prm)
{
Call call = new Call(this, prm.callId);
CallOpParam param = new CallOpParam();
param.statusCode = pjsip_status_code.PJSIP_SC_RINGING;
param.statusCode = pjsip_status_code.PJSIP_SC_OK;
pjsipfunctions.currentparam = param;
pjsipfunctions.currentcall = call;
var thread = new Thread(() =>
{
ShowPopup(call,prm.callId.ToString());
});
thread.SetApartmentState(ApartmentState.STA);
thread.Start();
// Thread myCallingThread = new Thread(new ThreadStart(ShowPopup));
//myCallingThread.SetApartmentState(ApartmentState.STA);
//myCallingThread.IsBackground = true;
//myCallingThread.Start();
}
public void ShowPopup(Call myCall,string call_id)
{
IncommingCallPopup win = new IncommingCallPopup(ref myCall, call_id );
win.Show();
System.Windows.Threading.Dispatcher.Run();
}
}
#####################################
public IncommingCallPopup(ref Call info, string callid)
{
currentCall = info;
Callid = callid;
CurrentCall = currentCall;
Prm.statusCode = pjsip_status_code.PJSIP_SC_RINGING;
InitializeComponent();
labelCallId.Content = callid;
}
private void rejectcall(object sender, RoutedEventArgs e)
{
Prm.statusCode = pjsip_status_code.PJSIP_SC_DECLINE;
CurrentCall.hangup(Prm);
}
private void transfercall(object sender, RoutedEventArgs e)
{
}
private void takecall(object sender, RoutedEventArgs e)
{
Prm.statusCode = pjsip_status_code.PJSIP_SC_OK;
try
{
CurrentCall.answer(Prm);
}
catch(Exception ex)
{
}
}
After crazy searching and trying some things i've figured it out.
Every Thread must registered in the Endpoint that you can hangup, pickup or transfer your call in another Class(Window).
All you need is to write this Method in a Public class and call this void everytime you call your void from another window ->
public void callpopup(String number) {
checkThread();
//answer, decline or something else
}
[MethodImpl(MethodImplOptions.Synchronized)]
public static void checkThread()
{
try
{
if (ep != null && !ep.libIsThreadRegistered())
ep.libRegisterThread(Thread.CurrentThread.Name);
}
catch (Exception e)
{
}
}
I've written a small WCF WebService (.svc) which I want to call from other applications. It's working fine so far.
In another application I've got a Windows Form. When this is shown, it will call BackgroundWorker1 to get a list of objects from the WebService and show it to the user on it's completed1-method. So far, so good.
Now, after the user selects one object and clicks "ok", then BackgroundWorker2 will call the WebService again to gather more information. But here, within the doWork2-method exactly after the WebService-call, the whole application breaks without any exceptions or errors. It just closes directly after the WebService is called.
This is very weird, because as I have a look at the WebServices log files, it seems to work normal and still logs the successful operation AFTER the other application is closed. So the WebService cannot be the problem, I think.
Another weird thing: If I call the WebService the second time on the GUI thread (and not with BackgroundWorker2), it just works. It blocks the UI, but it works.
So, why is my application just "broken" after the second call without any notification?
Any ideas are very much welcomed.
Thanks in advance.
Greets
Here's some simplified code. The application closes in "bgwGetSingleCar_DoWork":
public partial class MyForm : Form
{
private Controller _ctrl { get; set; }
private Config _config { get; set; }
private List<Cars> _cars { get; set; }
public bool Result { get; private set; }
public MyForm(Controller ctrl, Config config)
{
this._ctrl = ctrl;
this._config = config;
this.Result = false;
InitializeComponent();
}
private void MyForm_Load(object sender, EventArgs e)
{
try
{
this.bgwGetAllOffers.RunWorkerAsync(new WorkerInfo()
{
WorkerType = WorkerType.Type1,
IdLocal = this._config.IdLocal,
IdExternal = this._config.IdExternal,
});
}
catch (Exception ex)
{
// ...
}
}
private void FillList(List<Cars> list)
{
// ...
}
private void btnOk_Click(object sender, EventArgs e)
{
CarListItem v = (CarListItem)this.lstBoxCars.SelectedValue;
this._config.IdExternal = v.IdExternal;
try
{
this.bgwGetSingleCar.RunWorkerAsync(new WorkerInfo()
{
WorkerType = WorkerType.Type2,
IdLocal = this._config.IdLocal,
IdExternal = this._config.IdExternal,
});
}
catch (Exception ex)
{
// ...
}
}
private void bgwGetAllCars_DoWork(object sender, DoWorkEventArgs e)
{
try
{
WorkerInfo info = (WorkerInfo)e.Argument;
Client client = new Client();
GetCarsResult result = client.GetAllCars(new GetAllCarsRequest()
{
IdLocal = info.IdLocal,
IdExternal = info.IdExternal
});
if (!result.Success)
{
// ...
}
if (result.Cars != null)
{
this._cars = result.Cars.ToList();
}
}
catch (Exception ex)
{
/// ...
}
}
private void bgwGetAllCars_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
this.FillList(this._cars);
}
private void bgwGetSingleCar_DoWork(object sender, DoWorkEventArgs e)
{
try
{
WorkerInfo info = (WorkerInfo)e.Argument;
Client client = new Client();
// Application closes after this line, but WebService log still shows activity
GetCarsResult result = client.GetSingleCar(new GetSingleCarRequest()
{
IdLocal = info.IdLocal,
IdExternal = info.IdExternal
});
if (result.Success)
{
this.Result = true;
}
}
catch (Exception ex)
{
/// ...
}
}
private void bgwGetSingleOffer_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
this.DialogResult = System.Windows.Forms.DialogResult.OK;
}
}
public class CarListItem : Car
{
public override string ToString()
{
return ((DateTime)this.LastUpdate).ToString("dd.MM.yyyy - HH:mm");
}
}
I just found my "design break".
It was the button's "DialogResult" which caused the application to close too soon. But strange that the debugger didn't step ahead...
I have Windows phone page that fetch data from the web url using async. In my page class consructor I cannot call a async method. How do I initialize properties from the web url repsonse in the constructor.
public MyProfile()
{
InitializeComponent();
_populateFields();
}
private async void _populateFields()
{
try
{
var taskObj = await UserProfile.getUserProfile().getUserProfileFromServer();
//PoolCircle.UserProfile.ProfileResponseJson userObj = taskObj;
setDataContext(taskObj);
}
catch (Exception ex) { Debug.WriteLine(ex.Message); }
}
How do I fix this dependency? I am not sure how a factory method fix it. I am not creating object for the page class..
Try it from the OnNavigatedTo method. Overriding it. Its a method who you can put all your logic before the page appears
protected async override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e)
{
base.OnNavigatedTo(e);
_populateFields();
}
Documentation
hope helps! Greetings!
Make a method like PostData(); and call this after InitializeComponent();
private async void PostData()
{
try
{
var taskObj = await UserProfile.getUserProfile().getUserProfileFromServer();
//PoolCircle.UserProfile.ProfileResponseJson userObj = taskObj;
setDataContext(taskObj);
}
catch (Exception ex) { Debug.WriteLine(ex.Message); }
}
I'm having some problems calling a class or method in C# using Mono, can anyone give some help on what I'm doing wrong?
I want this piece of code:
public static void Execute(ScriptHost host)
{
try
{
TesteGUI teste = new TesteGUI(); //?
TesteGUI(); // ?
}
catch(Exception e)
{
}
finally
{
}
}
To call this piece of code:
public TesteGUI(Gtk.Window parentWindow) : base(Gtk.WindowType.Toplevel)
{
base.Modal = false;
base.TransientFor = parentWindow;
base.Decorated = false;
base.WindowPosition = WindowPosition.CenterAlways;
this.MyBuild();
base.KeyReleaseEvent += delegate(object o, KeyReleaseEventArgs args)
{
if (args.Event.Key == Gdk.Key.Escape)
{
this.Destroy();
}
};
}
What is my doing wrong and how can I make it work?
Thank you
You should do it like this:
public static void Execute(ScriptHost host)
{
try
{
TesteGUI teste = new TesteGUI(parentWindow); //where parentWindow is defined somewhere earlier and is of type Gtk.Window
}
catch(Exception e)
{
}
finally
{
}
}