我监听数据流并将数据作为插入语句存储在a中,ConcurrentQueue并使用System.Threading.Timer间隔为1000的大量插入插入数据.整个场景在静态类上运行.这是代码:
static void timer_Elapsed(object sender, ElapsedEventArgs e)
{
if (queryQueue.IsEmpty)
return;
string text = "";
//bulkBuilder is StringBuilder.
//queryQueue is ConcurrentQueue
bulkBuilder.AppendLine("PRAGMA synchronous = 0;PRAGMA count_changes = FALSE;PRAGMA journal_mode=OFF;Begin;");
while (queryQueue.TryDequeue(out text))
{
bulkBuilder.Append(text);
bulkBuilder.AppendLine(";");
}
bulkBuilder.AppendLine("Commit;");
try
{
sqlCommand.CommandText = bulkBuilder.ToString();
sqlCommand.ExecuteNonQuery();
}
catch (System.Exception ex)
{
Console.WriteLine("Error while inserting Data : " + ex.Message);
}
finally
{
bulkBuilder.Clear();
}
}
Run Code Online (Sandbox Code Playgroud)
有趣的是,sqlCommand仅用于插入,仅用ExecuteNonQuery()于此计时器.并且不时会收到错误消息"无法在datareader处于活动状态时设置commandtext".这完全是无稽之谈,因为这个代码无关与内心SQLiteDataReader的sqlCommand.
我怎样才能摆脱这个错误?
我在我的应用程序上连接到两台服务器(PROD是https,测试服务器是http).
在J2ME上:我可以毫无问题地连接到这两台服务器.在Android上我无法连接到测试服务器.当连接是http时,如果我不使用setChunkedStreamingMode,我不能得到,responseCode(StringIndexOutOfBoundsException);如果我使用setChunkedStreamingMode,响应代码是401.我该怎么办,我的错在哪里?
这是我的android代码,如果你想看J2me代码,我也可以添加它.
URL url = new URL(getUrl());
URLConnection conn = url.openConnection();
HttpURLConnection httpConn = (HttpURLConnection) conn;
httpConn.setAllowUserInteraction(false);
httpConn.setInstanceFollowRedirects(true);
httpConn.setConnectTimeout(10000);
httpConn.setRequestProperty("User-Agent", util.getDeviceFullModel()
+ " " + util.getSoftwareVersion());
httpConn.setRequestProperty("Accept-Charset", "utf-8");
httpConn.setRequestProperty("Content-Type",
"text/xml; charset=utf-8");
httpConn.setRequestProperty("SOAPAction",
"http://tempuri.org/IAuthenticationServiceForGroup/"+conTypeString);
httpConn.setRequestProperty("Software-Version", AppData.VERSION);
httpConn.setChunkedStreamingMode(getParams().getBytes("UTF8").length);
httpConn.setRequestMethod("POST");
httpConn.setDoOutput(true);
httpConn.setDoInput(true);
httpConn.connect();
os = httpConn.getOutputStream();
os.write(getParams().getBytes("UTF8"));
try {
os.close();
} catch (Exception e) {
onError(e);
}
response=httpConn.getResponseCode();
Run Code Online (Sandbox Code Playgroud)
J2ME代码:
HttpConnection c = (HttpConnection)XConnection.openConnection(XConnection.SERVER + "AuthenticationServiceForGroup.svc");
c.setRequestProperty("User-Agent", XUtil.getDeviceFullModel() + " " + XUtil.getSoftwareVersion());
c.setRequestProperty("Content-Type", "text/xml; …Run Code Online (Sandbox Code Playgroud) 我对连续创建的线程的执行顺序有疑问.这是代码.
#include <iostream>
#include <Windows.h>
#include <boost/thread.hpp>
using namespace std;
boost::mutex mutexA;
boost::mutex mutexB;
boost::mutex mutexC;
boost::mutex mutexD;
void SomeWork(char letter, int index)
{
boost::mutex::scoped_lock lock;
switch(letter)
{
case 'A' : lock = boost::mutex::scoped_lock(mutexA); break;
case 'B' : lock = boost::mutex::scoped_lock(mutexB); break;
case 'C' : lock = boost::mutex::scoped_lock(mutexC); break;
case 'D' : lock = boost::mutex::scoped_lock(mutexD); break;
}
cout << letter <<index << " started" << endl;
Sleep(800);
cout << letter<<index << " finished" << endl;
}
int main(int argc , …Run Code Online (Sandbox Code Playgroud) 我设法得到一个.net对象的地址
GCHandle objHandle = GCHandle.Alloc(obj,GCHandleType.WeakTrackResurrection);
int address = GCHandle.ToIntPtr(objHandle).ToInt32();
Run Code Online (Sandbox Code Playgroud)
我可以通过回忆起这个对象
Object obj = GCHandle.FromIntPtr(IntPtr(address)).Target;
Run Code Online (Sandbox Code Playgroud)
好吧,目的是将地址存储在本机类中,并具有哪个本机对象与哪个.net对象相关联的信息.
AFAIK地址不会因为分配而改变,是真的还是有人有更好的想法来实现我的目的?
谢谢
我有一个.NT类,它具有多个委托,用于从本机代码进行回调.是否有必要分配所有代表?我的意思是确实GCHandle.Alloc()只保护代表或拥有该代表的整个类被收集?
由于动态程序集加载需要appdomain加载以启用使用卸载相关appdomain来终止程序集,是否在要加载的进程中有"最大"数量的appdomains?我在想一个基于服务器的应用程序,每个用户都可以动态运行他的专有C#代码.但是,如果2000用户登录并加载他们的代码怎么办?除了appdomians的数量之外,我可能遇到任何可能的限制吗?
谢谢.
正如Hans Passant所 希望的那样这是我的情景.我有一个混合模式应用程序,其中本机代码执行所有艰苦的工作,同时尊重性能和托管代码只负责GUI.用户也将通过编写他们的专有C#代码参与.我有本地类的C++,GUI和用户代码的C#以及介于两者之间的包装类的C++/Cli.在我的所有C++类中,有一个执行%90的计算,每次都创建一个不同的参数.我们称之为NativeClass.有apprx.这个NativeClass的2000个实例,我必须在计算之前找到与某个参数相关的正确实例.所以我设计了一个hash_map,其参数是哈希码,用于此目的.当我得到一个参数时,我在hash_map中寻找正确的实例,我找到它并调用它的一些方法.
当用户通过编写C#代码来控制计算时,该类通过回调执行这些代码.这是微不足道的,但有时我需要一些关于用户构建的.Net类的信息.所以我需要以某种方式将特定的ManagedClass附加到NativeClass.我的第一个解决方案是使用GChandle.Alloc()并传输句柄地址.但有一些担心 GC不会正常工作.Hans建议Marshal.AllocCoTaskMem()和Marshal.StructureToPtr()在非托管内存中分配托管对象,但我相信这对值类型类或结构有效.ref类怎么样?如何在防止GC收集并使GC同时正常工作的同时传递对NativeClass的引用?
以下是一些示例代码:
class NativeClass
{
private:
int AddressOfManagedHandle;
public:
static NativeClass * GetNativeClassFromHashMap(int SomeParameter)
{
// return NativeClass associated with SomeParameter from NativeClassHashMap;
}
NativeClass(int addr, int SomeParameter) : AddressOfManagedHandle(addr)
{
}
int GetAddress(){return AddressOfManagedHandle;}
void DoCalculation(){
// CALCULATIONS
}
};
public ref class ManagedClass : MarshalByRefObject
{
private:
NativeClass* _nc;
//GCHandle handle;
void FreeManagedClass()
{
Marshal::FreeHGlobal(IntPtr(_nc->GetAddress()));
//if(handle.IsAllocated)
//handle.Free();
delete _nc;
}
public:
ManagedClass()
{
IntPtr addr = (Marshal::AllocHGlobal(Marshal::Sizeof(this))); // Error
Marshal::StructureToPtr(this,addr,true);
//handle …Run Code Online (Sandbox Code Playgroud) 我需要在我的C++代码中运行R中的统计函数.有没有办法从我的C++代码中调用它们?我正在使用VS 2005,我是否需要包含任何头文件或链接任何库?我使用他们网站提供的标准Windows安装程序安装了R.
问候,
正如标题所说,我想专门为字符串和字符指针设置一个函数模板,到目前为止,我做了这个,但我无法弄清楚通过引用传递字符串参数.
#include <iostream>
#include <string.h>
template<typename T> void xxx(T param)
{
std::cout << "General : "<< sizeof(T) << std::endl;
}
template<> void xxx<char*>(char* param)
{
std::cout << "Char ptr: "<< strlen(param) << std::endl;
}
template<> void xxx<const char* >(const char* param)
{
std::cout << "Const Char ptr : "<< strlen(param)<< std::endl;
}
template<> void xxx<const std::string & >(const std::string & param)
{
std::cout << "Const String : "<< param.size()<< std::endl;
}
template<> void xxx<std::string >(std::string param)
{
std::cout …Run Code Online (Sandbox Code Playgroud) 以下代码无法编译,为什么会这样?我该如何解决这个问题?
struct A{
template<int N> int get() { return N; }
};
template <typename X>
struct B : public X {
template<int N> int get() {
return X::get<N>();
}
};
int main(int argc, const char *argv[])
{
B<A> b;
return b.get<5>();
}
Run Code Online (Sandbox Code Playgroud)
编译错误:
test.cxx: In member function ‘int B<X>::get()’:
test.cxx:8:30: error: expected primary-expression before ‘)’ token
test.cxx: In member function ‘int B<X>::get() [with int N = 5, X = A]’:
test.cxx:15:25: instantiated from here
test.cxx:8:30: error: invalid operands of types …Run Code Online (Sandbox Code Playgroud) .net ×5
c# ×5
c++ ×4
templates ×2
android ×1
appdomain ×1
boost ×1
boost-thread ×1
c++-cli ×1
callback ×1
char ×1
delegates ×1
http-error ×1
inheritance ×1
java-me ×1
marshalling ×1
mutex ×1
pinning ×1
pointers ×1
r ×1
sqlite ×1
string ×1
visual-c++ ×1