|
I'm creating a socket server in which I'm getting a string like this.
IPEndPoint ipEnd = new IPEndPoint(IPAddress.Any, 5656);
Socket sock = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.IP);
sock.Bind(ipEnd);
sock.Listen(100);
Socket clientSock = sock.Accept();
while (true)
{
clientData = new byte[1024];
receivedBytesLen = clientSock.Receive(clientData);
clientDataInString = Encoding.ASCII.GetString(clientData, 0, receivedBytesLen);
Console.WriteLine("Received Data{0}",clientDataInString);
if(clientDataInString.Contains("Start"))
{
.....any method call....
}
}
I want to use this clientDataInString or the received string in another method.
Like this:
void abc()
{
if(clientDatainString.containg("A"))
{
Console.Writeline("Print A");
}
else if(clientDatainString.containg("B"))
{
Console.Writeline("Print B");
}
}
How can I use this String like this?
Please Suggest..
|
|
|
|
|
CodingHell wrote: How can I use this String like this?
Please Suggest.. Suggest what? You use this string the same way that you would use any string. Your question is not very clear.
Use the best guess
|
|
|
|
|
I'm unable to use the received string in a method because server socket is working in main method but abc method is not a part of this main.
So my motive is to switch and use received string in other methods of my program except where these are being received.
|
|
|
|
|
Then you need to pass the string to the other method as a fuction parameter.
Use the best guess
|
|
|
|
|
Pass the string as a parameter
[...]
Console.WriteLine("Received Data{0}",clientDataInString);
abc(clientDataInString);
} Of course, abc() has to accept a parameter of type System.String then:
void abc(string input)
{
if(input.Contains("A"))
{
Console.WriteLine("Print A");
}
else if(input.Contains("B"))
{
Console.WriteLine("Print B");
}
}
Ciao,
luker
|
|
|
|
|
I'm new to this, or precisely just realize that visual studio had this in the properties folder below visual studio project.
I'm planning to put important things like connection strings inside.
I tried to save a simple text in the setting and save it.
When I run the application, in the debug folder appear "WindowsFormsApplication1.exe.config" file.
I know that it's the setting I created, but the value of the setting is not inside.
When I re-run the application, it was able to load the text i saved before.
Where did the setting save the value?
Btw sorry for bad english.
|
|
|
|
|
|
private void button1_Click(object sender, EventArgs e)
{
Properties.Settings.Default.Constr = this.textBox1.Text;
Properties.Settings.Default.Save();
}
private void Form1_Load(object sender, EventArgs e)
{
this.textBox1.Text = Properties.Settings.Default.Constr;
}
Constr is the setting I create.
Actually I'm going to put licensing there too, of course I will encrypt it before I save.
Now I still can't find it.
This is what I use > http://msdn.microsoft.com/en-us/library/aa730869%28v=vs.80%29.aspx[^]
|
|
|
|
|
That's only one of the config files. It's like a blueprint holding the values you enter in Visual Studio at coding time.
What your application saves at runtime is stored in a user-dependent copy of that file somewhere in the region of "Documents and Settings/$YourUserName$/Application Data/$Company$/$ApplicationNameWithSomeAlphaNumericFuzz$/$ProductVersion$/user.config"
The exact location may depend on your OS version.
Ciao,
luker
|
|
|
|
|
lukeer wrote: "Documents and Settings/$YourUserName$/Application Data/$Company$/$ApplicationNameWithSomeAlphaNumericFuzz$/$ProductVersion$/user.config"
I found this but nothing inside.
"C:\Documents and Settings\Administrator\Application Data\Microsoft Corporation\Microsoft (R) Visual Studio (R) 2010\1.0.30319.1"
There are no strange company name in Application Data.
Btw thank you for answering.
Edit :
I found it here[^]
It's just like what you answered, I missed Local Settings before Application Data.
Thank you very much.
modified 23-Jul-13 6:00am.
|
|
|
|
|
Midnight Ahri wrote: I found it here[^]
Documented on MSDN[^].
Bastard Programmer from Hell
If you can't read my code, try converting it here[^]
|
|
|
|
|
I have the following Setup:
1) The WPF Thread itself (the MainWindow), which stores the Controls to Display my Content, and the Thread Safe Properties with the content to display.
2) The Update Thread (System.Threading.Thread, not BackgreoundWorker). This Thread updates the Properties of the WPF Thread.
3) The Draw Thread (System.Threading.Thread, not BackgreoundWorker), which actually manipulates the Controls of the WPF Thread (Adding Canvas, Removing Canvas, Setting Text, ...)
My Main problem is the thing, that the WPF Thread is storing the Thread Safe Properties. My Update Routine and Draw Routine are both called very very often (1000 Times a secound).
And thats exactly the problem. The WPF Thread is totally blocked due to the many Get, Set Operations of the other both Threads.
So my first Question is, does someone has an Idea where to store these Properties so the main WPF Thread isn't blocked?
My Secound Problem is the following:
The Update and Draw Thread are both working with the Dispatcher of the WPF thread. If they execute 1000 Times a secound the Dispatcher just runs out of mem (because of too many actions in queue). Is there a way to measure how many actions are in queue and to know if to store another or not. And Secound is there a way to say "Now execute the queue".
I know i can get the state of each dispatcher Object i add. But i don't want to wait till each action is e executed. I want to add as much as possible, so the Update and Draw Thread can run as fast as possible.
My Third side Question is, is my Concept to use WPF for this stupid, or not well planned? Shouldn't i use WPF for this, is there another way to achive my Goal?
My Goal explained:
via Update i have to draw content, very often to be exact, without getting the Part to display the content laggie aka responsive UI.
Please help me with any suggestions or sth else.
As a side Idea i have thought of taking the Thread safe properties into a third thread aka DataThread to store and change, but how can u store Properties into a thread? what is a good way to achive this?
thx in advance
sincerly Synergi
EDIT
Here is an example of one of my Thread Safe Properties. Maybe im loosing much time here. May there is a better Solution for this.
private Nullable<Point> Offset
{
get
{
Nullable<Point> returnValue = null;
object lockObject = objectPool.Aquire();
lockObject = this.offset;
bool locked = false;
bool aquired = false;
System.Threading.Monitor.TryEnter(lockObject, ref locked);
try
{
if (locked)
{
returnValue = this.offset;
aquired = true;
}
}
finally
{
if (locked && aquired)
{
System.Threading.Monitor.Exit(lockObject);
}
}
objectPool.Release(lockObject);
return returnValue;
}
set
{
object lockObject = objectPool.Aquire();
lockObject = this.offset;
bool locked = false;
bool aquired = false;
System.Threading.Monitor.TryEnter(lockObject, ref locked);
try
{
if (locked)
{
if (value.HasValue)
{
this.offset = value.Value;
aquired = true;
}
}
}
finally
{
if (locked && aquired)
{
System.Threading.Monitor.Exit(lockObject);
}
}
objectPool.Release(lockObject);
}
}
modified 22-Jul-13 21:16pm.
|
|
|
|
|
I SERIOUSY DOUBT you're getting 1,000 frames per second nor even have the hardware that can do it. There's just no point to it because the human eye just can't see that. Knock that back to AT MOST 60 frames a second and you might improve your situation dramatically.
|
|
|
|
|
I miss discribed my situation. I dont have over 1000 Frames, my update & Draw Thread are called more then 1000 Times. But that doesn't mean it is really the Render Thread of DirectX.
So now with ur conclusion of slowing it down to 60 fps the gui is now responsive again. Easy Solution.
Side way i still ask if someone knows a way to store properties out of the WPF Thread?
|
|
|
|
|
Hi Folks,
I have a question about the best way to design / redesign my software. I have a program that uses a web services to perform certain tasks. Each task is a different set of request / response classes on the server end and a general function call from the Main Hook. EG.
RequestObject req = new RequestObject("SomeVal");
ResponseObject rsp = new ResponseObject();
rsp = WebServiceHook.Method1(RequestObject);
Each Method takes a different request object and returns a different type of response object.
As it stands i have a class for each of these methods, Each class has a public method Process() that does the interaction. I am trying to determine the best way to group all this code together using OO techniques without sacrificing functionality. I would like just one ProcessMethod in one class that will handle the interaction with the webservice for all the different web methods. So i would only call one ProcessMethod passing a switch of sorts that would define the relevant types and uniques strings that method requires. Some Sample code below:
[ComVisible(false)]
private static InfoRequest infoReq = null;
[ComVisible(false)]
private static InfoResponse infoRsp = null;
public static bool Process(INTERFACEREQUEST COMReq, out INTERFACERESPONSE COMRsp)
{
bool blnReturnVal = false;
CInfoRsp InfoRsp = new CInfoRsp();
CInfoReq InfoReq = (CInfoReq)COMReq;
Globals.dtStart = DateTime.Now;
Globals.Log(Logger.LogLevel.Minimum, Logger.LogType.APIMethodEntryPoint, "SOME TEXT HERE", "AND HERE", InfoReq.SalePointID);
try
{
Globals.TheService.Url = Settings.URL;
infoReq = new InfoRequest();
infoRsp = new InfoResponse();
SetRequest(InfoReq);
CallWEBServiceMethod();
SetCOMResponseValues(InfoRsp);
COMRsp = InfoRsp;
blnReturnVal = AssignCOMResponse(InfoRsp);
}
catch (Exception ee)
{
COMRsp = new CInfoRsp(ee);
blnReturnVal = false;
Globals.Log(Logger.LogLevel.All, Logger.LogType.APIMethodException, ee.ToString() + " " + ee.InnerException);
}
finally
{
InfoRsp = null;
InfoReq = null;
infoReq = null;
infoRsp = null;
GC.Collect();
}
}
Is what i am trying to achieve possible or am i trying to over engineer this solution. It currently works i just find every time i have to add a new web method call i clone one of the existing classes and change the types in the processMethod and anywhere else that's needed. This is quite tedious and prone to bugs.
Thanks In Advance
|
|
|
|
|
|
KeithF wrote: Am i correct in saying this?
Yes.
You'd want an (abstract) base-class that defines the Process method;
public abstract bool Process(BaseProcessArgs args)
{ The factory would return a class that derives from this abstract base-class, with each class having it's own implementation for the Process method. The app would only have to call the method, without knowing specific details on the class.
Varying the parameters can be done in a similar way; EventArgs is a nice example on how to vary parameters, and still have some control. Imagine a base-class for the arguments, called "BaseProcessArgs"; if you'd need to pass a string and an int, your "Args" class would look like below;
class TestArgs: BaseProcessArgs
{
public string TestString { get; set; }
public int TestInt { get; set; }
} Alternatively, if only types will change, you might want to consider a generic class.
Bastard Programmer from Hell
If you can't read my code, try converting it here[^]
|
|
|
|
|
Folks,
Question about my design so far, I am wondering if i am going about this the right way?
I have a COM Library created in C#, with COMVisible set to true. This library talks to a 3rd party WebService calling various methods depending on the task at hand. For each Request / Response class exposed by the 3rd party DLL I have a Request / Response Pair (Class and Interface) to Marshal variables specifically for a VC6 application.
Mainly using:
MarshalAs(UnmanagedType.LPStr)]
So with that in my i have added a C# project to the solution to test this code, see test method below:
static void Main(string[] args)
{
ICOMReq iReq = new CCOMReq();
ICOMRsp iRsp = new CCOMRsp();
Link l = new Link();
iReq.Date_Time = DateTime.UtcNow;
iReq.Var2 = "1023758145865";
l.DoMethod1(iReq, out iRsp);
}
The link class looks like so at the moment be but will have all the methods created once the design is correct:
public class Link
{
public bool DoMethod1(ICOMReq _COMReq, out ICOMRsp COMRsp)
{
Method1 WebServiceMethod = new Method1(new Method1Request(), new Method1Response(), (CCOMReq)_COMReq, new CCOMRsp());
WebServiceMethod.Process();
COMRsp = null;
return true;
}
}
Each DoMethod(N) that will be in the link class will look the same performing its task with identical code to the other DoMethods. The key differences between the methods is the Param Types Passed in and the Method1 (Method1Request/Method1Response) type will vary depending on the webmethod to be called.
Class Method1 (There will be one of these for every method I need to implement on the WebService) looks like so:
public class Method1 : WebServiceInterfaceBridge<Method1Request, Method1Response, ICOMReq, ICOMRsp>
{
public Method1(Method1Request WEB_Req, Method1Response WEB_Rsp, CCOMReq COM_Req, ICOMRsp COM_Rsp)
: base(WEB_Req, WEB_Rsp, CBE_Req, CBE_Rsp)
{
WEB_Req.SOME_DATE_TIME = COM_Req.Date_Time;
}
public new void Process()
{
base.Process();
}
public override void LOG()
{
Console.WriteLine(this.WebMethod_Request.MEMBER_VAR_HERE);
}
}
The class (WebServiceInterfaceBridge) that all Method(N) classes will inherit from is shown below:
public abstract class WebServiceInterfaceBridge <T,U,V,W> : WebServiceInterface
where T : class
where U : class
where V : class
where W : class
{
protected T WebMethod_Request;
protected U WebMethod_Response;
protected V COM_Request;
protected W COM_Response;
public WebServiceInterfaceBridge(T tPHSReq, U uPHSRsp, V vCBEReq, W wCBERsp)
{
WebMethod_Request = tPHSReq;
WebMethod_Response = uPHSRsp;
COM_Request = vCBEReq;
COM_Response = wCBERsp;
}
public abstract void LOG();
public void CallWebServiceMethod()
{
WebMethod_Response = CallWebMethod<T, U>(WebMethod_Request);
}
public void Process()
{
LOG();
CallWebServiceMethod();
}
}
And finally here is the class that actually calls the Web service that the WebServiceBridge Class inherits from:
public class WebServiceInterface
{
private ServiceClient WEBSVC = new ServiceClient();
public U CallWebMethod<T, U>(T tRequest)
where T:class
where U:class
{
if (tRequest.GetType() == typeof(Method2Request))
{
Method2Request Req = (Method2Request)(object)tRequest;
Method2Response Rsp = WEBSVC.Method2(Req);
return (U)(object)Rsp;
}
else if (tRequest.GetType() == typeof(Method1Request))
{
Method1Request Req = (Method1Request)(object)tRequest;
Method1Response Rsp = WEBSVC.Method1(Req);
return (U)(object)Rsp;
}
else
{
throw new NullReferenceException();
}
}
}
I have left out my internal classes for marshaling the Req/Rsp's, they are just classes with Member Vars and Get/Set's.
So basically before i carry on with my design I am wondering from an OO point of view if I am approaching this task in the right way or if what I am doing is overkill and could be greatly simplified?
TIA
|
|
|
|
|
Hi All,
I have used this sample as reference How to use a web cam in C# with .NET Framework 4.0 and Microsoft Expression Encoder 4[^]
and managed to modify the files to stream to my IIS . I have no issue streaming towards my own IP address . However , problem arises when i try to stream it to another IP address . It disconnected after few minutes of streaming but strangely at times it works fine(Not most of the time) . Asked at Microsoft Expression Encoder Forum and no one dares to answer me . I hope here are brave programmers here which can help me solve this particular issue . Do request me if you need to take a look at my codes .
Thanks ,
Daniel
|
|
|
|
|
What does this have to do with C#?? A dropped connection doesn't have anything to do with your code. Once the connection starts it's up to Encoder and the client to keep it going. Your code has nothing to do with it.
|
|
|
|
|
I want to get the caret position with my C# application.but in some cases ,GetGuiThreadInfo does not work creectly.
it works well on
1. notepad
2. ie
3. explorer
4. word
and works bad on (hwndCaret will be 0)
1. Visual Studio
2. Firefox
3. Sublime Text 2
Anyone who can helps me?
public partial class Form1 : Form
{
[DllImport("user32.dll")]
public static extern IntPtr GetForegroundWindow();
[DllImport("user32.dll", CharSet = CharSet.Auto)]
public static extern IntPtr SendMessage(IntPtr hWnd, uint Msg, IntPtr wParam, IntPtr lParam);
[DllImport("user32.dll")]
static extern uint GetWindowThreadProcessId(IntPtr hWnd, IntPtr ProcessId);
[DllImport("user32.dll")]
static extern bool GetGUIThreadInfo(uint idThread, ref GUITHREADINFO lpgui);
[StructLayout(LayoutKind.Sequential)]
public struct GUITHREADINFO
{
public int cbSize;
public int flags;
public IntPtr hwndActive;
public IntPtr hwndFocus;
public IntPtr hwndCapture;
public IntPtr hwndMenuOwner;
public IntPtr hwndMoveSize;
public IntPtr hwndCaret;
public Rectangle rectCaret;
}
public GUITHREADINFO? GetGuiThreadInfo(IntPtr hwnd)
{
if (hwnd != IntPtr.Zero)
{
uint threadId = GetWindowThreadProcessId(hwnd, IntPtr.Zero);
GUITHREADINFO guiThreadInfo = new GUITHREADINFO();
guiThreadInfo.cbSize = Marshal.SizeOf(guiThreadInfo);
if (GetGUIThreadInfo(threadId, ref guiThreadInfo) == false)
return null;
return guiThreadInfo;
}
return null;
}
public void SendText(string text)
{
IntPtr hwnd = GetForegroundWindow();
if (String.IsNullOrEmpty(text))
return;
GUITHREADINFO? guiInfo = GetGuiThreadInfo(hwnd);
AddMessage("go");
if (guiInfo != null)
{
IntPtr ptr = (IntPtr)guiInfo.Value.hwndCaret;
AddMessage("hwndCaret = " + ptr.ToInt32());
if (ptr != IntPtr.Zero)
{
AddMessage("Left = " + guiInfo.Value.rectCaret.Left);
for (int i = 0; i < text.Length; i++)
{
SendMessage(ptr, 0x102, (IntPtr)(int)text[i], IntPtr.Zero);
}
}
}
}
public Form1()
{
InitializeComponent();
}
private void AddMessage(string message)
{
var i = listBox1.Items.Add(message);
listBox1.SelectedIndex = i;
}
private void button1_Click(object sender, EventArgs e)
{
Thread.Sleep(2000);
SendText("Abcdefg");
AddMessage("------------");
}
}
|
|
|
|
|
hey Friends....
I have a Table with Name (Student_Table)
fields(Stdid, StdRollNo, StuName)
with 5 records
And A windows Form With Name Form1
Now, My question is..
I want Dynamic Buttons equals to students records ie.. 5 buttons,
on That Buttons i want Student Roll No.
please guys help me
Jack_Spero
|
|
|
|
|
your question has been answered in the other forum you asked it in - a friendly bit of advice - please dont cross post - pick the best forum, post the question once !
'g'
|
|
|
|
|
Hello
I have been trying to export a string array from my C# unmanged DLL export to metatrader , but can not do that , please need you expert suggestion ..
[DllExport("_CheckStringArray", CallingConvention = CallingConvention.StdCall)]
public static void _CheckStringArray([In, Out, MarshalAs(UnmanagedType.LPStr)] StringBuilder [] str)
{
try
{
str[0].Append("hello");
}
catch (Exception e)
{
MessageBox.Show(e.ToString());
}
}
#import "CyboMySqlRead.dll"
void _CheckStringArray(string s[]);
string array [3] ;
_CheckStringArray(array)
Print(array[0]);
|
|
|
|
|
Is there a shorthand for updating an object? Something like an initializer?
var item = new FooViewModel { id = "test", name = "test" };
Something like that but instead:
Record myRecord = Context.RecordRepository.GetById(1234);
myRecord {
id = 123,
name = "blah"
}
Cheers, --EA
|
|
|
|
|